Spring中的Aware
来源:互联网 发布:链轮设计参数计算软件 编辑:程序博客网 时间:2024/06/06 17:54
日期: 2016-7-15
内容: spring中的Aware相关的一些接口功能描述;
1、 Spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以获取相应的资源。
2、 通过Aware接口,可以对Spring相应的资源进行操作(一定要慎重);
3、 为了对Spring进行简单的扩展提供了方便的入口。
以下对Aware相关的部分接口作介绍:ApplicationContextAware和BeanNameAware等进行介绍。
①、ApplicationContextAware:
在实现了这个接口之后,他可以提供对Spring IOC容器的上下文进行操作的功能。声明应用上下文的。
②、BeanNameAware:
提供对BeanName进行操作。
③、 ApplicationEventPublisherAware:
主要用于事件的发布。
④、BeanClassLoadAware:
相关的类加载器。
⑤、BeanFactoryAware:
声明BeanFactory的。
⑥、 BootstrapContextAware:
⑦、 LoadTimeWeaverAware:
一、 ApplicationContextAware接口的实现演示如下:
1、编写: com.bean.TestApplicationContextAware.java类实现ApplicationContextAware接口:
package com.bean;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class TestApplicationContextAware implements ApplicationContextAware {@Overridepublic void setApplicationContext(ApplicationContext applicationContext)throws BeansException {//这里加载的Bean是在配置文件applicationContext_Aware.xml里面进行配置的BeanSystem.out.println("applicationContext : "+applicationContext.getBean("applicationContextAware"));}}
2、 在配置文件applicationContext_Aware.xml里面配置Bean:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="applicationContextAware" class="com.bean.TestApplicationContextAware"></bean></beans>
3、编写测试类com.test..BeanLifeCycleTest.java:
<span style="white-space:pre"></span>@Testpublic void testApplicationContextAware(){//加载Spring配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_Aware.xml");}
2016/07/15 11:10:00 org.springframework.context.support.AbstractApplicationContext prepareRefresh情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@9664a1: startup date [Fri Jul 15 11:10:00 CST 2016]; root of context hierarchy2016/07/15 11:10:01 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions情報: Loading XML bean definitions from class path resource [applicationContext_LifeCycle.xml]开始日期: 2016年10月15日 11时:10分:654秒2016/07/15 11:10:01 org.springframework.context.support.AbstractApplicationContext prepareRefresh情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1484a05: startup date [Fri Jul 15 11:10:01 CST 2016]; root of context hierarchy2016/07/15 11:10:01 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions情報: Loading XML bean definitions from class path resource [applicationContext_Aware.xml]applicationContext : com.bean.TestApplicationContextAware@1e3cd51结束日期: 2016年10月15日 11时:10分:720秒
从结果分析可知:
applicationContext : com.bean.TestApplicationContextAware@1e3cd51在初始化容器的时候调用了setApplicationContext得到了以上的输出结果。
二、 BeanNameAware接口的演示:
1、 编写: com.bean.TestBeanNameAware.java类实现BeanNameAware接口:
package com.bean;import org.springframework.beans.factory.BeanNameAware;public class TestBeanNameAware implements BeanNameAware {@Overridepublic void setBeanName(String beanName) {System.out.println("BeanName: "+beanName);}}
2、 在配置文件applicationContext_Aware.xml里面配置Bean:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <span style="white-space:pre"></span><span style="white-space:pre"></span><!-- <span style="white-space:pre"></span><bean id="applicationContextAware" class="com.bean.TestApplicationContextAware"></bean><span style="white-space:pre"></span> --><span style="white-space:pre"></span> <span style="white-space:pre"></span><bean id="beanName" class="com.bean.TestBeanNameAware"></bean></beans>
3、编写测试类com.test..BeanLifeCycleTest.java:
@Testpublic void testBeanNameAware(){//加载Spring配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_Aware.xml");}
4、运行的结果如下:
2016/07/15 11:34:19 org.springframework.context.support.AbstractApplicationContext prepareRefresh情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1be2d65: startup date [Fri Jul 15 11:34:19 CST 2016]; root of context hierarchy2016/07/15 11:34:19 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions情報: Loading XML bean definitions from class path resource [applicationContext_LifeCycle.xml]2016/07/15 11:34:20 org.springframework.context.support.AbstractApplicationContext prepareRefresh情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1484a05: startup date [Fri Jul 15 11:34:20 CST 2016]; root of context hierarchy2016/07/15 11:34:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions情報: Loading XML bean definitions from class path resource [applicationContext_Aware.xml]开始日期: 2016年34月15日 11时:34分:02秒BeanName: beanName结束日期: 2016年34月15日 11时:34分:57秒
BeanName: beanName
- Spring中的Aware
- Spring中的Aware接口
- Spring中的Aware和事件
- Spring框架中的各种*Aware接口
- Spring框架中的各种*Aware接口
- Spring-Aware
- # Spring Aware
- Spring Aware
- Spring Aware
- Spring Aware (Spring 感知)
- Spring:Aware和事件
- spring-aware接口
- spring-Aware接口
- Spring Bean--Aware接口
- Spring -- 3.Aware
- spring之aware接口
- spring的Aware
- Spring Aware理解
- 安装mysql-python:EnvironmentError: mysql_config not found
- HDU1013 POJ1519 Digital Roots(解法二)
- Maven deploy时报Fatal error compiling: tools.jar not found错误的问题处理
- linux socket常用配置
- Python新手学习基础之数据类型——字符串类型
- Spring中的Aware
- 如何做好一个Sprint Demo
- [心得感想]16.7.14-21 DIARY
- 神级代码编辑软件(Sublime Text 3) v3.3114 汉化特别版
- Django中实现文件下载功能
- JavaScript踩坑小计——变量的作用域和类型声明
- java异常机制
- sysconf、pathconf和fpathconf函数
- poj 3254