0

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

ClassPathXmlApplicationContext("applicationContext.xml");

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

ApplicationContextAccess.getInstance().getApplicationContext.getbean("BeanName");

Applicationcontextaccess 实现为单例类:

公共类 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........?

4

0 回答 0