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