Spring-Aware

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資源

 

一個小栗子

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());

    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章