2016/1/16 12:45:20
1.Aware
- spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以获取相应资源
- 通过Aware接口,可以对Spring相应资源进行操作(一定要慎重)
- 为对Spring进行简单的扩展提供了方便的入口
XML:spring-aware.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="applicationContext" class="com.zjx.aware.ApplicationContext"></bean>
</beans>
bean类中:
package com.zjx.aware;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContext implements ApplicationContextAware{
@Override
public void setApplicationContext(
org.springframework.context.ApplicationContext arg0)
throws BeansException {
System.out.println("ApplicationContext:"+arg0.getBean("applicationContext").hashCode());
}
}
测试类:
package com.zjx.interfaces.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
public TestAware() {
super("classpath*:spring-aware.xml");
}
@Test
public void test() {
System.out.println("test方法中的ApplicationContext:"
+ super.getBean("applicationContext").hashCode());
}
}
结果打印出的两个类的哈希值一致,这说明实现了XXXAware接口,得到了输出
对于BeanName实现BeanNameAware,ApplicationContextAware接口的方式
XML:spring-aware.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="beanName" class="com.zjx.aware.BeanName"></bean>
</beans>
bean类
package com.zjx.aware;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class BeanName implements BeanNameAware,ApplicationContextAware{
private String beanName;
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("setApplicationContext中的实例哈希值:"+arg0.getBean(this.beanName).hashCode());
}
@Override
public void setBeanName(String arg0) {
this.beanName = arg0;
System.out.println("BeanName:"+arg0);
}
}
测试
package com.zjx.interfaces.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
public TestAware() {
super("classpath*:spring-aware.xml");
}
@Test
public void test() {
System.out.println("test方法中的实例哈希值:"
+ super.getBean("beanName").hashCode());
}
}
不管是通过test基类获取的applicationContext还是通过XXXaware接口获取的applicationContext的哈希值一致,说明两种方式都可以访问到容器上下文对象