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 ...
随机推荐
- 原生API实现拖拽上传文件实践
功能: 拖拽上传文件.图片,上传的进度条,能够同时上传多个文件. 完整的demo地址:https://github.com/qcer/FE-Components/tree/master/QDrag 涉 ...
- angularJS的一点小笔记
AngularJS用法:-------------------------------------------------- AngularJS 指令: AngularJS 通过被称为 指令 的新属性 ...
- ASP.NET MVC 分页问题
在使用Ajax.Pager进行分页的时候需要注意一下几个方面: 1.一定要引入jquery.unobtrusive-ajax.min.js这个js: 2.一定要在页面中使用注册分页器,注册方法:@{H ...
- Xcode9 FFmpeg冲突问题
升级Xcode9之后,工程中FFmpeg中的avutil.h下的AVMediaType与系统的AVFoundation框架冲突了. 报错信息:Typedef 'AVMediaType' cannot ...
- 微信小程序点击返回顶层实现方法
最近在研究微信小程序,被这个返回顶层给坑了一波,下面贴代码 wxml代码: <scroll-view scroll-y style="height: 1000rpx;" sc ...
- Spring Bean装配方式
Spring装配机制 在xml中进行显示配置 在Java中进行显示配置 隐式bean发现机制和自动装配 自动化装配bean 组件扫描(component scanning),Spring会自动发现应用 ...
- JAVA提高十:ArrayList 深入分析
前面一章节,我们介绍了集合的类图,那么本节将学习Collection 接口中最常用的子类ArrayList类,本章分为下面几部分讲解(说明本章采用的JDK1.6源码进行分析,因为个人认为虽然JDK1. ...
- ASP.NET中登录时记住用户名和密码(附源码下载)--ASP.NET
必需了解的:实例需要做的是Cookie对象的创建和对Cookie对象数据的读取,通过Response对象的Cookies属性创建Cookie,通过Request对象的Cookies可以读取Cookie ...
- robotframework自动化:登陆操作
robotframework自动化系统:登录 robotframework对于编程能力比较弱的测试人员而言,真的是雪中送炭!我们可以使用robotframework根据之前完成的测试用例,一步步完善自 ...
- 利用win10自带的系统配置禁止开机启动项和程序
一.利用win10自带的系统配置禁止开机启动项和程序 首先打开"运行"对话框,可以通过开始菜单打开运行,也可以按下快捷键WIN+R打开"运行".如下图. ...