1.spring 3.0 增加了---->AnnotationConfigApplicationContext初始化容器方法(之前我们常用Applicationcontext 这个摆脱不了xml配置)
2.有了AnnotationConfigApplicationContext---------->增加了@Configuration和@Bean(共同使用)
3.@Configuration等于@Component
4.@Bean等于bean配置方法,同时@Bean方法必须公开,有返回值
5.@Bean的属性有四:1.name;2.initMethod;3.destoryMethod;4.autowire (作用与xml差不多,仅4没有了constructor寻找方法)
6.看以下代码:
组件1:
package test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Configuration
public class person {
@Bean
public person demo() {
System.out.println("人造1号初始化完毕!");
return new person();
}
}
组件2:
package test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
@Configuration
public class person2 {
@Bean
public person2 demo1() {
System.out.println("人造2号初始化完毕!");
return new person2();
}
}
运行类:
package test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class aac {
public static void main(String[] args) {
AnnotationConfigApplicationContext aac=new AnnotationConfigApplicationContext("test");
}
}
运行结果:
需要注意的是AnnotationConfigApplicationContext aac = new AnnotationConfigApplicationContext("test"); //参数部分是写进包名的,而不像ApplicationContext的classpathxmlapplication("配置文件名");
总结:第一次无需配置进行aop测试,感觉非常好!!!!
Spring 3.0 注解配置详解
本文介绍 Spring 3.0 中引入的注解配置功能,包括使用 @Configuration 和 @Bean 替代 XML 配置的方法。通过示例代码展示了如何创建和初始化 Bean,以及 AnnotationConfigApplicationContext 的用法。
907

被折叠的 条评论
为什么被折叠?



