做为java开源的一部分,spring框架一直排在老大的位置。Spring4.0 是 Spring 推出的一个重大版本号升级,进一步加强了 Spring 作为 Java 领域第一开源平台的地位。Spring4.0 引入了众多 Java 开发人员期盼的新特性,如泛型依赖注入、SpEL、校验及格式化框架、Rest风格的 WEB 编程模型等。这些新功能有用性强、易用性高,可大幅减少 JavaEE 开发的难度,同一时候有效提升应用开发的优雅性。为了方便开发,Spring的ApplicationContext类,给我们提供了非常多有用的方法,我在这里进行一下解说。

看配置代码(applicationContext2.xml):

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  14.  
  15. <description>
  16. herman
  17. </description>
  18.  
  19. <alias name="nn" alias="abc"/>
  20.  
  21. <bean class="com.herman.ss.pojo.Person"></bean>
  22.  
  23. <bean id="person0" class="com.herman.ss.pojo.Person" name="a,b,c,d,e"></bean>
  24.  
  25. <bean id="person1" class="com.herman.ss.pojo.Person" name="m,n">
  26. <property name="age" value="20"></property>
  27. <property name="name" value="herman"></property>
  28. </bean>
  29.  
  30. <bean id="person2" class="com.herman.ss.pojo.Person">
  31. <property name="age">
  32. <value>20</value>
  33. </property>
  34. <property name="name">
  35. <value>herman</value>
  36. </property>
  37. </bean>
  38.  
  39. <bean id="person3" class="com.herman.ss.pojo.Person">
  40. <constructor-arg name="name" value="herman"></constructor-arg>
  41. <constructor-arg name="age" value="20"></constructor-arg>
  42. </bean>
  43.  
  44. <bean id="person4" class="com.herman.ss.pojo.Person">
  45. <constructor-arg type="int" value="20"></constructor-arg>
  46. <constructor-arg type="java.lang.String" value="herman"></constructor-arg>
  47. </bean>
  48.  
  49. <bean id="house" class="com.herman.ss.pojo.House">
  50. <constructor-arg>
  51. <null></null>
  52. </constructor-arg>
  53. <constructor-arg name="address" value="Shanghai"></constructor-arg>
  54. <constructor-arg name="price" value="10000000.12"></constructor-arg>
  55. </bean>
  56.  
  57. <bean id="person5" class="com.herman.ss.pojo.Person">
  58. <constructor-arg type="int" value="20"></constructor-arg>
  59. <constructor-arg type="java.lang.String" value="herman"></constructor-arg>
  60. <constructor-arg type="com.herman.ss.pojo.House" ref="house"></constructor-arg>
  61. </bean>
  62.  
  63. <bean id="person6" class="com.herman.ss.pojo.Person">
  64. <constructor-arg type="int" value="20" index="1"></constructor-arg>
  65. <constructor-arg value="herman" index="0"></constructor-arg>
  66. <constructor-arg type="com.herman.ss.pojo.House" ref="house"></constructor-arg>
  67. </bean>
  68. </beans>

在看測试代码:

  1. package com.herman.ss.test;
  2.  
  3. import java.util.Map;
  4.  
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. import org.springframework.stereotype.Component;
  8.  
  9. import com.herman.ss.pojo.House;
  10. import com.herman.ss.pojo.Person;
  11.  
  12. public class Test2 {
  13.  
  14. /**
  15. * @see 使用getBeansOfType获取bean对象集合
  16. * @author Herman.Xiong
  17. * @date 2014年8月6日15:38:10
  18. */
  19. public static void test0(){
  20. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
  21. Map<String, Person> map=ctx.getBeansOfType(Person.class);
  22. for (String key : map.keySet()) {
  23. System.out.println("key= "+ key + " and value= " + map.get(key));
  24. }
  25. }
  26.  
  27. /**
  28. * @see 使用containsBean推断bean对象是否存在
  29. * @author Herman.Xiong
  30. * @date 2014年8月6日15:40:18
  31. */
  32. public static void test1(){
  33. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
  34. System.out.println(ctx.containsBean("person0"));
  35. }
  36.  
  37. /**
  38. * @see 使用getBeanDefinitionCount统计定义bean对象的数量
  39. * @author Herman.Xiong
  40. * @date 2014年8月6日15:42:34
  41. */
  42. public static void test2(){
  43. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
  44. System.out.println(ctx.getBeanDefinitionCount());
  45. }
  46.  
  47. /**
  48. * @see 使用getBeanDefinitionNames获取定义的bean的名字
  49. * @author Herman.Xiong
  50. * @date 2014年8月6日15:45:50
  51. */
  52. public static void test3(){
  53. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
  54. String beanName []= ctx.getBeanDefinitionNames();
  55. for (int i = 0; i < beanName.length; i++) {
  56. System.out.println(beanName[i]);
  57. }
  58. }
  59.  
  60. /**
  61. * @see 依据bean名字获取bean的别名
  62. * @author Herman.Xiong
  63. * @date 2014年8月6日16:20:54
  64. */
  65. public static void test4(){
  66. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
  67. String[] aliases=ctx.getAliases("a");
  68. for (int i = 0; i < aliases.length; i++) {
  69. System.out.println(aliases[i]);
  70. }
  71. //person0,b,e,c,d
  72. /**
  73. * 由于bean的名字由两部分组成:
  74. * (1) 默认名字。当定义了bean的id属性时,默认名字为id属性的值;
  75. * 没有定义id时,取name属性的第一个值;id和name均没有定义时,取类名。
  76. * (2) 别名,除默认名字以外的其它名字。
  77. */
  78. }
  79.  
  80. /**
  81. * @see isPrototype,isSingleton推断是否为多例,单例,原型
  82. * @author Herman.Xiong
  83. * @date 2014年8月6日16:25:56
  84. */
  85. public static void test5(){
  86. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
  87. System.out.println(ctx.isPrototype("person0"));
  88. System.out.println(ctx.isSingleton("person0"));
  89. System.out.println(ctx.isTypeMatch("person0", House.class));
  90. }
  91.  
  92. /**
  93. * @see 使用isTypeMatch方法推断bean对象是否为指定类型
  94. */
  95. public static void test6(){
  96. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
  97. System.out.println(ctx.isTypeMatch("person0", House.class));
  98. }
  99.  
  100. /**
  101. * @see 其它測试
  102. */
  103. public static void test7(){
  104. ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext2.xml");
  105. System.out.println(ctx.getApplicationName());//模型的应用的名字
  106. System.out.println(ctx.getDisplayName());
  107. System.out.println(ctx.getStartupDate());
  108. System.out.println(ctx.findAnnotationOnBean("person0", Component.class));//返回相应的注解实例(參数指定了Bean的配置名称和须要返回的注解的类型)
  109. System.out.println(ctx.getBeanNamesForAnnotation(Component.class));//返回全部使用了相应注解annotationType的Bean
  110. /**
  111. * ConfigurableBeanFactory
  112. 这个接口定义了设置父容器(ParentBeanFactory),设置类装载器,
  113. 设置属性编辑器(PropertyEditorRegistrar)等一系列功能,增强了IoC容器的可定制性
  114. AutowireCapableBeanFactory
  115. 定义了一些自己主动装配Bean的方法
  116. SingletonBeanRegistry
  117. 这个接口没有继承BeanFactory,它主要定义了在执行期间向容器注冊单例模式Bean的方法
  118. BeanDefinitionRegistry
  119. 这个接口没有继承BeanFactory,它主要定义了向容器中注冊BeanDefinition对象的方法
  120. 在Spring配置文件里,每个<bean>节点元素在Spring容器中都是由一个BeanDefinition对象描写叙述的)
  121. */
  122. }
  123.  
  124. public static void main(String[] args) {
  125. //test0();
  126. //test1();
  127. //test2();
  128. //test3();
  129. //test4();
  130. //test5();
  131. //test6();
  132. test7();
  133. }
  134. }

自己执行測试一把,是不是感觉非常爽。

欢迎大家关注我的个人博客!!!!

如有不懂,疑问或者欠妥的地方,请加QQ群:135430763   进行反馈,共同学习!

Spring4.0MVC学习资料,ApplicationContext中的方法具体解释(三)的更多相关文章

  1. Spring4.0MVC学习资料,注解自己主动扫描bean,自己主动注入bean(二)

    Spring4.0的新特性我们在上一章已经介绍过了. 包含它对jdk8的支持,Groovy Bean Definition DSL的支持.核心容器功能的改进,Web开发改进.測试框架改进等等.这张我们 ...

  2. jquery中end()方法的解释

    来源:http://www.jquery001.com/jquery-end-method.html 对于end()方法,jQuery文档是这样解释的:jQuery回到最近的一个"破坏性&q ...

  3. 好记性不如烂笔头86-spring3学习(7)-ApplicationContext中bean的生命周期

    假设使用ApplicationContext来生成.管理Bean, 一个Bean从建立到销毁,会历经几个运行阶段. 我个人理解一般的bean的生命周期主要包含:建立,初始化,使用阶段,销毁四个核心阶段 ...

  4. Spring4.0学习笔记(6) —— 通过工厂方法配置Bean

    1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...

  5. jquery中ajax方法返回的三种数据类型:text、json、xml;

    1.当dataType:"text"时,处理页面用的是DBDA类中的Strquery()方法,所以返回的数据是下面这样的,所以要对返回来的数据用split根据“|”和“^”来分割, ...

  6. Asp.net mvc 中Action 方法的执行(三)

    [toc] 前面介绍了 Action 方法执行过程中的一些主要的组件以及方法执行过程中需要的参数的源数据的提供以及参数的绑定,那些都可以看作是 Action 方法执行前的一些必要的准备工作,接下来便将 ...

  7. Python学习-18.Python中的错误处理(三)

    在某些情况下,我们需要定义自己的异常并且抛出 先定义一个错误: class MyError(BaseException): def __init__(self): pass 上面定义了一个叫MyErr ...

  8. 在学习c++过程中,总结类的三个用户以及使用权限,感觉非常实用

    首先我们需要知道类的三个用户分别是:类的实现者,类的普通用户和类的继承者(派生类),接下来分别讲解这几种用户的区别. 1 .类的实现者:顾明思议,就是类的设计者,拥有最大的权限,可以访问类中任何权限的 ...

  9. IOS开发UIImage中stretchableImageWithLeftCapWidth方法的解释

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...

随机推荐

  1. [poj 2991]Crane[线段树表示向量之和,而非数量]

    题意: 起重机的机械臂, 由n段组成, 对某一些连接点进行旋转, 询问每次操作后的末端坐标. 思路: 由于旋转会影响到该点之后所有线段的角度, 因此容易想到用线段树记录角度, 成段更新. (但是不是每 ...

  2. 开发测试时给 Kafka 发消息的 UI 发送器――Mikasa

    开发测试时给 Kafka 发消息的 UI 发送器――Mikasa 说来话长,自从入了花瓣,整个人就掉进连环坑了. 后端元数据采集是用 Storm 来走拓扑流程的,又因为 @Zola 不是很喜欢 Jav ...

  3. 实现ListView A~Z快速索引

    ListView A~Z快速索引这种效果在通信录和城市列表中经常看到,方便用户查找,是一种增加用户体验的好方法. 实现步骤: 1.自定义一个名叫SlideBar 的View. 2.在布局文件中加入这个 ...

  4. Oracle数据库案例整理-Oracle系统执行时故障-Shared Pool内存不足导致数据库响应缓慢

    1.1       现象描写叙述 数据库节点响应缓慢,部分用户业务受到影响. 查看数据库告警日志,開始显示ORA-07445错误,然后是大量的ORA-04031错误和ORA-00600错误. 检查数据 ...

  5. 轻松学习之Linux教程六 正則表達式具体解释

    本系列文章由@超人爱因斯坦出品.转载请注明出处. 作者:超人爱因斯坦    个人站点:http://www.hpw123.net          文章链接:http://hpw123.net/a/L ...

  6. hdu5046(重复覆盖+二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:要在n个城市里建造不超过k个机场覆盖所有城市,问机场城市之间最大距离最小为多少. 分析:二 ...

  7. IOS 与ANDROID框架及应用开发模式对照一

    IOS 和ANDROID操作系统都是眼下流行的移动操作系统,被移动终端和智能设备大量採用,两者都採用了先进的软件技术进行设计,为了方便应用开发两者都採用了先进的设计模式. 两者在框架设计上都採用了什么 ...

  8. [Sqlite] 移动嵌入式数据库Sqlite日报SQL操作语句汇总

    ,EXPLAIN分析 没有建立索引之前.分析都是表扫描: sqlite> EXPLAIN SELECT *  FROM COMPANY  WHERE Salary < 20000; add ...

  9. php-GD库的函数(二)

    <?php //imagecopy — 拷贝图像的一部分粘贴到某图像上 /*bool imagecopy ( resource $dst_im , resource $src_im , int ...

  10. Cordova CLI源码分析(三)——初始化

    本部分主要涉及以下三个文件 1 cli.js 2 cordova.js 3 events.js 通过前一篇package.json的分析,可以知道,当命令行执行cordova相关命令时,首先调用mai ...