Spring中的Aware

日期: 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 {

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		//这里加载的Bean是在配置文件applicationContext_Aware.xml里面进行配置的Bean
		System.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>@Test
	public void testApplicationContextAware()
	{
		//加载Spring配置文件
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_Aware.xml");
	}
	


4、运行的结果如下:

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 hierarchy
2016/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 hierarchy
2016/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 {

	@Override
	public 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:

@Test
	public 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 hierarchy
2016/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 hierarchy
2016/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秒


正确的获取了Bean的name: 
BeanName: beanName















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值