spring源码解读:aware接口

package org.springframework.beans.factory;

/**
 * Marker superinterface indicating that a bean is eligible to be
 * notified by the Spring container of a particular framework object
 * through a callback-style method.  Actual method signature is
 * determined by individual subinterfaces, but should typically
 * consist of just one void-returning method that accepts a single
 * argument.
 * <p>Note that merely implementing {@link Aware} provides no default
 * functionality. Rather, processing must be done explicitly, for example
 * in a {@link org.springframework.beans.factory.config.BeanPostProcessor BeanPostProcessor}.
 * Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor}
 * and {@link org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory}
 * for examples of processing {@code *Aware} interface callbacks.
 *
 * @author Chris Beams
 * @since 3.1
 */
public interface Aware {

}

作用是标记超类接口表名改bean 是一个有资格被spring容器通过回调通知的特定框架对象,实际方法签名由单个子接口确定,但通常应为只包含一个接受单个参数的void返回方法。如BeanNameAware;

package org.springframework.beans.factory;

/**
 * Interface to be implemented by beans that want to be aware of their
 * bean name in a bean factory. Note that it is not usually recommended
 * that an object depend on its bean name, as this represents a potentially
 * brittle dependence on external configuration, as well as a possibly
 * unnecessary dependence on a Spring API.
 *
 * <p>For a list of all bean lifecycle methods, see the
 * {@link BeanFactory BeanFactory javadocs}.
 *
 * @author Juergen Hoeller
 * @author Chris Beams
 * @since 01.11.2003
 * @see BeanClassLoaderAware
 * @see BeanFactoryAware
 * @see InitializingBean
 */
public interface BeanNameAware extends Aware {

	/**
	 * Set the name of the bean in the bean factory that created this bean.
	 * <p>Invoked after population of normal bean properties but before an
	 * init callback such as {@link InitializingBean#afterPropertiesSet()}
	 * or a custom init-method.
	 * @param name the name of the bean in the factory.
	 * Note that this name is the actual bean name used in the factory, which may
	 * differ from the originally specified name: in particular for inner bean
	 * names, the actual bean name might have been made unique through appending
	 * "#..." suffixes. Use the {@link BeanFactoryUtils#originalBeanName(String)}
	 * method to extract the original bean name (without suffix), if desired.
	 */
	void setBeanName(String name);

需要注意的是仅仅实现aware接口是不提供任何默认功能的。相反,处理必须明确地完成,如在一个BeanPostProcessor接口中,可以参考ApplicationContextAwareProcessor和AbstractAutowireCapableBeanFactory,对于接口处理回调的例子。
package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;

/**
 * Factory hook that allows for custom modification of new bean instances,
 * e.g. checking for marker interfaces or wrapping them with proxies.
 *
 * <p>ApplicationContexts can autodetect BeanPostProcessor beans in their
 * bean definitions and apply them to any beans subsequently created.
 * Plain bean factories allow for programmatic registration of post-processors,
 * applying to all beans created through this factory.
 *
 * <p>Typically, post-processors that populate beans via marker interfaces
 * or the like will implement {@link #postProcessBeforeInitialization},
 * while post-processors that wrap beans with proxies will normally
 * implement {@link #postProcessAfterInitialization}.
 *
 * @author Juergen Hoeller
 * @since 10.10.2003
 * @see InstantiationAwareBeanPostProcessor
 * @see DestructionAwareBeanPostProcessor
 * @see ConfigurableBeanFactory#addBeanPostProcessor
 * @see BeanFactoryPostProcessor
 */
public interface BeanPostProcessor {

	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
	 * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code>
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one; if
	 * <code>null</code>, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 */
	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
	 * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code>
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
	 * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
	 * instance and the objects created by the FactoryBean (as of Spring 2.0). The
	 * post-processor can decide whether to apply to either the FactoryBean or created
	 * objects or both through corresponding <code>bean instanceof FactoryBean</code> checks.
	 * <p>This callback will also be invoked after a short-circuiting triggered by a
	 * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
	 * in contrast to all other BeanPostProcessor callbacks.
	 * @param bean the new bean instance
	 * @param beanName the name of the bean
	 * @return the bean instance to use, either the original or a wrapped one; if
	 * <code>null</code>, no subsequent BeanPostProcessors will be invoked
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
	 * @see org.springframework.beans.factory.FactoryBean
	 */
	Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}

其继承关系


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

BeanNameAware,可以在Bean中得到它在IOC容器中的Bean的实例的名字。

BeanFactoryAware,可以在Bean中得到Bean所在的IOC容器,从而直接在Bean中使用IOC容器的服务。

ApplicationContextAware,可以在Bean中得到Bean所在的应用上下文,从而直接在Bean中使用上下文的服务。

MessageSourceAware,在Bean中可以得到消息源。

ApplicationEventPublisherAware,在bean中可以得到应用上下文的事件发布器,从而可以在Bean中发布应用上下文的事件。

ResourceLoaderAware,在Bean中可以得到ResourceLoader,从而在bean中使用ResourceLoader加载外部对应的Resource资源。

在设置Bean的属性之后,调用初始化回调方法之前,Spring会调用aware接口中的setter方法。

下面给出一个例子:

定义一个实现BeanNameAware接口的类:

    package com.spring.aware;  
      
    import org.springframework.beans.factory.BeanNameAware;  
      
    /**  
     * <p>Class:LoggingBean</p>  
     * <p>Description:</p>  
     * @author Liam  
     * @Date [2012-9-7 上午9:23:12]  
     */  
    public class LoggingBean implements BeanNameAware {  
      
        private String name;  
      
        public void setBeanName(String name) {  
            this.name = name;  
        }  
      
        public void run() {  
            System.out.println("容器被感知--BeanNameAware:Bean name is'" + this.name + "'.>>"+name);  
        }  
    }  

在定义个实现BeanFactoryAware接口的类:

    package com.spring.aware;  
      
    import org.springframework.beans.BeansException;  
    import org.springframework.beans.factory.BeanFactory;  
    import org.springframework.beans.factory.BeanFactoryAware;  
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
    import org.springframework.beans.factory.support.DefaultListableBeanFactory;  
      
    /**  
     * <p>Class:ShutdownHookBean</p>  
     * <p>Description:</p>  
     * @author Liam  
     * @Date [2012-9-7 上午9:24:13]  
     */  
    public class BeanDestried implements BeanFactoryAware{  
      
        private ConfigurableListableBeanFactory beanFactory;  
      
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {  
            if (beanFactory instanceof DefaultListableBeanFactory) {  
                this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;  
            }  
        }  
      
        public void run() {  
            if (this.beanFactory != null) {  
                System.out.println("容器被感知--BeanFactory-Destroying singletons.>>"+beanFactory);  
                this.beanFactory.destroySingletons();  
            }  
        }  
      
    }  

在applicationContext中配置好:

    <bean id="logging" class="com.spring.aware.LoggingBean"/>  
    <bean id="beanDestried" class="com.spring.aware.BeanDestried"/>  

测试类如下:

    package com.spring.aware;  
      
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
      
    /**  
     * <p>Class:App</p>  
     * <p>Description:</p>  
     * @author Liam  
     * @Date [2012-9-7 上午9:13:36]  
     */  
    public class App {  
        public static void main(String[] args) throws Exception {  
            ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:beans.xml");  
            LoggingBean lb = (LoggingBean) factory.getBean("logging");  
            lb.run();  
            BeanDestried sd = (BeanDestried)factory.getBean("beanDestried");  
            sd.run();  
        }  
    }  

测试结果如下:

容器被感知--BeanNameAware:Bean name is'logging'.>>logging
容器被感知--BeanFactory-Destroying singletons.>>org.springframework.beans.factory.support.DefaultListableBeanFactory@a6cdf5: 
defining beans [logging,beanDestried]; root of factory hierarchy



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值