Spring自带了多种应用上下文

AnnotationConfigApplicationContext:从一个或多个java配置类中加载应用上下文

AnnotationConfigWebApplicationContext:从一个或多个java配置类加载web的应用上下文

ClassPathXmlApplicationContext: 从类路径下的xml文件中加上上下文定义

FileSystemXmlApplicationContext:从文件系统下的xml文件加载上下文定义

XmlWebApplicationContext:从web应用下的xml文件加载上下文

FileSystemXmlApplicationCont 和 ClassPathXmlApplicationContext的区别是

FileSystemXmlApplicationCont是指定的文件系统下去寻找对应的xml文件

ClassPathXmlApplicationContext是在所有的类路径下去寻找xml文件的

 ApplicationContext context = new FileSystemXmlApplicationContext("d:/test.xml");

 or

 ApplicationContext context = new ClassPathXmlApplicationContext("test.xml");

bean的生命周期

本地实验bean去实现其中的几个接口,例如 BeanNameAware, BeanPostProcessor, DisposableBean

代码大致如下,其中LogUtil自行替换即可

Applicationcontext的工具类,这里仅仅用class来获取,其他方法类似

public class SpringContextHolder {

    private static ApplicationContext innerContext;

    @Autowired
public void setInnerContext(ApplicationContext context){
innerContext = context;
} public static <T> T getBean(Class<T> clazz){
return innerContext.getBean(clazz);
} }
 @Component
public class TestBean implements BeanNameAware, BeanPostProcessor, DisposableBean { private String name; public void setBeanName(String name) {
LogUtil.info("name = " + name);
this.name = name;
} public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
LogUtil.info("post Before invoked, name " + beanName);
return bean;
} public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
LogUtil.info("post After invoked, name " + beanName);
return bean;
} public void destroy() throws Exception {
LogUtil.info("be destroy");
}
}

xml配置开启注解即可,这里不贴了

还有另一个与其一样的类,不同点是 打印到控制台的内容有 22 作为前缀

测试类大致如下

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring.xml")
public class SpringTest { @Test
public void test(){
try {
TestBean testBean = SpringContextHolder.getBean(TestBean.class);
TestBean2 testBean2 = SpringContextHolder.getBean(TestBean2.class);
} catch (QuestFailedException e) {
e.printStackTrace();
}
} }

运行结果如下

 post Before invoked, name testB
22 post Before invoked, name testB
post After invoked, name testB
22 post After invoked, name testB
post Before invoked, name testA
22 post Before invoked, name testA
post After invoked, name testA
22 post After invoked, name testA
post Before invoked, name springContextHolder
22 post Before invoked, name springContextHolder
post After invoked, name springContextHolder
22 post After invoked, name springContextHolder
post Before invoked, name org.springframework.context.event.internalEventListenerProcessor
22 post Before invoked, name org.springframework.context.event.internalEventListenerProcessor
post After invoked, name org.springframework.context.event.internalEventListenerProcessor
22 post After invoked, name org.springframework.context.event.internalEventListenerProcessor
post Before invoked, name org.springframework.context.event.internalEventListenerFactory
22 post Before invoked, name org.springframework.context.event.internalEventListenerFactory
post After invoked, name org.springframework.context.event.internalEventListenerFactory
22 post After invoked, name org.springframework.context.event.internalEventListenerFactory
post Before invoked, name quest
22 post Before invoked, name quest
post After invoked, name quest
22 post After invoked, name quest
post Before invoked, name knight
22 post Before invoked, name knight
post After invoked, name knight
22 post After invoked, name knight
post Before invoked, name minstrel
22 post Before invoked, name minstrel
post After invoked, name minstrel
22 post After invoked, name minstrel

可见第二个接口是针对spring环境下的所有bean的,当有多个接口的实现者时,均会被调用

spring的包情况

Spring容器配置的三种方式

xml配置

java类配置

隐式的bean发现机制和自动配置

重点讲解第三点,spring从2个角度来实现自动化装配

组件扫描:spring自动发现应用上下文所创建的bean

自动装配:spring自动满足bean之间的依赖

基于java类配置的自动扫描

 @Configuration
@ComponentScan
public class CDPlayerConfig {
// nothing
}

ComponentScan会默认扫描该类所在的包的类,及其子包所有的类,这个与传统的在xml中配置 <context:component-scan base-package="xx" /> 的用法是一样的

@ComponentScan(value="xxx") 表明xxx是基础包 与 @ComponentScan(basePackages={"x1","x2"}) 是等价的

还可以是 @ComponentScan(basePackageClasses={a.class, b.class})  这类类锁在的包,也会作为扫描的基础包

bean类

 // 接口
public interface CompactDisc {
void play();
} // 一个实现类
@Component
public class SgtPeppers implements CompactDisc{ private String title = "sgt title"; private String artist = " paul "; public void play() {
LogUtil.info(title + " -> " + artist);
}
}

测试类

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest { @Autowired
private CompactDisc compactDisc; @Test
public void test(){
Assert.assertNotNull(compactDisc);
} }

运行的结果是通过,表明通过这种方式,成功的将bean装配了

这里顺带说下 Autowired 和 Resource的区别

Autowired自动装备,默认先按照类型去查找,如果找到了唯一的实例,那么顺利装配,如果不唯一,则需要使用 @Qualifier来指定

Resource则默认按照属性名去寻找,如果没有找到,则按照类型寻找,按类型寻找发现不唯一,也会报错,这个时候,就需要同  @Qualifier 一起使用

按上面的代码 修改2个bean类,以及测试类如下

 @Component(value = "cdc")
public class CdCommon implements CompactDisc { public void play() {
LogUtil.info("i'm cdc");
}
} @Component(value = "sp")
public class SgtPeppers implements CompactDisc{ private String title = "sgt title"; private String artist = " paul "; public void play() {
LogUtil.info(title + " -> " + artist);
}
} // 测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CDPlayerConfig.class)
public class CDPlayerTest { @Autowired
@Qualifier(value = "sp")
private CompactDisc compactDisc; @Resource
@Qualifier(value = "cdc")
private CompactDisc cdc1;
// private CompactDisc cdc; @Test
public void test(){
Assert.assertNotNull(compactDisc);
// Assert.assertNotNull(cdc);
Assert.assertNotNull(cdc1);
} }

这个运行的结果也是可以的

spring xml 方式声明bean ,参数有list时,可以这么使用

最新文章

  1. 3D几何变换
  2. jquery获取input表单值的代码
  3. Redis和Memcached对比
  4. 在Android开发中使用Ant 一:环境的搭建及入门
  5. php基础05:常量
  6. [SAP ABAP开发技术总结]将文件存储到数据库表中,并可发送邮件
  7. 【原创】lua编译时发现缺少readline库
  8. opengl Test
  9. Esper系列(一)初探
  10. ubuntu server修改时区
  11. c语言枚举型常量
  12. 堆C数组实现
  13. 洛谷 P3410 拍照
  14. 20165328 实验四《Andriid应用开发》实验报告
  15. arrow function
  16. linux命令之free篇
  17. NDK/JNI学习--环境搭建
  18. Spark2 broadcast广播变量
  19. jQuery学习-键盘事件
  20. router基本使用

热门文章

  1. XSS绕过小结
  2. 第三周四则运算辅助(CAI)结对项目需求文档
  3. Excel如何快速统计一列中相同数值出现的个数--数据透视表
  4. Consumer高级特性
  5. LeetCode第十八题-四数之和
  6. spring cloud 集群健康监控--turbine-dashboard仪表盘
  7. Power BI行级别安全性(数据权限管理)
  8. “Excel-建议不可用于您所选择的数据”错误提示
  9. identifier of an instance of **** was altered from **** to *****
  10. 生活英语读写MOOC-Literature Tutor-有声名著阅读推荐