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配置:


猜你喜欢

转载自blog.csdn.net/weixin_44543131/article/details/113172805