How to call the Spring container in a Struts2 interceptor

Source: Internet
Author: User

The first type:

Some beans in the spring configuration file are usually called with ApplicationContext, so the spring context container is created first.

ApplicationContext AC = (applicationcontext) invocation.getinvocationcontext (). Getapplication (). Get ( Webapplicationcontext.root_web_application_context_attribute);

The invocation in the above code is the parameter of the Struts2 Interceptor method intercept (Actioninvocation invocation) method

Where Root_web_application_context_attribute is defined in the source code:

String Root_web_application_context_attribute = WebApplicationContext.class.getName () + ". ROOT ";

So this way you can get ApplicationContext

The second type:

ServletContext Sc=servletactioncontext.getservletcontext ();
ApplicationContext Ctx=webapplicationcontextutils.getwebapplicationcontext (SC);

Get the servlet context first (this is one of the three ways that Struts2 accesses the servlet)

Spring provides us with a convenient access to the Webapplicationcontext Webapplicationcontext tool class, which is easier than the previous one,

But if you look closely at the source of Webapplicationcontextutils's Getwebapplicationcontext (ServletContext SC), you will find that you will still use Root_web_application_ Context_attribute, so it boils down to the same principle.

public static Webapplicationcontext Getwebapplicationcontext (ServletContext SC) {
Return Getwebapplicationcontext (SC, webapplicationcontext.root_web_application_context_attribute);
}

How to call the Spring container in a Struts2 interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.