spring boot: spring Aware的目的是为了让Bean获得Spring容器的服务
Spring Aware的目的是为了让Bean获得Spring容器的服务
//获取容器中的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 boot: spring Aware的目的是为了让Bean获得Spring容器的服务的更多相关文章
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...
- 56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】
什么是"异步调用"? "异步调用"对应的是"同步调用",同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执 ...
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...
- (16)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】
在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使用Druid,在上一节我们新建了一个类:DruidConfiguration我在这个类进行编码: package com.k ...
- 曹工说Spring Boot源码(21)-- 为了让大家理解Spring Aop利器ProxyFactory,我已经拼了
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(3)-- 手动注册Bean Definition不比游戏好玩吗,我们来试一下
写在前面的话 相关背景及资源: 曹工说Spring Boot源码系列开讲了(1)-- Bean Definition到底是什么,附spring思维导图分享 工程代码地址 思维导图地址 工程结构图: 大 ...
- spring boot 教程(一) 构建我的第一个Spring boot
Spring Boot特点 1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自动配置Spring 5. 提供生产就绪型功能,如指标, ...
- 从零一起学Spring Boot之LayIM项目长成记(一) 初见 Spring Boot
项目背景 之前写过LayIM的.NET版后端实现,后来又写过一版Java的.当时用的是servlet,websocket和jdbc.虽然时间过去很久了,但是仍有些同学在关注.偶然间我听说了Spring ...
- (D)spring boot使用注解类代替xml配置实例化bean
bean经常需要被实例化,最常见的就是new一个呗,Bean bean = new Bean(),方便好用还快捷. 然而在我们刚开始学习写i项目的时候却发现,new不好用哦,并且也不报错,根本不知道怎 ...
随机推荐
- p:nth-last-child(2)
<!DOCTYPE html><html><head><style> p:nth-last-child(2){background:#ff0000;}& ...
- MFC重绘原理的关键理解
// ====================Windows重绘消息与函数========================== 得到桌面窗口的句柄,然后再绘图HWND GetDesktopWindow ...
- 常用代码块:创建httpclient 2
HttpGet httpGet = new HttpGet(url);SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(ne ...
- 前端基础-html(3)
一.列表标签 1.ul(无序列表)标签 ul(unordered list)无序列表,ul下的子元素只能是li(list item),如下示例: <ul> <li>第一项< ...
- 使用openresty + lua 搭建api 网关(一)安装openresty ,并添加lua模块
openresty 有点不多说,网上各种介绍,先安装吧. 官方操作在此,http://openresty.org/cn/installation.html, tar -xzvf openresty-V ...
- Java基础—抽象类和接口
1.抽象类 在Java语言中使用abstrac关键字来定义抽象类和抽象方法,抽象方法没有定义,方法名后面直接跟一个分号,而不是花括号. public abstract class Employee { ...
- Django进阶项目
本来想上午轻轻松松搞定,没想到还是出了其他的问题,好在最后都解决了 在middleware.py当中 # -*- coding:utf-8 -*- __author__ = 'feimao' impo ...
- Centos7.2安装bacula及bacula-web
serverd端安装(centos7) bacula的安装很简单,但是配置文件内容很多,配置不正确服务就启动不了,所以需要用webmin来配置. 1.安装基础软件包: 关闭SElinux(重要)set ...
- api响应类
接口开发响应类封装 class response{ /* * 封通信接口数据 * @param integer $code 状态码 * @param string $message 状态信息 * @p ...
- 预防SQL注入攻击
/** * 预防SQL注入攻击 * @param string $value * @return string */ function check_input($value) { // 去除斜杠 if ...