spring boot: spring Aware的目的是为了让Bean获得Spring容器的服务
2017-12-28 16:09
519 查看
Spring Aware的目的是为了让Bean获得Spring容器的服务
//获取容器中的bean名称
import org.springframework.beans.factory.BeanNameAware;
//获得资源加载器,可以获得额外的资源
import org.springframework.context.ResourceLoaderAware;
配置:
运行:
//获取容器中的bean名称
import org.springframework.beans.factory.BeanNameAware;
//获得资源加载器,可以获得额外的资源
import org.springframework.context.ResourceLoaderAware;
package ch2.aware; import java.io.IOException; import org.apache.commons.io.IOUtils; //获取容器中的bean名称 import org.springframework.beans.factory.BeanNameAware; //获得资源加载器,可以获得额外的资源 import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; //AwareService是spring的一个组件 @Service //实现BeanNameAware,ResourceLoaderAware资源的接口,获得名称和资源加载的服务 public class AwareService implements BeanNameAware,ResourceLoaderAware { private String beanName; private ResourceLoader loader; //实现ResourceLoaderAware需要重写setResourceLoader @Override public void setResourceLoader(ResourceLoader resourceLoader) { // TODO Auto-generated method stub this.loader = resourceLoader; } //实现BeanNameAware需要重写setBeanName @Override public void setBeanName(String name) { // TODO Auto-generated method stub this.beanName = name; } public void outputResult() { System.out.println("bean的名字为:"+ beanName); Resource resource = loader.getResource("classpath:ch2/aware/test.txt"); try { System.out.println("ResourceLoader加载的内容为:" + IOUtils.toString(resource.getInputStream())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
配置:
package ch2.aware; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; //声明本类是一个配置类 @Configuration //自动加载ch2.aware包下面的内容 @ComponentScan("ch2.aware") public class AwareConfig { }
运行:
package ch2.aware; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(AwareConfig.class); AwareService awareService = context.getBean(AwareService.class); awareService.outputResult(); context.close(); } }
相关文章推荐
- Spring通过ApplicationContextAware获得bean的方法、获取数据库连接的方法、获取Appl...
- Spring进阶之路(3)-bean获得Spring的容器
- web后台守护线程如何获得spring容器中的bean
- Spring 实现容器销毁该bean的时候获得一次回调
- 如何通过代码直接获得Spring容器中的Bean?
- spring接口 BeanFactoryAware,动态获取IOC容器里面的对象(多例)
- web后台守护线程如何获得spring容器中的bean
- spring源码分析:Aware接口让你的bean对容器直接操作
- 通过ApplicationContextAware获取spring容器中的bean
- 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
- spring容器启动就获得实现指定接口的beanMap
- Spring进阶之路(3)-bean获得Spring的容器
- 通过ApplicationContextAware获取spring容器中的bean
- 如何获得Spring容器里管理的Bean,。不论是Service层,还是实体Dao层
- spring,springMVC的优点和区别 spring 是是一个开源框架,是为了解决企业应用程序开发,功能如下 ◆目的:解决企业应用开发的复杂性 ◆功能:使用基本的JavaBean代替EJB,并
- web后台守护线程如何获得spring容器中的bean
- 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
- 如何从当前spring容器中获得bean?
- 7 -- Spring的基本用法 -- 8... 抽象Bean与子Bean;Bean继承与Java继承的区别;容器中的工厂Bean;获得Bean本身的id;强制初始化Bean
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)