SpringAware
哈哈,终于把分布式的课程演讲给混过去了,下面开始随便自己学点东西。
正题:SpringAware--------在实际项目中,用到spring容器的本省功能资源,这是Bean必须意识到Spring容器的存在,才能调用Spring容器所提供的资源,这就是所谓的Spring Aware.
分两部分,一部分演示书上的列子,一部分用SpringBoot改进,注此列是参照SpringBoot的一本书学习而来,具体不记得了。
一 BeanNameAware--------------获得容器中Bean的名字
BeanFactoryAware------------获得当前容器的BeanFactory,这样可以调用容器的服务
ApplicationContextAware-----获得当前容器的ApplicationContext,这样可以调用容器的服务
ApplicationEventPublisherAware----应用时间发布器,可以发布事件
ResourceLoaderAware-------获得资源加载器,可以获得外部文件
1 继承两个接口并分别重写,BeanNameAware,ResourceLoaderAware这样就分别从容器总拿到了Bean和资源加载器。
package org.sselab.service; 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; import java.io.IOException; /**
* Created by pinker on 2016/11/8.
*/
@Service
public class AwareService implements BeanNameAware, ResourceLoaderAware {
private String name;
private ResourceLoader loader; @Override
public void setBeanName(String s) {
this.name = s;
} @Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.loader = resourceLoader;
} public void outputResult() {
System.out.println("Bean的名称是:" + name); Resource resource =
loader.getResource("classpath:test.txt");
try {
System.out.println(IOUtils.toString(resource.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
2 配置类
package org.sselab.conf; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* Created by pinker on 2016/11/8.
*/
@Configuration
@ComponentScan("org.sselab")
public class AwareConfig { }
3 运行主类
package org.sselab; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.sselab.conf.AwareConfig;
import org.sselab.service.AwareService; /**
* Created by pinker on 2016/11/8.
*/
public class Application { public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class); AwareService service = context.getBean(AwareService.class);
service.outputResult();
context.close();
}
} 分析一下,2是个配置类,肯定可以用SpringBootApplication来代替,这里我们伪造一个访问资源,看看能不能输出。
public String getContext(){
Resource resource =loader.getResource("classpath:test.txt");
String result= null;
try {
result = IOUtils.toString(resource.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
然后构造访问的controller和主启动类OK了,完成了!
实验结果如果所想,其实就是因为少了配置和采用了自动注入,所以没有本质的区别。
SpringAware的更多相关文章
- JUnit4的使用2
package com.imooc.test.aware; import org.junit.Test; import org.junit.runner.RunWith; import org.jun ...
- Spring课程 Spring入门篇 3-3 Spring bean装配(上)之aware接口
课程链接: 本节主要介绍了以下内容: 1 aware介绍 2 代码演练 3 课程总结 1 aware介绍 1.1 为什么要使用aware? 在java类中,可以方便的获取xml配置文件中的bean的各 ...
- Spring Bean Life Cycle Methods – InitializingBean, DisposableBean, @PostConstruct, @PreDestroy and *Aware interfaces
Spring Beans are the most important part of any Spring application. Spring ApplicationContext is res ...
- spring学习六----------Bean的配置之Aware接口
© 版权声明:本文为博主原创文章,转载请注明出处 Aware Spring提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化后,可以获取相应的资源 通过Aware接口,可以对S ...
- 阿里P7工作总结:Spring MVC的工作原理,看完受益匪浅
这篇文章将深入探讨Spring框架的一部分——Spring Web MVC的强大功能及其内部工作原理. 项目安装 在本文中,我们将使用最新.最好的Spring Framework 5.我们将重点介绍S ...
随机推荐
- Simple Validation in WPF
A very simple example of displaying validation error next to controls in WPF Introduction This is a ...
- java自动化测试-http请求get
首先我10.1过来自己玩通了讨鬼转极,看了电视剧白夜追凶,换了工作小组,这段时间确实比较少的更新博客,确实有点不勤奋,我先自我检讨 我就不赘述java的安装了,这个是比较简单的,有必要的话以后在讲 对 ...
- C++类中静态变量和静态方法使用介绍
静态成员的提出是为了解决数据共享的问题.实现共享有许多方法,如:设置全局性的变量或对象是一种方法.但是,全局变量或对象是有局限性的.这一章里,我们主要讲述类的静态成员来实现数据的共享. 静态数据成员 ...
- IDoc 基础知识
Application Link Enabling ALE主要为了分布式业务系统而设计的.它可以使业务流程中的每个步骤分布在不同的SAP系统上,系统间可以通过IDoc交互数据.IDoc可以认为是个信封 ...
- Linux入门(4)——Ubuntu16.04安装MATLAB2016b
通常有三个安装文件: Matlab 2016b Linux64 Crack.rar R2016b_glnxa64_dvd1.iso R2016b_glnxa64_dvd2.iso sudo apt i ...
- 第一篇bolg
仅以此篇谨记自己,以后加油
- 【NOIP2015资源+题解】
数据下载(含cena配置文件+一套自己写的代码) 试题下载(pdf版) Day1 T1 Day1 T2 Day1 T3 Day2 T1 Day2 T2 Day3 T3
- 23种设计模式JAVA 实现目录总结
曾看了不少的有关设计模式的文章,有的提供的实现在现在看来是有些问题,所以现在对以前看过的有关设计模式的文章在这里总结一下,随笔中有引用其他资料,并根据自己的理解重新实现了一次,23种设计模式中,并没有 ...
- 网络库Alamofire使用方法学习笔记
Github地址 由于Alamofire是swift网络库,所以,以下的所有介绍均基于swift项目 导入Alamofire 以下为使用cocoapods导入,其余的方式请参考官网 source 'h ...
- .6-Vue源码之AST(2)
上一节获取到了DOM树的字符串,准备进入compile阶段: // Line-9326 function compileToFunctions(template,options,vm) { // 获取 ...