Aware结尾的接口用途——向容器获取资源。
对两个接口举例:BeanNameAware,ApplicationContextAware。
首先对ApplicationContextAware接口的实现做一个实例:
//1.类
package com.imooc.aware;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class MoocApplicationContext implements ApplicationContextAware {
//重写接口方法
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
//这里就可以从容器内获取自身的bean。
System.out.println("MoocApplicationContext : " + applicationContext.getBean("moocApplicationContext"));
}
}
//2.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="moocApplicationContext" class="com.imooc.aware.MoocApplicationContext" ></bean>
</beans>
//3.测试
package com.imooc.test.aware;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
//import com.imooc.aware.MoocApplicationContext;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
public TestAware() {
super("classpath:spring-aware.xml");
}
@Test
public void testMoocApplicationContext() {
System.out.println("testMoocApplicationContext : " + super.getBean("moocApplicationContext"));
}
}
//4,结果
8月 02, 2018 1:11:55 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 13:11:55 CST 2018]; root of context hierarchy
8月 02, 2018 1:11:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aware.xml]
MoocApplicationContext : 252277567
testMoocApplicationContext : 252277567
8月 02, 2018 1:11:55 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 13:11:55 CST 2018]; root of context hierarchy
可以看到在首先是setApplicationContext()方法的输出,然后是测试类方法的输出,其中两个返回都是xml配置的容器中相同的bean对象,说明这个操作,可以让类获取容器中的bean对象资源(这块确实不太懂)。
下面是第二个实例,实现BeanNameAware接口,
//1.类
package com.imooc.aware;
import org.springframework.beans.factory.BeanNameAware;
public class MoocBeanName implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println("MoocBeanName : " + name);
}
}
这个是读取容器里bean的name的作用,所以不用getBean。
//2.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="moocBeanName" class="com.imooc.aware.MoocBeanName" ></bean>
</beans>
//3.测试类
package com.imooc.test.aware;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
public TestAware() {
super("classpath:spring-aware.xml");
}
@Test
public void textMoocBeanName() {
System.out.println("textMoocBeanName : " + super.getBean("moocBeanName"));
}
}
//4.结果
8月 02, 2018 1:23:12 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 13:23:12 CST 2018]; root of context hierarchy
8月 02, 2018 1:23:12 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aware.xml]
MoocBeanName : moocBeanName
textMoocBeanName : com.imooc.aware.MoocBeanName@4145bad8
8月 02, 2018 1:23:12 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Thu Aug 02 13:23:12 CST 2018]; root of context hierarchy
看到在类中获取了容器内bean的name,在测试类里面获得了相应的bean。
这块内容有点乱,还需要后面继续加强学习。