Spring的配置,bean后处理器,工厂使用以及生命周期等

//最原始的 //@Test /public void myTest() { IStudentService iStudentService=new StudentServiceimpl(); iStudentService.doFirst(); iStudentService.doDestroy(); }/ //使用配置文件 @Test public void myTest() { String resource = “applicationContext.xml”; //------------<<< //以文件路径 //ApplicationContext applicationContext=new FileSystemXmlApplicationContext(“classpath:applicationContext.xml”); //配置多个spring:例如spring-student.xml,spring-school.xml等,则路径资源可写为spring-; //在总spring.xml中,使用import标签引入其他spring-(…).xml文件,即可在路径资源下只引用总配置文件spring.xml //直接使用工厂创建 //IStudentService iStudentService=new StudentFactory().getStudentFactory(); //iStudentService.doDestroy(); //用bean注册创建 //StudentFactory factory=(StudentFactory) applicationContext.getBean(“serviceFactory”); //---------------factory.getStudentFactory(); //用bean的工厂注册创建 //IStudentService iStudentService=(IStudentService) applicationContext.getBean(“someStudentFactory”); //iStudentService.doDestroy(); / * scope作用域: * prototype,每次都会创建一个对象,原型模式 * singleton:单例模式,默认 * IStudentService iStudentService2=(IStudentService) * applicationContext.getBean(“serviceStudent”); * System.out.println(iStudentService2); * System.out.println(iStudentService); */ //------------>>> //以当前类路径 ApplicationContext applicationContext=new ClassPathXmlApplicationContext(resource); //最原始的方式 IStudentService iStudentService=(IStudentService) applicationContext.getBean(“serviceStudent”); iStudentService.doDestroy(); String doFirst = iStudentService.doFirst(); System.out.println(doFirst); //关闭容器 ((ClassPathXmlApplicationContext)applicationContext).close(); }

ApplicationContext配置:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值