【SpringBoot基础】Aware框架 Bean获取资源调度
2019-04-04 16:31
821 查看
【SpringBoot基础】Aware框架 Bean获取资源调度
内容概要
Aware框架 :
原:bean注册到容器中,容器调用服务进行整合。
Aware: 使Bean获得容器的相关功能。
共6个接口
BeanNameAware : 获取容器中 Bean的名称
ResourceLoaderAware: 获取资源加载器,读取外部资源
BeanFactoryAware : 获取当前bean factory
MessageSourceAware: 获取messagesource ,可获取文本信息
ApplicationEventPublisherAware : 获得事件发布功能
ApplicationContextAware: 获取容器中的所有资源
文件结构
test.txt 内容
调用Bean类 AwareService
package com.Aware; import org.apache.commons.io.IOUtils; 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; /*Aware框架 :原:bean注册到容器中,容器调用服务整合。 Aware: 使Bean获得容器的相关功能。 BeanNameAware : 获取容器中 Bean的名称 ResourceLoaderAware: 获取资源加载器,读取外部资源 BeanFactoryAware : 获取当前bean factory MessageSourceAware: 获取messagesource ,可获取文本信息 ApplicationEventPublisherAware : 获得事件发布功能 ApplicationContextAware: 获取容器中的所有资源 */ @Service public class AwareService implements BeanNameAware,ResourceLoaderAware { private String name; //bean 名称 private ResourceLoader loader; //加载器 public void setBeanName(String name) { this.name = name; } public void setResourceLoader(ResourceLoader resourceLoader) { this.loader = resourceLoader; } public void sayResult(){ System.out.println(name); //加载文件 Resource resource = loader.getResource("txt/test.txt"); try { System.out.println("Resource 加载内容:"+IOUtils.toString(resource.getInputStream())); }catch (Exception e){ System.out.println(e.getMessage()); } } }
配置类 AwareConfig
package com.Aware; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("com.Aware") public class AwareConfig { }
测试类 Main
package com.Aware; import org.spr 20000 ingframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[]args){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class); AwareService bean = context.getBean(AwareService.class); bean.sayResult(); context.close(); } }
输出显示
相关文章推荐
- 【spring Boot】spring boot获取资源文件的三种方式【两种情况下】
- Spring boot 获取resources 静态资源 jar包于linux服务器上运行
- spring不通过注入的方式获取对象(BeanFactoryAware \ApplicationContextAware )
- springboot 普通类获取spring IOC容器中的Bean<七>
- spring bean装配(bean的作用域、bean的生命周期、Aware接口、Bean的自动装配Autowiring、资源文件Resources )
- Spring - 运行时获取bean(ApplicationContextAware接口)
- springboot学习笔记一: service层获取调用dao层查询接口,获取到的bean为空,但是用sql语句去查数据库却有数据
- Spring Boot2.x-04Spring Boot基础-使用注解装配bean
- S2SH框架中,Spring通过注解获取bean实例
- spring接口 BeanFactoryAware,动态获取IOC容器里面的对象(多例)
- 通过ApplicationContextAware获取spring容器中的bean
- Spring ApplicationContextAware 获取bean
- SpringBoot普通类中如何获取其他bean例如Service、Dao(转)
- 通过ApplicationContextAware获取spring容器中的bean
- 实现ApplicationContextAware接口,java(new或者java反射获取的对象)中获取spring容器的bean
- Springboot 获取本地资源的方法
- springboot jar 方式获取 资源图片 文字等
- 【SpringBoot基础】Condition框架 条件注解使用
- spring 框架通过new Object()获取applicationContext 中的bean方案
- spring boot 基础web项目框架说明