Userservice.java
Package com.bjsxt.service;import Com.bjsxt.dao.userdao;import com.bjsxt.model.User; Public classUserService {PrivateUserdao Userdao; Public voidinit () {System. out. println ("Init"); } Public voidAdd (user user) {userdao.save (user); } PublicUserdao Getuserdao () {returnUserdao; } Public voidSetuserdao (Userdao Userdao) { This. Userdao =Userdao; } PublicUserService (Userdao Userdao) {super (); This. Userdao =Userdao; } Public voiddestroy () {System. out. println ("Destroy"); }}
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/beanshttp//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> <refbean="u"/> </constructor-arg> </bean> </beans>
@Test Public voidTestadd () throws Exception {Classpathxmlapplicationcontext ctx=NewClasspathxmlapplicationcontext ("Beans.xml"); UserService Service= (UserService) Ctx.getbean ("UserService"); UserService Service2= (UserService) Ctx.getbean ("UserService"); Generally do not mix with protytyoe so see destory if is singleton see two times init two times deatory Ctx.destroy (); }
Plus Lazy-init, the first time the object does not initialize is only used when the initialization of the relatively few
Spring re-contact life cycle