Spring4.0MVC学习资料,ApplicationContext中的方法具体解释(三)
做为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中的方法具体解释(三)的更多相关文章
- Spring4.0MVC学习资料,注解自己主动扫描bean,自己主动注入bean(二)
Spring4.0的新特性我们在上一章已经介绍过了. 包含它对jdk8的支持,Groovy Bean Definition DSL的支持.核心容器功能的改进,Web开发改进.測试框架改进等等.这张我们 ...
- jquery中end()方法的解释
来源:http://www.jquery001.com/jquery-end-method.html 对于end()方法,jQuery文档是这样解释的:jQuery回到最近的一个"破坏性&q ...
- 好记性不如烂笔头86-spring3学习(7)-ApplicationContext中bean的生命周期
假设使用ApplicationContext来生成.管理Bean, 一个Bean从建立到销毁,会历经几个运行阶段. 我个人理解一般的bean的生命周期主要包含:建立,初始化,使用阶段,销毁四个核心阶段 ...
- Spring4.0学习笔记(6) —— 通过工厂方法配置Bean
1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...
- jquery中ajax方法返回的三种数据类型:text、json、xml;
1.当dataType:"text"时,处理页面用的是DBDA类中的Strquery()方法,所以返回的数据是下面这样的,所以要对返回来的数据用split根据“|”和“^”来分割, ...
- Asp.net mvc 中Action 方法的执行(三)
[toc] 前面介绍了 Action 方法执行过程中的一些主要的组件以及方法执行过程中需要的参数的源数据的提供以及参数的绑定,那些都可以看作是 Action 方法执行前的一些必要的准备工作,接下来便将 ...
- Python学习-18.Python中的错误处理(三)
在某些情况下,我们需要定义自己的异常并且抛出 先定义一个错误: class MyError(BaseException): def __init__(self): pass 上面定义了一个叫MyErr ...
- 在学习c++过程中,总结类的三个用户以及使用权限,感觉非常实用
首先我们需要知道类的三个用户分别是:类的实现者,类的普通用户和类的继承者(派生类),接下来分别讲解这几种用户的区别. 1 .类的实现者:顾明思议,就是类的设计者,拥有最大的权限,可以访问类中任何权限的 ...
- IOS开发UIImage中stretchableImageWithLeftCapWidth方法的解释
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...
随机推荐
- 日志收集之kafka
日志收集之kafka http://www.jianshu.com/p/f78b773ddde5 一.介绍 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1 ...
- Xshell怎样登陆本地虚拟机
Xshell怎样登陆本地虚拟机 本经验介绍了怎样使用Xshell登陆本地虚拟机,这里以centos为例.其实其它远程登陆,原理也是一样的. 工具/原料 VMware虚拟机 Xshell远程登陆工具 ...
- hdu3622(二分+two-sat)
传送门:Bomb Game 题意:给n对炸弹可以放置的位置(每个位置为一个二维平面上的点),每次放置炸弹是时只能选择这一对中的其中一个点,每个炸弹爆炸的范围半径都一样,控制爆炸的半径使得所有的爆炸范围 ...
- AngularJS是为了克服HTML在构建应用上的不足而设计的
AngularJS中文网:http://www.apjs.net/ 简介 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构 ...
- UFLDL教程笔记及练习答案二(预处理:主成分分析和白化)
首先将本节主要内容记录下来.然后给出课后习题的答案. 笔记: :首先我想推导用SVD求解PCA的合理性. PCA原理:如果样本数据X∈Rm×n.当中m是样本数量,n是样本的维数.PCA降维的目的就是为 ...
- BaiduMap_SDK_DEMO_3.0.0_for_Xamarin.Android_by_imknown
2.4.2 已稳定, 同一时候已经放置到分支/Release 2.4.2了. 3.0.0 已开发完毕, 可是不推荐大家用于项目中, 请观望或者自己进一步调试. 个人感觉尽管3.0.0简化了开发, 可是 ...
- hdu4956 Poor Hanamichi
解决暴力的直接方法.一个直接的推论x%11方法. 打表可以发现,以解决不同的情况都不会在很大程度上会出现. 所以从l暴力开始枚举.找到的第一个错误值输出要. 如果它超过r同样在美国发现-1. #inc ...
- iOS_25彩票_幸运转盘
终于效果图: 各个view的关系图: 背景圆盘(须要扣图处理)LuckyBaseBackground.png 盖在背景圆盘上面的转盘 LuckyRotateWheel.png watermark/2/ ...
- python手记(52)
python将信息加密进图片 从图片中解密信息 >>> runfile(r'K:\testpro\test1.py', wdir=r'K:\testpro') http://blog ...
- Lichee(两) 在sun4i_crane该平台下编译
让我们先来回顾一下编译命令 $ cd workdir/lichee $ ./build.sh -p sun4i_crane -k 3.0 lichee文件夹下的build.sh #!/bin/bas ...