Spring-Aware

    技术2022-08-06  113

    Aware的作用

    我们在实际的开发中,我们却经常要用到Spring容器本身的功能资源,所以Spring容器中的Bean此时就要意识到Spring容器的存在才能调用Spring所提供的资源。

    Aware是一个具有标识作用的超级接口,实现该接口的bean是具有被spring 容器通知的能力的,而被通知的方式就是通过回调。也就是说:直接或间接实现了这个接口的类,都具有被spring容器通知的能力。

    Spring的依赖注入的最大亮点是所有的Bean对Spring容器的存在是没有意识的,我们可以将Spring容器换成其他的容器,Spring容器中的Bean的耦合度因此也是极低的。

     

    常见的Aware子类接口

    private ApplicationContext applicationContext;//spring容器核心上下文 private BeanFactory beanFactory;//IOC容器 private String s;//当前bean在IOC容器中的Bean的实例的名字 private ApplicationEventPublisher applicationEventPublisher;//在bean中可以得到应用上下文的事件发布器 private MessageSource messageSource;//在Bean中可以得到消息源 private ResourceLoader resourceLoader;//在Bean中可以得到ResourceLoader,从而加载外部对应的Resource资源

     

    一个小栗子

    github地址:https://github.com/a422478514/java-practice/tree/master/src/main/java/com/daquan/_202007/_01/spring/aware

    package com.daquan._202007._01.spring.aware; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.*; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; /** * 当前bean通过实现多个XxxxxXxxxxAware接口,来注入XxxxxXxxxx的bean实例,待spring容器初始化完成后,变量中的xxxxxXxxxx就有值了,可以直接访问(使用) */ @Service public class TestSpringAware implements ApplicationContextAware, BeanNameAware, BeanFactoryAware, MessageSourceAware, ApplicationEventPublisherAware, ResourceLoaderAware { private ApplicationContext applicationContext;//spring容器核心上下文 private BeanFactory beanFactory;//IOC容器 private String s;//当前bean在IOC容器中的Bean的实例的名字 private ApplicationEventPublisher applicationEventPublisher;//在bean中可以得到应用上下文的事件发布器 private MessageSource messageSource;//在Bean中可以得到消息源 private ResourceLoader resourceLoader;//在Bean中可以得到ResourceLoader,从而加载外部对应的Resource资源 //在Bean中得到Bean所在的应用上下文 @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } //在Bean中得到Bean所在的IOC容器 @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } //在Bean中得到它在IOC容器中的Bean的实例的名字 @Override public void setBeanName(String s) { this.s = s; } //在bean中可以得到应用上下文的事件发布器 @Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } //在Bean中可以得到消息源 @Override public void setMessageSource(MessageSource messageSource) { this.messageSource = messageSource; } //在Bean中可以得到ResourceLoader,从而加载外部对应的Resource资源。 @Override public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public ApplicationContext getApplicationContext() { return applicationContext; } public BeanFactory getBeanFactory() { return beanFactory; } public String getS() { return s; } public void setS(String s) { this.s = s; } public ApplicationEventPublisher getApplicationEventPublisher() { return applicationEventPublisher; } public MessageSource getMessageSource() { return messageSource; } public ResourceLoader getResourceLoader() { return resourceLoader; } }

    application.xml内容如下:

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config /> <!--自动扫描含有@Service将其注入为bean --> <context:component-scan base-package="com.daquan._202007" /> </beans>

    启动类:

    package com.daquan._202007._01.spring.aware; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.MessageSource; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ResourceLoader; public class TestSpringAwareMain { public static void main(String[] args) { System.out.println("加载spring容器"); ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml"); TestSpringAware testSpringAware = (TestSpringAware)classPathXmlApplicationContext.getBean("testSpringAware"); ApplicationContext applicationContext = testSpringAware.getApplicationContext(); System.out.println(applicationContext.getId()); BeanFactory beanFactory = testSpringAware.getBeanFactory(); System.out.println(beanFactory.containsBean("testSpringAware")); String s = testSpringAware.getS(); System.out.println(s); ApplicationEventPublisher applicationEventPublisher = testSpringAware.getApplicationEventPublisher(); applicationEventPublisher.publishEvent(new ApplicationEvent(new String("hello")) { @Override public Object getSource() { return super.getSource(); } }); MessageSource messageSource = testSpringAware.getMessageSource(); System.out.println(messageSource.toString()); ResourceLoader resourceLoader = testSpringAware.getResourceLoader(); System.out.println(resourceLoader.getClass()); } }

     

     

    Processed: 0.015, SQL: 9