最近忙于双11的项目,没日没夜的,慌于学业,实在是惭愧,今天好不容易有点时间,看了些源码,有了些新的知识,记录下来,以备以后使用.
今天要介绍的是spring的org.springframework.context包里的两个接口。spring真是强大,不仅为我们提供了基本的脚手架,并且针对特殊的需求,也开放了很多的接口以满足你特殊的需求。今天要介绍的这两位,一个可以让你在任何时候得到当前的容器,另外一位则可以让你在bean初始化完成之后来做一些自己的业务逻辑。
ApplicationContextAware的介绍
/**
* Interface to be implemented by any object that wishes to be notified
* of the {@link ApplicationContext} that it runs in.
* 这个接口可以被任何的Objec实现,如果你希望知道当前类里运行的ApplicationContext
* //be notifeid of 被说明
*
* Implementing this interface makes sense for example when an object
* requires access to a set of collaborating beans. Note that configuration
* via bean references is preferable to implementing this interface just
* for bean lookup purposes.
* 实现这个接口有这样的功能,比如你需要在当前的对象里得到一系列的相关bean.需要注意的是,
* 通过bean的说明的配置实现这个接口的时候,仅仅用来查找bean
*
*
This interface can also be implemented if an object needs access to file
* resources, i.e. wants to call {@code getResource}, wants to publish
* an application event, or requires access to the MessageSource. However,
* it is preferable to implement the more specific {@link ResourceLoaderAware},
* {@link ApplicationEventPublisherAware} or {@link MessageSourceAware} interface
* in such a specific scenario.
* 当你想获取文件的资源的时候,例如想要调用getResource或者是想要发布一个应用事件或者需要访问信息资源的权限.
* 然而,如果是上述场景,更为推荐的做法是实现特殊的接口ResourceLoaderAware、ApplicationEventPublisherAware、
* MessageSourceAware
*
*
Note that file resource dependencies can also be exposed as bean properties
* of type {@link org.springframework.core.io.Resource}, populated via Strings
* with automatic type conversion by the bean factory. This removes the need
* for implementing any callback interface just for the purpose of accessing
* a specific file resource.
* 需要注意的是文件的资源依赖也可以以bean的属性的方式暴露.这个去除了为了访问特殊文件资源而实现callback接口
* 的需要
*
*
{@link org.springframework.context.support.ApplicationObjectSupport} is a
* convenience base class for application objects, implementing this interface.
*
*
*
For a list of all bean lifecycle methods, see the
* {@link org.springframework.beans.factory.BeanFactory BeanFactory javadocs}.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
* @see ResourceLoaderAware
* @see ApplicationEventPublisherAware
* @see MessageSourceAware
* @see org.springframework.context.support.ApplicationObjectSupport
* @see org.springframework.beans.factory.BeanFactoryAware
*/
用途: 这个接口可以让你知道当前的类里运行的ApplicationContext是什么,通过这个ApplicationContext则可以获取spring容器里的任何一个bean(通过beanid来获取). 这个是他的主要用途,而通过实现这个接口还可以获取任何调用getresource,或者是想要发布一个应用事件,或者是发布一个消息,通常这种情况建议使用ResourceLoaderAware、ApplicationEventPublisherAware、 MessageSourceAware这三个接口.
今天我们通过一个简单的示例来说明实现这个类可以得到容器的功能,代码如下:
package top.huster.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
/**
* Created by longan.rtb on 16/11/16.
*/
@Service
public class SpringTest implements ApplicationContextAware{
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
applicationContext = applicationContext;
}
public void getBeanByName() {
Object object = applicationContext.getBean("beanname");
}
}
这样在这个类里面,你可以在任何时候获得任何的一个bean
ApplicationListener的介绍
这个接口同样在org.springframework.context的包里面,他只定义了一个方法onApplicationEvent,结合ApplicationEvent类的使用,你可以实现当容器的某个事件触发的时候,你可以写一些自己的业务逻辑,这个也是spring强大的地方,他开放的东西让你满足你很多特殊的需求每当在一个ApplicationEvent发布到 ApplicationContext时,这个bean得到通知。其实这就是标准的Oberver设计模式。
package top.huster.spring.test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;
/**
* Created by longan.rtb on 16/11/16.
*/
@Service
public class SpringTest implements ApplicationListener{
public void onApplicationEvent(ApplicationEvent event) {
if (!(event instanceof ContextRefreshedEvent)) {
return;
}
//do something
}
}
通常,你想要在bean被初始化的时候做一些事情,但是经常遇到你依赖的类还没被初始化这样做起来就很被动,经常出现空指针的问题,通过实现这个类就可以去除你的难言之隐,这个比通过xml配置一个initMethod来的更为优雅,因为你需要客户在用你的bean的时候配置初始化的方法. 通常这样使用的时候,你的xml配置如下:
今天先到这里,下次继续分享。