ITeye博客

ApplicationContextAware

prodream · 2010-03-30 14:36 · Spring · 企业架构 · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。