如何在java类中使用applicationcontextaware

时间:2012-04-12 18:33:00

标签: spring applicationcontext

我需要在java类中加载applicationcontext,其中定义了applicationcontextaware bean。我需要使用applicationcontextaware访问applicationcontext.xml中的其他bean。我不想使用

加载上下文

的ClassPathXmlApplicationContext( “applicationContext.xml中”);

我需要像这样访问applicationContext中的bean

ApplicationContextAccess.getInstance()getApplicationContext.getbean( “BeanName”);

Applicationcontextacess实现为单例类:

public class ApplicationContextAccess实现ApplicationContextAware {

private ApplicationContext applicationContext = null;
private static ApplicationContextAccess applicationContextAccess=null;

private ApplicationContextAccessor() {

}

public static synchronized ApplicationContextAccess getInstance() {

        if(applicationContextAccess == null)
    {
        applicationContextAccess = new ApplicationContextAccess();
    }

    return applicationContextAccess;

}
public void ApplicationContext getApplicationContext() {
    return applicationContext;
}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    applicationContext = applicationContext;

}

}

我需要访问applicationContext中的bean,就像这个ApplicationContextAccess.getInstance()。getApplicationContext.getbean(“BeanName”);

但我怀疑getApplicationContext如何加载applicationContext.xml ........?

0 个答案:

没有答案