Applicationcotext. 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: P = "http://www.springframework.org/schema/p" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <! -- Inject bean Attributes --> <bean id = "mybean" class = "com. zxiaoyao. beans. mybean "> <property name =" name "value =" Zhang San "/> <property name =" Age "value =" 100 "/> </bean> <! -- Inject through constructor --> <bean id = "mybean2" class = "com. zxiaoyao. beans. mybean2 "> <constructor-Arg value =" "/> <constructor-Arg value =" 200 "/> </bean> <! -- Inject other beans --> <bean id = "mybean3" class = "com. zxiaoyao. beans. mybean3 "> <property name =" my2 "ref =" mybean2 "/> </bean> <! -- Inject internal Bean --> <bean id = "mybean5" class = "com. zxiaoyao. beans. mybean5 "> <property name =" my4 "> <Bean class =" com. zxiaoyao. beans. mybean4 "/> </property> </bean> <! -- This configuration applies to list or array --> <bean id = "mybean6" class = "com. zxiaoyao. beans. mybean6 "> <property name =" mys "> <list> <ref bean =" mybean "/> <ref bean =" mybean2 "/> <ref bean =" mybean3 "/> <ref bean = "mybean5"/> </List> </property> </bean> <! -- Set assembly --> <! -- Set --> <bean id = "mybean7" class = "com. zxiaoyao. beans. mybean6 "> <property name =" mys "> <set> <ref bean =" mybean "/> <ref bean =" mybean2 "/> <ref bean =" mybean3 "/> <ref bean = "mybean5"/> </set> </property> </bean> <! -- Map --> <bean id = "mybean8" class = "com. zxiaoyao. beans. mybean6 "> <property name =" mymap "> <map> <Entry key =" M "value-ref =" mybean "/> <Entry key =" m2 "value-ref = "mybean2"/> <Entry key = "m3" value-ref = "mybean3"/> <Entry key = "M5" value-ref = "mybean5"/> </ map> </property> </bean> <! -- Properties --> <bean id = "mybean9" class = "com. zxiaoyao. beans. mybean7 "> <property name =" properties "> <props> <prop key =" S1 "> world's first 2 people </prop> <prop key =" S2 "> world's second 2 people </prop> <prop key = "S3"> second place in the world </prop> <prop key = "S4"> second place in the world </prop> </props> </property> </bean> <! -- Automatically assemble this method and set byname, bytype, constructor, and autodetect for global settings on the default-autowire = "" attribute of the beans node --> <bean id = "mybean10" class = "com. zxiaoyao. beans. mybean8 "autowire =" byname "> </bean> <bean id =" mybean11 "class =" com. zxiaoyao. beans. mybean8 "autowire =" bytype "> </bean> <bean id =" mybean12 "class =" com. zxiaoyao. beans. mybean8 "autowire =" constructor "> </bean> <bean id =" mybean13 "class =" com. zxiaoyao. beans. mybean8 "autow Ire = "autodetect" Scope = "Singleton"> <! -- Scope Attributes --> <! -- Singleton defines the bean range as one instance for each spring container --> <! -- Prototype defines that the bean can be instantiated multiple times (an instance is created once) --> <! -- Request defines the bean range as HTTP request. Valid only when spring context with web capabilities (such as springmvc) is used --> <! -- Session defines the bean range as HTTP session. Valid only when spring context with web capabilities (such as springmvc) is used --> <! -- Global-session defines that the bean scope is a global http session. Only in the Portlet --> </bean> <! -- Factory method to create Bean --> <bean id = "methodinstance" class = "com. zxiaoyao. Beans. singletonbean" factory-method = "createinstance"> </bean> <! -- Initialize and destroy beans --> <! -- On the beans node, you can set the call global initialization and destruction --> <bean id = "mybean14" class = "com. zxiaoyao. beans. mybean9 "init-method =" initmethod "Destroy-method =" cleanmethod "> </bean> <! -- This bean implements initializingbean, And the disposablebean interface implements its afterpropertiesset destroy method to initialize and destroy beans --> <bean id = "mybean15" class = "com. zxiaoyao. beans. mybean10 "> </bean> </beans>
The get set and tostring methods are removed from the required bean code snippets.
Public class mybean implements mybeani {private string name; private int age; // ------------------------------- public class mybean2 implements mybeani {private string name; private int age; // define public class mybean3 implements mybeani {private string name; private int age; private mybean2 my2; // define public class mybean4 implements mybeani {private string name; private int age; // define public class mybean5 implements mybeani {private string name; private int age; private mybean4 my4; // define public class mybean6 implements mybeani {private string name; private int age; private mybean my; private collection <mybeani> mys; private Map <string, mybeani> mymap; // define public class mybean7 implements mybeani {private properties; @ overridepublic string tostring () {stringbuilder sb250 = new stringbuilder (); For (iterator <Object> it = properties. keyset (). iterator (); it. hasnext ();) {string key = (string) it. next (); sb250.append ("[" + key + ":" + properties. getproperty (key) + "]"). append ("\ n");} return sb250.tostring ();} // ------------------------------------------------- public class mybean8 implements mybeani {private mybean2 mybean2; // -------------------------------------------- public class mybean9 implements mybeani {private string name; private int age; Public void initmethod () {system. out. println ("init anyting .... ");} public void cleanmethod () {system. out. println ("clean all ........ ");} // --------------------------------------------- public class mybean10 implements mybeani, initializingbean, disposablebean {private string name; private int age; Public void initmethod () {system. out. println ("init anyting. 10... ");} public void cleanmethod () {system. out. println ("clean all .... 10 .... ");} public void afterpropertiesset () throws exception {initmethod ();} public void destroy () throws exception {cleanmethod ();} // define public interface mybeani {} // -------------------------------------------------- public class singletonbean {private string name; private int age; private singletonbean () {This. name = "the three"; this. age = 101;} Private Static class innersingletonclass {static singletonbean instance = new singletonbean ();} public static singletonbean createinstance () {return innersingletonclass. instance ;}
Test class
public class Mytest {public static void main(String[] args) throws InterruptedException {test1();}public static void test1(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");MyBean myb = (MyBean)context.getBean("mybean");System.out.println(myb.getName());MyBean2 myb2 = (MyBean2)context.getBean("mybean2");System.out.println(myb2);MyBean3 myb3 = (MyBean3) context.getBean("mybean3");System.out.println(myb3.getMy2().toString());MyBean5 myb5 = (MyBean5) context.getBean("mybean5");System.out.println(myb5.getMy4().toString());MyBean6 myb6 = (MyBean6) context.getBean("mybean6");System.out.println(myb6.getMys());MyBean6 myb7 = (MyBean6) context.getBean("mybean7");System.out.println(myb7.getMys());MyBean6 myb8 =(MyBean6) context.getBean("mybean8");System.out.println(myb8.getMymap().get("m2"));MyBean7 myb9 = (MyBean7)context.getBean("mybean9");System.out.println(myb9.toString());MyBean8 myb10 = (MyBean8)context.getBean("mybean10");System.out.println(myb10.getMybean2());MyBean8 myb11 = (MyBean8)context.getBean("mybean11");System.out.println(myb11.getMybean2());MyBean8 myb12 = (MyBean8)context.getBean("mybean12");System.out.println(myb12.getMybean2());MyBean8 myb13 = (MyBean8)context.getBean("mybean13");System.out.println(myb13.getMybean2());SingletonBean sb = (SingletonBean) context.getBean("methodInstance");System.out.println(sb);DisposableBean db = (DisposableBean)context;try {db.destroy();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}