Spring Bean装配·Aware·Resource

简介: 作用:实现了Aware接口的bean在初始化后可以获取相应资源并进行相应的操作举例:ApplicationContextAware:向实现了该接口的bean提供IOC容器的上下文信息(ApplicationContext),实现了该接口的bean必须配置到配置文件中并由spring的be...
作用:实现了Aware接口的bean在初始化后可以获取相应资源并进行相应的操作
举例:
ApplicationContextAware:向实现了该接口的bean提供IOC容器的上下文信息(ApplicationContext),实现了该接口的bean必须配置到配置文件中并由spring的bean容器加载
BeanNameAware:向实现了该接口的bean提供关于beanName定义的相关内容
其他就做不介绍了,官网文档内容如下:
 

Table 7.4. Aware interfaces

Name Injected Dependency Explained in…​

ApplicationContextAware

Declaring ApplicationContext

Section 7.6.2, “ApplicationContextAware and BeanNameAware”

ApplicationEventPublisherAware

Event publisher of the enclosing ApplicationContext

Section 7.15, “Additional Capabilities of the ApplicationContext”

BeanClassLoaderAware

Class loader used to load the bean classes.

Section 7.3.2, “Instantiating beans”

BeanFactoryAware

Declaring BeanFactory

Section 7.6.2, “ApplicationContextAware and BeanNameAware”

BeanNameAware

Name of the declaring bean

Section 7.6.2, “ApplicationContextAware and BeanNameAware”

BootstrapContextAware

Resource adapter BootstrapContext the container runs in. Typically available only in JCA aware ApplicationContexts

Chapter 32, JCA CCI

LoadTimeWeaverAware

Defined weaver for processing class definition at load time

Section 11.8.4, “Load-time weaving with AspectJ in the Spring Framework”

MessageSourceAware

Configured strategy for resolving messages (with support for parametrization and internationalization)

Section 7.15, “Additional Capabilities of the ApplicationContext”

NotificationPublisherAware

Spring JMX notification publisher

Section 31.7, “Notifications”

PortletConfigAware

Current PortletConfig the container runs in. Valid only in a web-aware Spring ApplicationContext

Chapter 25, Portlet MVC Framework

PortletContextAware

Current PortletContext the container runs in. Valid only in a web-aware Spring ApplicationContext

Chapter 25, Portlet MVC Framework

ResourceLoaderAware

Configured loader for low-level access to resources

Chapter 8, Resources

ServletConfigAware

Current ServletConfig the container runs in. Valid only in a web-aware Spring ApplicationContext

Chapter 22, Web MVC framework

ServletContextAware

Current ServletContext the container runs in. Valid only in a web-aware Spring ApplicationContext

Chapter 22, Web MVC framework

Resource
 
Resources (针对于资源文件的统一接口)

A、UrlResource:URL 对应的资源,根据一个 URL 地址即可获取
B、ClassPathResource:获取类路径下的资源
C、FileSystemResource:获取文件系统里面的资源
D、ServletContextResource:ServletContext 封装的资源,用于访问 ServletContext 环境下的资源
E、InputStreamResource:获取输入流封装的资源
F、ByteArrayResource:获取字节数组封装的资源

通过实现 ApplicationContextAware 接口中的方法 ,因为需要实现ResourceLoader,而
ApplicationContextAware 实现了ResourceLoader
setApplicationContext(ApplicationContext applicationContext)Resource resource = applicationContext.getResource("xxx");
具体演示示例见github 
项目地址:https://github.com/DaleyChao/SpringPractice/commit/e84bffe0d2c72fa853d9bb1ffc4d4186e5de0e10
相关文章
|
1月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
163 26
|
3月前
|
XML 安全 Java
|
3月前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
101 12
|
3月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
|
3月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
93 6
|
3月前
|
XML Java 数据格式
🌱 深入Spring的心脏:Bean配置的艺术与实践 🌟
本文深入探讨了Spring框架中Bean配置的奥秘,从基本概念到XML配置文件的使用,再到静态工厂方式实例化Bean的详细步骤,通过实际代码示例帮助读者更好地理解和应用Spring的Bean配置。希望对你的Spring开发之旅有所助益。
246 4
|
4月前
|
缓存 Java Spring
实战指南:四种调整 Spring Bean 初始化顺序的方案
本文探讨了如何调整 Spring Boot 中 Bean 的初始化顺序,以满足业务需求。文章通过四种方案进行了详细分析: 1. **方案一 (@Order)**:通过 `@Order` 注解设置 Bean 的初始化顺序,但发现 `@PostConstruct` 会影响顺序。 2. **方案二 (SmartInitializingSingleton)**:在所有单例 Bean 初始化后执行额外的初始化工作,但无法精确控制特定 Bean 的顺序。 3. **方案三 (@DependsOn)**:通过 `@DependsOn` 注解指定 Bean 之间的依赖关系,成功实现顺序控制,但耦合性较高。
224 4
实战指南:四种调整 Spring Bean 初始化顺序的方案
|
3月前
|
安全 Java 开发者
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
64 1
|
4月前
|
消息中间件 Java 数据库
解密Spring Boot:深入理解条件装配与条件注解
Spring Boot中的条件装配与条件注解提供了强大的工具,使得应用程序可以根据不同的条件动态装配Bean,从而实现灵活的配置和管理。通过合理使用这些条件注解,开发者可以根据实际需求动态调整应用的行为,提升代码的可维护性和可扩展性。希望本文能够帮助你深入理解Spring Boot中的条件装配与条件注解,在实际开发中更好地应用这些功能。
84 2
|
5月前
|
XML Java 数据格式
Spring从入门到入土(bean的一些子标签及注解的使用)
本文详细介绍了Spring框架中Bean的创建和使用,包括使用XML配置文件中的标签和注解来创建和管理Bean,以及如何通过构造器、Setter方法和属性注入来配置Bean。
120 9
Spring从入门到入土(bean的一些子标签及注解的使用)

热门文章

最新文章