简体   繁体   中英

How to get beans defined in main-servlet.xml from controller

I understand beans in my main-servlet.xml is in child context. And based on research I know I can get root ApplicationContext within my GreetingController through WebApplicationContextUtils. Which I'll get null because i didn't define such a root context in web.xml.

Now my question is how can i get child context and do something like childContext.getBean("forfun") without @AutoWired annotation,I think I've tested it and it works;

Thanks in advance for your reading and info.

Edit: AutoWire the ApplicationContext or implement ApplicationContextAware will help you get both the child and root ApplicationContext.

Edit2:I found another way to get the context created by defaultdispacher, the context is actually stored in servletcontext attributes with name org.springframework.web.servlet.FrameworkServlet.CONTEXT.(servletName) supposed your default servlet is main , you can get the context craete by defaultdispacher[main-servlet.xml] with the following code.

ApplicationContext context1=(WebApplicationContext) request.getSession().getServletContext().getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.main")

But still don't understand why in the WebApplicationContextUtils method we can't access child context.

web.xml file

<webapp>
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/main-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/main/*</url-pattern>
</servlet-mapping>
</web-app>

main-servlet.xml

<beans>
<bean name ="/testing/*" class="springmvc.GreetingController"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/Jsp/"
p:suffix=".jsp"/>
<bean id="forfun" class="springmvc.Foo"></bean>
</beans>

GreetingController.java

@Controller
public class GreetingController {

@AutoWired
Foo forfun;//this works even if forfun is defined in main-servlet.xml

@RequestMapping("/testing")
public String testing(HttpServletRequest request){
    ApplicationContext context = 
                WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        Foo f=(Foo) context.getBean("forfun");//supposed to work if i have it defined in root context
    /*
    I would something like this
    ChildContext child = Utils.getChildContext(request);
    Foo f=(Foo) child.getBean("forfun");
    */
        return "test";
    }
}

The main-servlet.xml you've declared above is loaded by your DispatcherServlet .

You can inject the corresponding WebApplicationContext with

@Autowired
private WebApplicationContext context;

and use it to get any bean defined in that context or a parent.

Note that this will work only if the bean you're injecting it into is created within the WebApplicationContext created by the DispatcherServlet .

Alternatively, as stated in the comments by M. Deinum, there's a static utility class called RequestContextUtils that provides a getWebApplicationContext(ServletRequest) method for doing the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM