12.1 Aware
- 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源;
- 使用aware能让我们在应用的任意位置获得spring容器的资源;
- 我们通过实现aware接口来识别spring容器的资源;
- Spring包含的aware有:
- BeanNameAware
- BeanFactoryAware
- ApplicationContextAware
- MessageSourceAware
- ApplicationEventPublisherAware
- ResourceLoaderAware
- 实现ApplicationContextAware接口,可识别所有的资源,但最好是你用到什么就使用什么;
- 这也就意味着我们就耦合到了spring框架上了,没有spring框架你的代码将无法执行;
12.2 示例
12.2.1 新建演示bean
package com.wisely.aware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
@Component
public class DemoBean implements BeanNameAware,ResourceLoaderAware{
private String name;
private ResourceLoader loader;
//BeanNameAware接口的方法
public void setBeanName(String beanName) {
this.name = beanName;
}
//ResourceLoaderAware接口的的方法
public void setResourceLoader(ResourceLoader resourceLoader) {
this.loader = resourceLoader;
}
public String getName() {
return name;
}
public ResourceLoader getLoader() {
return loader;
}
}
12.2.2 新建演示用文件info.txt
jhkljhlkjhlkj
111111111111
12.2.3 测试
package com.wisely.aware;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class Main {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.aware");
DemoBean db = context.getBean(DemoBean.class);
System.out.println(db.getName());
ResourceLoader rl = db.getLoader();
Resource r = rl.getResource("classpath:com/wisely/aware/info.txt");
System.out.println(IOUtils.toString(r.getInputStream()));
context.close();
}
}
输出结果
demoBean
jhkljhlkjhlkj
111111111111
相关推荐
Self-Aware Computing Systems ISBN-10 书号: 3319474723 ISBN-13 书号: 9783319474724 Edition 版本: 1st ed. 2017 出版日期: 2017-01-23 pages 页数: (722 ) $219.99 This book provides formal and informal ...
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
CVPR 2022 Image Dehazing Transformer with Transmission-Aware 3D Position Embedding 源代码,很有学习价值,大佬任文琦团队的最新去雾杰作,从3D定位的角度去思考去雾。CVPR 2022 Image Dehazing Transformer ...
Interest-aware Message-Passing GCN for Recommendat.md
Spring Aware BeanNameAware BeanFactoryAware ApplicationContextAware MessageSourceAware ApplicationEventPublisherAware ResourceLoaderAware 多线程 计划任务 cron fixDelay ...
2007年Shai Avidan 和Ariel Shamir论文《Seam Carving for Content-Aware Image Resizing》所提出方法的Matlab代码实现和作者的论文。 效果非常炫,而且代码不长
一篇2009年pami文章,《Context-aware Visual Tracking》
2019-ICML-YOU-Position-aware Graph Neural Networks-利用邻近锚点集,强化位置描述-rrrrr1
ECCV2020 最新论文 Structure-Aware Human-Action Generation
DAP Detection-Aware Pre-Training With Weak Supervision
Group-Aware Long- and Short-Term Graph Representation Learning for Sequential Group Recommendation
调试好的Relation-Aware-Global-Attention-Networks-master 其中已经包含迁移所需的resnet50和分割好的数据集
Tidal-Traffic-Aware-Routing-_弹性光网络_traffic_源码
Context-Aware Saliency Detection的Matlab代码,可直接运行,图片自己放,
Context-aware composition of mobile service
arXiv预印版论文:Towards QoS-Aware and Resource-Efficient GPU Microservices Based on Spatial Multitasking GPUs In Datacenters。
Since sequential information plays an ...art sequential recommendation methods and context-aware recommendation methods on two public datasets, i.e., the Taobao dataset and the Movielens-1M dataset.
Relation-Aware Pedestrian Attribute Recognition with Graph Convolutional Networks
4.1. Introduction to the Spring IoC container and beans .............................................. 22 4.2. Container overview .........................................................................
NULL 博文链接:https://ylxy3058.iteye.com/blog/2225314