Spring (iv) configuration and life cycle of the JavaBean action range

Source: Internet
Author: User

There are several types of Bean's scope:

    • Singleton in each spring IOC container, a bean defines only one instance of an object. The bean is initialized by default when the container starts, but we can specify the bean node's lazy-init= "true" to defer initializing the bean, so only the bean is initialized for the first time. Such as:
      <bean id= ":" class= ":" lazy-init= "true"/>
      If you want to apply lazy initialization to all beans, you can set default-lazy-init= "true" at the root node beans, as follows:
      <beans default-lazy-init= "true" >
    • prototype every time a bean gets a new object from the container
There are three other types of applications that are applied in Web project: RequestSessionGlobal Session
Package Test.spring.jnit;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Test.spring.service.impl.personservicebean;public class Beanscopetest {@Testpublic void Testscope () { ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Beans.xml");//------------------------ -------------------------Personservicebean personServiceBean1 = (Personservicebean) Applicationcontext.getbean (" Personservice "); Personservicebean personServiceBean2 = (Personservicebean) applicationcontext.getbean ("Personservice"); System.out.println (PersonServiceBean1 = = PersonServiceBean2);}}
This code returns true by the previous configuration, but if you change the scope of the Personservicebean
<bean id= "Personservice" class= "Test.spring.service.impl.PersonServiceBean" scope= "prototype" ></bean>
It will return false.

<bean id= "Personservice" class= "Test.spring.service.impl.PersonServiceBean" lazy-init= "false" init-method= "Init "Destroy-method=" ></bean> "destroy"
After the initialization of the bean is instantiated, the destruction releases the resource after the spring container is closed
Package Test.spring.service.impl;import Test.spring.service.personservice;public class Personservicebean implements Personservice {public void init () {System.out.println ("Initialize");} Public Personservicebean () {System.out.println ("instantiate Now");} @Overridepublic void Save () {System.out.println ("=============");} public void Destroy () {System.out.println ("Frees Resources");}}

Package Test.spring.jnit;import Org.junit.test;import Org.springframework.context.support.abstractapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Beanscopetest {@Testpublic void Testscope () {//ApplicationContext ApplicationContext = new//classpathxmlapplicationcontext (//"beans.xml");// If scope= "Singleton", the Bean is now instantiated//System.out.println ("-------------------------------");/* * If scope= "prototype", Now instantiate the Bean. * If scope= "singleton", the Bean is instantiated at this time by setting lazy-init= "true" to delay the instantiation. *///Personservicebean personServiceBean1 = (Personservicebean)//applicationcontext//. Getbean ("Personservice");// Personservicebean personServiceBean2 = (Personservicebean)//applicationcontext//. Getbean ("Personservice");//// System.out.println (PersonServiceBean1 = = personServiceBean2);////return Trueabstractapplicationcontext Abstractapplicationcontext = new Classpathxmlapplicationcontext ("Beans.xml"); Abstractapplicationcontext.close ();}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If you want to reprint, please specify the source: Http://blog.csdn.net/lindonglian

Spring (iv) configuration and life cycle of the JavaBean action range

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.