Java Spring中注解详解(创建Bean的对象)

spring注解(创建Bean的对象)

@Controller     C: SpringMVC框架

@Service      M: service

@Repository    M: MyBatis/SpringJDBC

@Component   JavaBean不属于M或C层,并且需要通过spring创建对象

注意:依赖AOP机制的,引入aop-jar包

1、操作属性   setter   构造方法

     基本类型   +  API  类           @Value(“”)不需要setter

@Component
class Courses1 implements Courses{
    @Value("语文")
    private String name;

    @Override
    public String toString() {
        return "Courses1{" +
                "name='" + name + '\'' +
                '}';
    }
}

2、注意Component后面可以添加id,使用方法和在config配置bean中的id的使用方法相同:

package zj.it.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zj.it.entity.Teacher;
import zj.it.four_two.Student;
import zj.it.twenty_seven.Hello;

/**
 * Created by Jiang on 2019/3/27
 */
public class Test {

    public static void main(String[] args){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationConfig.xml");
      
        Couser1 student = (Couser1 ) applicationContext.getBean("stu");
        System.out.println(student);
    }

}

3、如果需要使用list和map,可以直接使用@Autowired注解:

@Autowired
    @Qualifier("one")
    private Book books;

4、上面代码中的Book是一个接口, @Qualifier("one")调用的是接口的一个实现类(Qualifier用来选择要使用的类),one表示实现类的id。Book类如下:

package zj.it.four_two;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * Created by Jiang on 2019/4/2
 */
@Component("book")
public interface Book {
    String setName();
}
@Service("one")
class Book1 implements Book{

    @Value("战争中")
    private String name;

    public Book1() {
        System.out.println("安徒生童话");
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String setName() {
        System.out.println("安徒生童话");
        return "你好";
    }

    @Override
    public String toString() {
        return "Book1{" +
                "name='" + name + '\'' +
                '}';
    }
}
@Service("two")
class Book2 implements Book{

    @Value("战争中ssssss")
    private String name;

    public void setName(String name) {
        this.name = name;
    }


    public Book2() {
        System.out.println("格林童话");
    }

    @Override
    public String setName() {
        System.out.println("格林童话");
        return "你好啊";
    }

    @Override
    public String toString() {
        return "Book2{" +
                "name='" + name + '\'' +
                '}';
    }
}

5、上述代码测试结果如下:

Student{name='朱江', age=22, book=[Book1{name='战争中'}, Book2{name='战争中ssssss'}], 
bookMap={one=Book1{name='战争中'}, two=Book2{name='战争中ssssss'}}, coursesList=
[Courses1{name='语文'}, Courses1{name='数学'}, Courses1{name='英语'}, Courses1{name='物理'}, 
Courses1{name='化学'}], books=Book1{name='战争中'}, stringSet=[sssss, sssseweerss], 
booksLength=[Book1{name='吱吱吱吱1'}, Book2{name='吱吱吱吱2'}, Book1{name='吱吱吱吱3'}, 
Book2{name='吱吱吱吱4'}], classRooms=ClassRooms{name='一班'}, teachers=Teachers{yid=111111,
 name='朱江水水水水'}}

6、完整Student类代码如下:

package zj.it.four_two;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import zj.it.entity.ClassRoom;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Jiang on 2019/4/2
 */
@Component("stu")
public class Student {

    @Value("朱江")
    private String name;
    @Value("22")
    private int age;
    private List book;
    private Map bookMap;
    private List coursesList;
    @Autowired
    @Qualifier("one")
    private Book books;

    private Set stringSet;

    private Book[] booksLength;
    private ClassRooms classRooms;
    private Teachers teachers;


    public Student() {
    }

    @Autowired()
    public Student(List book, Map bookMap,Teachers teachers,ClassRooms classRooms,List coursesList) {
        this.book = book;
        this.bookMap = bookMap;
        this.teachers = teachers;
        this.classRooms = classRooms;
        this.coursesList = coursesList;
    }

    public void setBooksLength(Book[] booksLength) {
        this.booksLength = booksLength;
    }

    public void setStringSet(Set stringSet) {
        this.stringSet = stringSet;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", book=" + book +
                ", bookMap=" + bookMap +
                ", coursesList=" + coursesList +
                ", books=" + books +
                ", stringSet=" + stringSet +
                ", booksLength=" + Arrays.toString(booksLength) +
                ", classRooms=" + classRooms +
                ", teachers=" + teachers +
                '}';
    }
}

先说到这,有补充欢迎留言。

你可能感兴趣的:(Java)