一、Java Base Container configuration
The central aitifacts in Spring’s new Java-configuration are @Configuration
anntotated classes and @Bean
annotated methods.
@Configuration
public class Student {
@Bean
public User myUser() {
return new User();
}
}
测试:
public void test01() {
ApplicationContext ac = new AnnotationConfigApplicationContext(Student.class);
System.out.println(ac.getBean("myUser",User.class));
}
在上面的测试例子中:即使Student上面没有@Configuration
注解也能正常工作,Spring Container中的bean的名称为方法的名称(myUser)
1.1 使用register(Class)的方式来创建容器
An AnnotationConfigApplicationContext may be instantiated using a no-arg constructor and then configured using the register() method. This approach is particularly useful when programmatically building an AnnotationConfigApplicationContext.
Controller
@Configuration
public class StudentController {
@Autowired
private StudentService studentService;
public Student getStudent() {
return studentService.getStudent();
}
}
Service
@Configuration
public class StudentService {
@Autowired
private StudentDAO studentDAO;
public Student getStudent() {
return studentDAO.getStudent();
}
}
@Configuration
public class StudentDAO {
public Student getStudent() {
Student student = new Student();
student.setAge(20);
student.setName("gbx");
return student;
}
}
测试类:
public void test01() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
ac.register(StudentController.class);
ac.register(StudentService.class);
ac.register(StudentDAO.class);
ac.refresh();
StudentController sc = ac.getBean(StudentController.class);
System.out.println(sc.getStudent().getAge());
}
结果会输出DAO中注入进去的20,上面的例子中即使是Controller、Service、DAO上面不加@Configuration,也能正常运行,因为已近将这几类交给了Spring容器。
1.2 包的扫描
在上面例子中,在StudentController上面加上:
@Configuration
@ComponentScan(basePackages="com.google")
public class StudentController {
此时就可以在测试类中直接写:
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
ac.register(StudentController.class);
ac.refresh();
StudentController sc = ac.getBean(StudentController.class);
System.out.println(sc.getStudent().getAge());
因为在注册StudentController的时候,发现上面有注解ComponentScan,此时会扫描包,当类上面有注解@Component、@Configuration时全都会自动注册进Spring容器中,因此不需要显示地加载Service和DAO了,但是此时的Service和DAO上面必须有相应的注解,否则报错。
更简单的写法是:
public void test01() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
ac.scan("com.google");
ac.refresh();
StudentController sc = ac.getBean(StudentController.class);
System.out.println(sc.getStudent().getAge());
}
此时直接进行包的扫描,不需要指定具体注册的类。其中ac.refresh()很关键,如果有类注册进来,必须进行refresh操作,否则取不到相应的Bean。
同XML扫描类型:java-annotation 扫描也会扫描@Component @Controller @Service @Repository @Configuration这些注解类,并将其交给Spring容器。
1.3 @Bean注解
当进行包的扫描时,也会扫描组件中的@Bean注解,将其注册在Spring容器中:
@Configuration
public class StudentController {
@Autowired
private StudentService studentService;
@Bean
public AccountService accountService() {
return new AccountService();
}
此时,会将一个AccountService的实例注册在容器中,通过ac.getBean(“accountService”,AccountService.class)来取到注册进来的bean。