---
title: "ApplicationContextAware"
author: "prodream"
date: "2010-03-30T14:36:09Z"
tags: []
categories: ["企业架构"]
slug: "applicationcontextaware"
draft: false
lastmod: "2010-03-30T14:36:09Z"
id: 628180
url: "https://prodream.iteye.com/blog/628180"
---

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