Java Framework Spring Boot learning Note (c): Scope of beans

Source: Internet
Author: User
Tags getmessage

The Spring framework Bean supports the following five scopes:

Here are two scopes, Singleton and Protoype

Singleton scope

The singleton scope is the default scope, and Getbean is the same bean within the same IOC container, and if you create an object instance that is defined by a singleton scope Bean, the instance is stored in the bean's cache, then all future targets for that Both the Bean's request and the reference return the cached object.

Writing Helloworld.java

1  Packagecom.example.spring;2 3  Public classHelloWorld {4     PrivateString message;5      Public voidsetmessage (String message) {6          This. Message =message;7     }8      Public voidGetMessage () {9System.out.println ("Your Message:" +message);Ten     } One}

Write Beans.xml, set to singleton scope

1 <?xml version= "1.0" encoding= "UTF-8"?>2 <beans xmlns= "http://www.springframework.org/ Schema/beans "3        xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "4        xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd ">56     class=" Com.example.spring.HelloWorld "scope="  Singleton">7     </bean>8 </beans>

Writing Application.java

1  Packagecom.example.spring;2 3 Importorg.springframework.beans.factory.BeanFactory;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6  Public classApplication {7      Public Static voidMain (string[] args) {8         //Where the bean configuration file is located d:\\ideaprojects\\spring\\src\\beans.xml9         //using the Beanfactory containerTenBeanfactory factory =NewClasspathxmlapplicationcontext ("File:d:\\ideaprojects\\spring\\src\\beans.xml"); OneHelloWorld Obja = (HelloWorld) factory.getbean ("HelloWorld"); AObja.setmessage ("I ' m object A"); - obja.getmessage (); -HelloWorld OBJB = (HelloWorld) factory.getbean ("HelloWorld"); the objb.getmessage (); -     } -}

Run output

Your message:i ' m object a12:06:56.273 [main] DEBUG Org.springframework.beans.factory.support.defaultlistablebeanfactory-returning cached instance of singleton beans ' HelloWorld 'Your message:i' m object A

Prototype scope

If the scope is set to prototype, each creation of an object instance is configured for the current instance only Bean,getbean is a different bean.

Set the above Beans.xml to prototype scope

1 <?xml version= "1.0" encoding= "UTF-8"?>2 <beans xmlns= "http://www.springframework.org/ Schema/beans "3        xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "4        xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd ">56     class=" Com.example.spring.HelloWorld "scope="  Prototype">7     </bean>8 </beans>

Run output:

Your message:i ' m object a13:52:03.767 [main] DEBUG org.springframework.beans.factory.support.defaultlistablebeanfactory-creating instance of beans ' helloWorld ' 13:52:03.767 [main] DEBUG org.springframework.beans.factory.support.defaultlistablebeanfactory-finished creating instance of beans ' HelloWorld ' Your message:null

  

Java Framework Spring Boot learning Note (c): Scope of beans

Related Article

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.