Spring实现Aware接口

2017-10-27  本文已影响43人  AmeeLove

介绍

aware接口有以下这些:

service

package com.ghgcn.aware.service;

import org.apache.commons.io.IOUtils;
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.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 实现BeanNameAware,ResourceLoaderAware接口
 */
@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware,BeanFactoryAware ,ApplicationContextAware,MessageSourceAware{

    private String beanName;
    private ResourceLoader resourceLoader;

    private BeanFactory beanFactory;

    private ApplicationContext applicationContext;

    private MessageSource messageSource;
    /**
     * 重写
     * @param name
     */
    @Override
    public void setBeanName(String name) {

        this.beanName=name;

    }

    /**
     * 重写
     * @param resourceLoader
     */
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {

        this.resourceLoader=resourceLoader;
    }


    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory=beanFactory;
    }

    /**
     * 输出
     */
    public void outputResoult(){
        System.out.println("Bean 的名称为  "+beanName);
        System.out.println("beanFactory 的名称为  "+beanFactory.toString());
        System.out.println("applicationContext 的名称为 getApplicationName   "+applicationContext.getApplicationName()+" getDisplayName  "+applicationContext.getDisplayName()+" getId "+applicationContext.getId());
        System.out.println("messageSource 的名称为 messageSource   "+messageSource.getMessage("message", null, "Default", null));
        Resource resource=resourceLoader.getResource("classpath:test.txt");

        try {
            System.out.println("ResourceLoader加载文件内容:  "+ IOUtils.toString(resource.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource=messageSource;
    }
}

配置

package com.ghgcn.aware.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.ghgcn.aware"})
public class AwareConfig {
}

运行

package com.ghgcn.aware.test;

import com.ghgcn.aware.config.AwareConfig;
import com.ghgcn.aware.service.AwareService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AwareTest {

    public  static  void  main(String[] args){

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);

        AwareService awareService = context.getBean(AwareService.class);

        awareService.outputResoult();

        context.close();

    }
}

结果

15:54:13.971 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'awareService'
Bean 的名称为  awareService
beanFactory 的名称为  org.springframework.beans.factory.support.DefaultListableBeanFactory@4313f5bc: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,awareConfig,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,awareService]; root of factory hierarchy
applicationContext 的名称为 getApplicationName    getDisplayName  org.springframework.context.annotation.AnnotationConfigApplicationContext@7791a895 getId org.springframework.context.annotation.AnnotationConfigApplicationContext@7791a895
messageSource 的名称为 messageSource   Default
ResourceLoader加载文件内容:           fsdafsd
         fdsafds
         a111111111

容器管理的Bean一般不需要了解容器的状态和直接使用容器,但是在某些情况下,是需要在Bean中直接对IOC容器进行操作的,这时候,就需要在Bean中设定对容器的感知。Spring IOC容器也提供了该功能,它是通过特定的Aware接口来完成的

完成

只要实现了相关的*Aware接口,Spring就会setter方法自动注入相关属性

上一篇下一篇

猜你喜欢

热点阅读