spring学习六----------Bean的配置之Aware接口
© 版权声明:本文为博主原创文章,转载请注明出处
Aware
Spring提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化后,可以获取相应的资源
通过Aware接口,可以对Spring相应资源进行操作
为Spring进行简单的扩展提供了方便的入口
本博客只对ApplicationContextAware和BeanNameAware进行测试
1.项目结构
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springbean</groupId> <artifactId>SpringBean</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringBean Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <!-- 版本统一 --> <spring.version>4.3.7.RELEASE</spring.version> </properties> <dependencies> <!-- junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>SpringBean</finalName> </build> </project>
3.TestBase.java
package org.spring.aware.test; import org.junit.After; import org.junit.Before; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StringUtils; /** * 单元测试基类 * */ public class TestBase { private ClassPathXmlApplicationContext context; private String springXmlPath; /** * 无参构造器,若无此构造器则必须传入spring配置文件的路径,有此构造器可以设置默认值 */ public TestBase() { } /** * 含参构造器,初始化spring配置文件的位置 * * @param springXmlPath */ public TestBase(String springXmlPath) { this.springXmlPath = springXmlPath; } /** * 初始化加载spring配置文件,在@Test注解的方法执行之前执行 */ @Before public void begin() { if(StringUtils.isEmpty(springXmlPath)) {//使用默认的spring配置文件 springXmlPath = "classpath:spring-*.xml"; } //加载配置文件至spring容器中 context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+")); //启动容器并将启动信号扩散至该容器中的所有组件 context.start(); } /** * 销毁spring容器,在@Test注解的方法执行之后执行 */ @After public void destroy() { context.destroy(); } /** * 根据beanID获取bean对象 * * @param beanId * beanId * @return */ public Object getBean(String beanId) { return context.getBean(beanId); } }
4.MyApplicationContext.java
package org.spring.aware; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class MyApplicationContext implements ApplicationContextAware { public void setApplicationContext(ApplicationContext context) throws BeansException { System.out.println("MyApplicationContext:" + context.getBean("myApplicationContext")); } }
5.MyBeanName.java
package org.spring.aware; import org.springframework.beans.factory.BeanNameAware; public class MyBeanName implements BeanNameAware { public void setBeanName(String name) { System.out.println("MyBeanName:" + name); } }
6.spring-aware.xml
6.1 继承ApplicationContextAware接口
<?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="myApplicationContext" class="org.spring.aware.MyApplicationContext"/> </beans>
6.2 继承BeanNameAware接口
<?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="myBeanName" class="org.spring.aware.MyBeanName"/> </beans>
7.TestSpringAware.java
package org.spring.aware.test; import org.junit.Test; public class TestSpringAware extends TestBase { /** * 通过构造器传入spring配置文件路径 */ public TestSpringAware() { super("classpath:spring-aware.xml"); } @Test public void testApplicationContext() { System.out.println("testApplicationContext:" + super.getBean("myApplicationContext")); } @Test public void testBeanName() { System.out.println("testBeanName:" + super.getBean("myBeanName")); } }
8.效果预览
8.1 执行testApplicationContext方法
8.2 执行testBeanName方法
同时继承ApplicationContextAware和BeanNameAware两个接口
1.MyAware.java
package org.spring.aware; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class MyAware implements ApplicationContextAware, BeanNameAware { private String name; public void setBeanName(String name) { this.name = name; } public void setApplicationContext(ApplicationContext context) throws BeansException { System.out.println("MyAware:" + context.getBean(name)); } }
2.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myAware" class="org.spring.aware.MyAware"/> </beans>
3.TestSpringAware.java
package org.spring.aware.test; import org.junit.Test; public class TestSpringAware extends TestBase { /** * 通过构造器传入spring配置文件路径 */ public TestSpringAware() { super("classpath:spring-aware.xml"); } @Test public void testApplicationContext() { System.out.println("testApplicationContext:" + super.getBean("myApplicationContext")); } @Test public void testBeanName() { System.out.println("testBeanName:" + super.getBean("myBeanName")); } @Test public void testAware() { System.out.println("testAware:" + super.getBean("myAware")); } }
4.效果预览
4.1 执行testAware方法
© 版权声明:本文为博主原创文章,转载请注明出处