Spring4.0MVC學習資料,ApplicationContext中的方法具體解釋(三)

來源:互聯網
上載者:User

標籤:

做為java開源的一部分,spring架構一直排在老大的位置。Spring4.0 是 Spring 推出的一個重大版本號碼升級,進一步加強了 Spring 作為 Java 領域第一開源平台的地位。Spring4.0 引入了眾多 Java 開發人員期盼的新特性,如泛型依賴注入、SpEL、校正及格式化架構、Rest風格的 WEB 編程模型等。這些新功能有用性強、易用性高,可大幅減少 JavaEE 開發的難度,同一時候有效提升應用開發的優雅性。為了方便開發,Spring的ApplicationContext類,給我們提供了非常多有用的方法,我在這裡進行一下解說。

看配置代碼(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/schema/tx            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">               <description>    herman    </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="20"></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="20"></constructor-arg></bean><bean id="person4" class="com.herman.ss.pojo.Person"><constructor-arg type="int" value="20"></constructor-arg><constructor-arg type="java.lang.String" value="herman"></constructor-arg></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="20"></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="20" 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>
在看測試代碼:
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 使用getBeansOfType擷取bean對象集合 * @author Herman.Xiong * @date 2014年8月6日15:38:10 */public static void test0(){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 使用containsBean推斷bean對象是否存在 * @author Herman.Xiong * @date 2014年8月6日15:40:18 */public static void test1(){ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");System.out.println(ctx.containsBean("person0"));}/** * @see 使用getBeanDefinitionCount統計定義bean對象的數量 * @author Herman.Xiong * @date 2014年8月6日15:42:34 */public static void test2(){ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");System.out.println(ctx.getBeanDefinitionCount());}/** * @see 使用getBeanDefinitionNames擷取定義的bean的名字 * @author Herman.Xiong * @date 2014年8月6日15:45:50 */public static void test3(){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 依據bean名字擷取bean的別名 * @author Herman.Xiong * @date 2014年8月6日16:20:54 */public static void test4(){ApplicationContext 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/** * 由於bean的名字由兩部分組成: * (1) 預設名字。當定義了bean的id屬性時,預設名字為id屬性的值; *  沒有定義id時,取name屬性的第一個值;id和name均沒有定義時,取類名。 * (2) 別名,除預設名字以外的其它名字。 */}/** * @see isPrototype,isSingleton推斷是否為多例,單例,原型 * @author Herman.Xiong * @date 2014年8月6日16:25:56 */public static void test5(){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 使用isTypeMatch方法推斷bean對象是否為指定類型 */public static void test6(){ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");System.out.println(ctx.isTypeMatch("person0", House.class));}/** * @see 其它測試 */public static void test7(){ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");System.out.println(ctx.getApplicationName());//模型的應用的名字System.out.println(ctx.getDisplayName());System.out.println(ctx.getStartupDate());System.out.println(ctx.findAnnotationOnBean("person0", Component.class));//返回相應的註解執行個體(參數指定了Bean的配置名稱和須要返回的註解的類型)System.out.println(ctx.getBeanNamesForAnnotation(Component.class));//返回全部使用了相應註解annotationType的Bean/** * ConfigurableBeanFactory 這個介面定義了設定父容器(ParentBeanFactory),設定類裝載器,設定屬性編輯器(PropertyEditorRegistrar)等一系列功能,增強了IoC容器的可定製性 AutowireCapableBeanFactory 定義了一些自己主動裝配Bean的方法 SingletonBeanRegistry 這個介面沒有繼承BeanFactory,它主要定義了在執行期間向容器注冊單例模式Bean的方法 BeanDefinitionRegistry 這個介面沒有繼承BeanFactory,它主要定義了向容器中注冊BeanDefinition對象的方法在Spring設定檔裡,每個<bean>節點元素在Spring容器中都是由一個BeanDefinition對象描寫敘述的) */}public static void main(String[] args) {//test0();//test1();//test2();//test3();//test4();//test5();//test6();test7();}}
自己執行測試一把,是不是感覺非常爽。

歡迎大家關注我的個人部落格!!!!
如有不懂,疑問或者欠妥的地方,請加QQ群:135430763   進行反饋,共同學習!

Spring4.0MVC學習資料,ApplicationContext中的方法具體解釋(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.