在Bean中获取Spring容器中的各种组件

Spring提供了许多***Aware接口来获取容器中的各种组件

***Aware 都定义了一个set***(XXX xx)方法需要子类bean去实现 。

在Spring初始化bean时 ,会对该bean的类型进行检查,判断是否实现了**Aware接口,如果实现了,就调用它对应的**set方法,并将对应的对象设置进去 。



以ApplicationContextAware为例,它用于获取ApplicationContext对象, 它的定义如下:

public interface ApplicationContextAware {
	
	/** 
	 * Set the ApplicationContext that this object runs in.
	 * Normally this call will be used to initialize the object.
	 * <p>Invoked after population of normal bean properties but before an init callback such
	 * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
	 * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
	 * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
	 * {@link MessageSourceAware}, if applicable.
	 * @param applicationContext the ApplicationContext object to be used by this object
	 * @throws ApplicationContextException in case of context initialization errors
	 * @throws BeansException if thrown by application context methods
	 * @see org.springframework.beans.factory.BeanInitializationException
	 */
	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}



实例:在我创建的Service中获取容器中的ResourceLoader、ApplicationEventPublisher、beanName、BeanFactory、MessageSource、ApplicationContext

package com.spring.service.impl;

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.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;

import com.spring.service.PersonService;

public class PersonServiceImpl implements PersonService, ApplicationContextAware, MessageSourceAware, BeanFactoryAware, BeanNameAware, ApplicationEventPublisherAware, ResourceLoaderAware {

    private String                    name;

    private ResourceLoader            resourceLoader;

    private ApplicationEventPublisher applicationEventPublisher;

    private String                    beanName;

    private BeanFactory               beanFactory;

    private MessageSource             messageSource;

    private ApplicationContext        applicationContext;

    public void sayHello() {
        System.out.println("Hello World!");
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void setBeanName(String name) {
        this.beanName = name;
    }

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

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

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

}


那么在该Service中就可以使用获取的那些对象了, spring在创建该bean的时候就会自动调用这些set方法,将对应的对象注入到我们的bean中。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值