Spring Bean Range Example

Source: Internet
Author: User

Spring The goal is to create objects by default single

Sets the Bean range. By Bean Dollar Scope property

Scope Value Range:

Singleton: Single case

Proptotype: Non-singleton

Request: Create the Bean and call Request.setattribute ("Beanid", beanobj);

Session: Create the Bean and call Request.getsession (). SetAttribute ("Beanid", beanobj);

Globalsession: Global Session Distributed server

Test code

public class Initbeanapp {@Testpublic void Show () {ApplicationContext ac = new Classpathxmlapplicationcontext (" Applicatiocontext.xml "); Bean1 bean1 = (Bean1) ac.getbean ("bean2"); Bean1 bean2 = (Bean1) ac.getbean ("bean2"); Bean1 bean3 = (Bean1) ac.getbean ("Bean3"); Bean1 Bean4 = (Bean1) ac.getbean ("Bean3"); System.out.println (BEAN1); System.out.println (BEAN2); System.out.println (BEAN3); System.out.println (BEAN4);}}


Configuration Code

<!--bean scope--><!--scope: Used to set the scope default value of the Bean: Singleton  Singleton--><bean id= "bean2" class= " Com.hao947.bean.Bean1 "scope=" singleton "></bean><!--the scope of the bean--><!--scope: For setting the bean range Default: Prototype--><bean id= "Bean3" class= "Com.hao947.bean.Bean1" scope= "prototype" ></bean>


Spring Bean Range Example

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.