SPRING4.0MVC study Materials, ApplicationContext method in detail (iii)

Source: Internet
Author: User

As part of Java Open Source, the spring framework is always in the boss's place. Spring4.0 is a major version of spring's upgrade that further strengthens spring's position as the first open source platform in the Java world. Spring4.0 introduces new features that many Java developers expect, such as generic dependency injection, SPEL, checksum format framework, RESTful WEB programming model, and more. These new features are practical and easy to use, greatly reducing the difficulty of Java EE development, while effectively improving the elegance of application development. For the convenience of development, spring's ApplicationContext class, provides us with many practical methods, I am here to explain.

Look at the configuration Code (applicationcontext2.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" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns: p= "http://www.springframework.org/schema/p" xsi:schemalocation= "Http://www.springframework.org/schema/beans http            ://www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.0.xsd Http://www.springframework.org/schema /AOP http://www.springframework.org/schema/aop/spring-aop-4.0.xsd Http://www.springframework.org/sche Ma/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <description> Herm An </description> <alias name= "nn" alias= "abc"/>    <bean class= "Com.herman.ss.pojo.Person" ></bean><bean id= "Person0" class= "Com.herman.ss.pojo.Person "Name=" a,b,c,d,e "></bean><bean id=" Person1 "class=" Com.herman.ss.pojo.Person "name=" M,n ">< Property Name= "Age" value= "></property><property" name= "name" value= "Herman" ></property> </bean><bean id= "Person2" class= "Com.herman.ss.pojo.Person" ><property name= "age" ><value> 20</value></property><property name= "Name" ><value>herman</value></property> </bean><bean id= "Person3" class= "Com.herman.ss.pojo.Person" ><constructor-arg name= "name" value= " Herman "></constructor-arg><constructor-arg name=" Age "value=" ></constructor-arg></ Bean><bean id= "Person4" class= "Com.herman.ss.pojo.Person" ><constructor-arg type= "int" value= ">" </constructor-arg><constructor-arg type= "java.lang.String" value= "Herman" &GT;&LT;/CONSTRUCTOR-ARG&GT;</bean><bean id= "House" class= "Com.herman.ss.pojo.House" ><constructor-arg><null></ Null></constructor-arg><constructor-arg name= "Address" value= "Shanghai" ></constructor-arg> <constructor-arg name= "Price" value= "10000000.12" ></constructor-arg></bean><bean id= "person5 "Class=" Com.herman.ss.pojo.Person "><constructor-arg type=" int "value=" ></constructor-arg>< Constructor-arg type= "java.lang.String" value= "Herman" ></constructor-arg><constructor-arg type= " Com.herman.ss.pojo.House "ref=" house "></constructor-arg></bean><bean id=" Person6 "class=" Com.herman.ss.pojo.Person "><constructor-arg type=" int "value=" "index=" 1 "></constructor-arg>< Constructor-arg value= "Herman" index= "0" ></constructor-arg><constructor-arg type= " Com.herman.ss.pojo.House "ref=" house "></constructor-arg></bean></beans>
Look at the test code:
Package Com.herman.ss.test;import Java.util.map;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.stereotype.component;import Com.herman.ss.pojo.house;import Com.herman.ss.pojo.Person;public Class Test2 {/** * @see get a collection of Bean objects using Getbeansoftype * @author Herman.xiong * @date August 6, 2014 15:38:10 */public static void t Est0 () {ApplicationContext ctx=new classpathxmlapplicationcontext ("Com/herman/ss/config/applicationcontext2.xml") ; Map<string, person> Map=ctx.getbeansoftype (Person.class); for (String Key:map.keySet ()) {System.out.println (" Key= "+ key +" and value= "+ map.get (key));}} /** * @see Use Containsbean to determine if a Bean object exists * @author Herman.xiong * @date August 6, 2014 15:40:18 */public static void Test1 () {applic Ationcontext ctx=new classpathxmlapplicationcontext ("Com/herman/ss/config/applicationcontext2.xml"); System.out.println (Ctx.containsbean ("Person0"));} /** * @see Use GetbeandefinitioncountStatistics define the number of Bean objects * @author Herman.xiong * @date August 6, 2014 15:42:34 */public static void Test2 () {ApplicationContext ctx=new Cla Sspathxmlapplicationcontext ("Com/herman/ss/config/applicationcontext2.xml"); System.out.println (Ctx.getbeandefinitioncount ());} /** * @see use Getbeandefinitionnames to get the name of the defined bean * @author herman.xiong * @date August 6, 2014 15:45:50 */public static void test 3 () {ApplicationContext ctx=new classpathxmlapplicationcontext ("Com/herman/ss/config/applicationcontext2.xml"); String Beanname []= ctx.getbeandefinitionnames (); for (int i = 0; i < beanname.length; i++) {System.out.println (beanname [i]);}} /** * @see Get the Bean alias based on the bean name * @author Herman.xiong * @date August 6, 2014 16:20:54 */public static void Test4 () {Applicationcont Ext ctx=new Classpathxmlapplicationcontext ("Com/herman/ss/config/applicationcontext2.xml"); String[] Aliases=ctx.getaliases ("a"); for (int i = 0; i < aliases.length; i++) {System.out.println (aliases[i]);} person0,b,e,c,d/** * Because the name of the bean consists of two parts: * (1) The default name. When a bean's ID attribute is defined, the default name is the value of the id attribute; * When an ID is undefined, the first value of the Name property is taken, and the name is not defined, when the ID and name are undefined. * (2) aliases, other than the default name. */}/** * @see Isprototype,issingleton to determine whether it is a multi-instance, single, prototype * @author Herman.xiong * @date August 6, 2014 16:25:56 */public static void t Est5 () {ApplicationContext ctx=new classpathxmlapplicationcontext ("Com/herman/ss/config/applicationcontext2.xml") ; System.out.println (Ctx.isprototype ("Person0")); System.out.println (Ctx.issingleton ("Person0")); System.out.println (Ctx.istypematch ("Person0", House.class));} /** * @see Use the Istypematch method to determine whether the bean object is of the specified type */public static void Test6 () {ApplicationContext ctx=new Classpathxmlapplicationcontext ("Com/herman/ss/config/applicationcontext2.xml"); System.out.println (Ctx.istypematch ("Person0", House.class));} /** * @see Other tests */public static void Test7 () {ApplicationContext ctx=new classpathxmlapplicationcontext ("com/herman/ss/ Config/applicationcontext2.xml "); System.out.println (Ctx.getapplicationname ());//The application name of the model SYSTEM.OUT.PRINTLN (Ctx.getdisplayname ()); System.out.println (Ctx.getstartupdate ()); SysteM.out.println (Ctx.findannotationonbean ("Person0", Component.class));// Returns the corresponding annotation instance (the parameter specifies the bean's configuration name and the type of note to be returned) System.out.println (Ctx.getbeannamesforannotation (Component.class)); Returns all bean/** * configurablebeanfactory that use the corresponding annotation Annotationtype This interface defines the settings parent container (parentbeanfactory), sets the class loader, sets the property editor ( Propertyeditorregistrar), which enhances the customization of IOC containers Autowirecapablebeanfactory defines some methods for automating the assembly of Beans Singletonbeanregistry This interface does not inherit beanfactory, it mainly defines the method of registering a singleton pattern bean to the container during runtime beandefinitionregistry This interface does not inherit Beanfactory, It primarily defines the method of registering a Beandefinition object into a container in a spring configuration file, where each <bean> node element is described by a Beandefinition object in the Spring container */}public static void Main (string[] args) {//test0 ();//test1 ();//test2 ();//test3 ();//test4 ();//test5 ();//test6 (); Test7 ();}}
Run the test yourself, is not feeling very cool.

Welcome everyone to pay attention to my personal blog!!!!
If you do not understand, doubt or inappropriate place, please add QQ Group: 135430763 to Feedback, learn together!

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.