Spring Learning (6) Examples of---bean definitions and scope

Source: Internet
Author: User

A The definition of a bean

First define a Beanannotation

Package Com.mypackage;import Org.springframework.stereotype.Component, @Componentpublic class Beanannotation {public void Say (String args) {System.out.println ("beanannotation:" +args);}}

XML configuration:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans"        Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"        xmlns:context= "Http://www.springframework.org/schema /context "        xsi:schemalocation=" Http://www.springframework.org/schema/beans/            http Www.springframework.org/schema/beans/spring-beans-4.1.xsd            Http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-4.1.xsd ">                <context:component-scan Base-package= "Com.mypackage" >        </context:component-scan>    </beans>

Test:

Package Com.mypackage;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class UnitTest {@Testpublic void Test () { ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:spring-beansnnotation.xml");  Beanannotation bean= (beanannotation) Context.getbean ("Beanannotation"); Bean.say ("Test");}}

Test results:

2015-7-6 11:36:44 org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh Info: Refreshing Org[email protected]1342a80d:startup Date [Mon Jul 11:36:44 CST 2015]; Root of context hierarchy2015-7-6 11:36:44 Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [spring-beansnnotation.xml]beanannotation: Test

(ii) Scope instances

Specify the scope:

Package Com.mypackage;import Org.springframework.context.annotation.scope;import Org.springframework.stereotype.Component, @Scope ("Prototype") @Componentpublic class Beanannotation {public void Say ( String args) {System.out.println ("beanannotation:" +args);} public void Myhashcode () {System.out.println ("Beanannotation:" +this.hashcode ());}}

Configuration 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"        xmlns:context= "Http://www.springframework.org/schema /context "        xsi:schemalocation=" Http://www.springframework.org/schema/beans/            http Www.springframework.org/schema/beans/spring-beans-4.1.xsd            Http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-4.1.xsd ">                <context:component-scan Base-package= "Com.mypackage" >        </context:component-scan>    </beans>

Unit tests:

Package Com.mypackage;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class UnitTest {@Testpublic void Test () { ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:spring-beansnnotation.xml");  Beanannotation bean= (beanannotation) Context.getbean ("Beanannotation"); Bean.myhashcode (); Beanannotation bean11= (beanannotation) Context.getbean ("Beanannotation"); Bean11.myhashcode ();}}

Results:

2015-7-6 12:54:03 org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh Info: Refreshing Org[email protected]1342a80d:startup Date [Mon Jul 12:54:03 CST 2015]; Root of context hierarchy2015-7-6 12:54:03 Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [spring-beansnnotation.xml]beanannotation : 1617829356beanannotation:1567531625

The hashcode of a bean is not the same as two different objects.

Note the above @scope ("prototype"), which is @scope, that is, using the default value

The results obtained:

2015-7-6 13:00:30 org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh Info: Refreshing Org[email protected]1342a80d:startup Date [Mon Jul 13:00:30 CST 2015]; Root of context hierarchy2015-7-6 13:00:30 Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [spring-beansnnotation.xml]beanannotation : 1617829356beanannotation:1617829356

The description gets the same object.

Spring Learning (6) Examples of---bean definitions and scope

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.