Spring re-contact life cycle

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.