简体   繁体   English

如何从contextloaderlistener加载的应用程序上下文中获取bean?

[英]how to get beans from application context loaded by contextloaderlistener?

I'm new to Spring/Spring Mvc and here's my problem. 我是Spring / Spring Mvc的新手,这是我的问题。 In my webapp, besides the spring-servlet.xml , I have a jdbc.xml which define beans like datasource, dao ... Before using contextloaderlistener , I load my jdbc.xml inside the Controller's constructor like this ApplicationContext context = new ClassPathXmlApplicationContext("jdbcbeans.xml") then get the beans from that. 在我的web应用程序中,除了spring-servlet.xml之外,我还有一个jdbc.xml,它定义了诸如数据源,dao之类的bean。在使用contextloaderlistener之前,我将jdbc.xml加载到了控制器的构造函数中,例如ApplicationContext context = new ClassPathXmlApplicationContext( “ jdbcbeans.xml”),然后从中获取bean。 But since I'm using contextloaderlistener to load the file, how can I get the reference to the context ? 但是,由于我正在使用contextloaderlistener加载文件,因此如何获得对上下文的引用? I was able to set up everything using those @Autowired things but I just want to know is there any way to do that ? 我能够使用这些@Autowired东西来设置一切,但是我只想知道有什么方法可以做到这一点?

You can use WebApplicationContextUtils . 您可以使用WebApplicationContextUtils

ApplicationContext context;
context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

See here for details. 有关详细信息,请参见此处

You can do the following to get an instance of Application Context in case of a container managed bean use ApplicationContextAware interface 如果容器管理的bean使用ApplicationContextAware接口,则可以执行以下操作来获取Application Context的实例

    public class MyBean implements ApplicationContextAware {
       private static ApplicationContext context;     

     public void setApplicationContext(ApplicationContext acontext) throws BeansException {
       context = context;
     }

     public static ApplicationContext getApplicationContext() {
       return context;
     }
   }

Or you can write the following 或者您可以编写以下内容

@Autowired
private ApplicationContext Context;

An instance of the Application Context will be autowired. Application Context的实例将被自动装配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于加倍的上下文(servlet + ContextLoaderListener),所有Spring Framework bean都会重复 - All Spring Framework beans get duplicated, becase of doubled context (servlet+ContextLoaderListener) Apache Tomcat 8春季加载时间编织不适用于ContextLoaderListener加载的bean - Apache Tomcat 8 spring load time weaving is not working for beans loaded by ContextLoaderListener 如何从测试App上下文中排除Spring Boot应用程序bean? - How to exclude spring boot application beans from the test App context? 如何避免从Webapp的应用程序上下文中检索Spring bean? - How to avoid retrieving spring beans from application context in webapp? 如何从过滤器中的servlet上下文中获取bean - How can i get beans from servlet context in filter Web应用程序中的ContextLoaderListener和Servlet上下文是什么? - what is ContextLoaderListener and Servlet context in a web application? @Inject不从应用程序上下文中注入Bean - @Inject Not Injecting Beans from Application Context 在ServletContext和ContextLoaderListener中组织bean - Organizing beans in ServletContext and ContextLoaderListener 如何从外部应用程序检索Tomcat中加载的Spring上下文 - How to retrieve Spring context loaded in Tomcat from outside application 将xml定义中的其他bean注册到已经初始化的应用程序上下文中 - Register additional beans from xml definition into application context that is already initialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM