JDK-doc 2.5.6
Interface ApplicationContextAware
Interface to be implemented by any object that wishes to be notified of the ApplicationContext
that it runs in.
method:
/*
* Set the ApplicationContext that this object runs in.
*/
void setApplicationContext(ApplicationContext applicationContext)
实现了这个接口的类将告诉application:我需要你提供给我ApplicationContext实例.
public class UserService implements ApplicatonContextAware{
private static UserService instance;
private static ApplicationContext appContext;
final public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//System.out.println("BeanAware.setApplicationContext("+getClass().getName()+")");
Map beans = applicationContext.getBeansOfType(getClass());
instance=((UserService) beans.values().iterator().next());
this.appContext = applicationContext;
}
public static UserService getInstance(){
return instance;
}
}//end class