Spring 装配Aware 接口和Resource

Spring 装配Aware接口

aware,翻译过来是知道的,已感知的,意识到的,所以这些接口从字面意思应该是能感知到所有Aware前面的含义。
实现BeanNameAware接口,可以让该Bean感知到自身的BeanName(对应Spring容器的BeanId属性)属性,同理,其他的Aware接口也是为了能够感知到自身的一些属性。
比如实现了ApplicationContextAware接口的类,能够获取到ApplicationContext,实现了BeanFactoryAware接口的类,能够获取到BeanFactory对象。
  1. .Spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在初始化之后,可以获取相应的资源
  2. 通过Aware接口,可以对Spring相应资源进行操作(慎重)
  3. 为对Spring进行简单的扩展提供了方便的入口
  4. 接下来我们将用ApplicationContextAware 和BeanNameAware接口举例

 示例代码:定义类MoocApplicationContext 实现 ApplicationContextAware 接口

package com.imooc.aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/*
 * 实现接口 ApplicationContextAware
 */
public class MoocApplicationContext implements ApplicationContextAware {

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// 加载的了这个Bean的IOC容器上下文信息
		System.out.println("MoocApplicationContext:" + applicationContext.getBean("moocApplicationContext"));
	}

}

配置文件

<?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"
	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 ">
    
    <bean id="moocApplicationContext" class="com.imooc.aware.MoocApplicationContext"></bean>
</beans>

测试方法

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAware {

	private static ApplicationContext context;
	@Test
	public void testMoocApplicationContext(){
	
		context = new ClassPathXmlApplicationContext(
			 "spring-aware.xml"); 
		
	}
}

  初始化容器的时候调用 setApplicationContext()方法 得到了当前的Bean----moocApplicationContext,当然了 在测试类中 也可以使用getBean()方法得到当前的Bean。大家不信的话可以自行测试,两个 Bean 的 hashCode 值是一样的。以上我们成功的将 当前的ApplicationContext 写进我们的类中。
示例代码:定义类 MoocBeanName 实现 BeanNameAware接口

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,并把beanName  赋值给成员变量, 在另外一个Bean里面 通过
 * getBean()方法 得到 由setBeanName()方法 传入 的实例
 */
public class MoocBeanName implements BeanNameAware,ApplicationContextAware{

	private String beanName;
	@Override
	public void setBeanName(String name) {
		this.beanName = name;
		System.out.println("MoocBeanName:" + name);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("setApplicationContext:" + applicationContext.getBean(this.beanName).hashCode());
	}
}

测试

  深度解析:BeanNameAware接口帮助Java Bean知道(意识到)自己在配置文件中的 id,实现BeanNameAware接口,实现方法为setBeanName()方法,初始化该对象后Spring会执行回调方法,将 id 设置进来。SetBeanName方法的回调发生在所有参数被设置完之后,初始化(init-method)被执行之前。

Bean自动装配之Resource

JDK 提供的访问资源的类( File 等)不能很好满足各种某些资源的访问需求。比如缺少从类路径和 Web 容器的上下文中获取资源的资源操作类。Spring 的 Resource 接口提供了更好用的资源访问能力。Spring 使用 Resource 访问各种资源文件,配置文件资源,国际化属性资源等。

针对于资源文件的统一接口

 ResourceLoader 资源加载的一个类
所有的 Application context 都实现了这个接口,并且它们都可以获取Resource 实例

public interface ResourceLoader {
	Resource getResource(String location);
}
Resource template = ctx.getResource("文件位置");
Resource template = ctx.getResource("classpath:some/myTemplate.txt");
Resource template = ctx.getResource("file:/path/myTemplate.txt");

 定义Resource类

package com.imooc.resource;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

public class MoocResource implements ApplicationContextAware {

	private ApplicationContext applicationContext;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	public void resource() throws IOException {  // 配置文件在本地Resource中
		 Resource resource = applicationContext.getResource("classpath:config.txt");
		 System.out.println("文件名:" + resource.getFilename());
		 System.out.println("文件长度:" + resource.contentLength());
	}
	
}

配置文件

<?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" 
    default-autowire="constructor" >

	<bean id="moocResource" class="com.imooc.resource.MoocResource"></bean>

</beans>

测试类

package com.imooc.test.resource;

import java.io.IOException;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.imooc.resource.MoocResource;

public class TestResource {
	private static ApplicationContext context;

	@Test
	public void testResource() throws IOException {

		context = new ClassPathXmlApplicationContext("spring-resource.xml");
		MoocResource moocResource = (MoocResource) context.getBean("moocResource");
		moocResource.resource();
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值