Spring ---XXXAware 接口 XXXCapable接口

1.Spring 中 XXXAware 接口

XXXAware 表示对XXX可以感知,实现 XXXAware接口的类 表示 这个类需要XXX

如果某个类 需要使用spring的一些东西,通过实现XXXAware接口就可以了,Spring 看到会给你送过来,接收方式 是通过  实现XXXAware接口的唯一方法set方法来接收

例如 某个类 需要使用ApplicationContext上下文,就可以实现如下

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

//这个类 需要使用ApplicationContext ,只需要 实现ApplicationContextAware接口
//spring 会自动调用 setApplicationContext()方法 把ApplicationContext传给我们
//我们 用this.applicationContext 接收就可以了
public class SpringAwareTest implements ApplicationContextAware{

	private ApplicationContext applicationContext;

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

2.Spring 中 XXXCapable接口

XXXCapable 表示 有提供XXX的能力

实现XXXCapable接口的类 告诉Spring 它有提供XXX的能力,如果Spring需要XXX,可以调用 这个类的get()方法来获得

例如 某个类实现了 EnvironmentCapable接口,表示告诉Spring 它具有Environment能力,它可以提供Environment,如果Spring需要Environment,可以调用它的 getEnvironment方法来获得。

 

 

posted on 2020-06-14 21:20  dreamstar  阅读(121)  评论(0编辑  收藏  举报