初学spring init-method="init" destroy-method="destroy" scope="prototype"
2015-03-21 15:24
344 查看
beans.xml
UserServiceTest.java
当加了scope=”prototype”,前面两个方法就没用了,测试的结果是,运行new ClassPathXmlApplicationContext(“beans.xml”)不会初始化,而在调用getBean方法时,有两次调用,就两次初始化调用了init方法,最后没有调用销毁,如果去掉scope=”prototype”,new ClassPathXmlApplicationContext(“beans.xml”)就会初始化,只有一次,最后结束会调用销毁方法。所以前面两个不要和第三个一起用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"> </bean> <bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype"> <!-- <property name="userDAO" ref="u" /> --> <constructor-arg> <ref bean="u"/> </constructor-arg> </bean> </beans>
UserServiceTest.java
package com.bjsxt.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection //Inverse of Control public class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); UserService service2 = (UserService)ctx.getBean("userService"); ctx.destroy(); } }
当加了scope=”prototype”,前面两个方法就没用了,测试的结果是,运行new ClassPathXmlApplicationContext(“beans.xml”)不会初始化,而在调用getBean方法时,有两次调用,就两次初始化调用了init方法,最后没有调用销毁,如果去掉scope=”prototype”,new ClassPathXmlApplicationContext(“beans.xml”)就会初始化,只有一次,最后结束会调用销毁方法。所以前面两个不要和第三个一起用
相关文章推荐
- 关于Spring的destroy-method和scope="prototype"不能共存问题
- Spring init-method和destroy-method属性的使用
- spring bean 中的init-method和destroy-method (带示例说明)
- Spring注解@Scope("prototype")
- Spring init-method and destroy-method example
- spring&nbsp;scope=&quot;prototype&quot;&amp;nb…
- Spring init-method和destroy-method 的使用
- Spring的配置文件 destroy-method="close"的作用
- struts+spring action应配置为scope="prototype"
- spring scope="prototype" 和scope="singleton"区分
- spring scope="prototype" 和scope="singleton"区分
- struts+spring action应配置为scope="prototype"
- Spring注解@Scope("prototype")
- spring <bean scope="prototype">探讨
- spring scope="prototype" 学习笔记
- struts+spring action应配置为scope="prototype"
- Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例
- Spring注解之@Scope("prototype")
- Spring 注解 @Scope("prototype") 的作用
- spring scope="prototype" 和scope="singleton"区分