Original link:http://blog.csdn.net/a9529lty/article/details/42145545
1. When the servlet mode is loaded,
"Web. Xml"
XML code
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</ Servlet-class>
- <init-param>
- <param-name>contextconfiglocation</param-name>
- <param-value>classpath*:/springmvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
The key of the spring container placed in the ServletContext is Org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC
Note the following SPRINGMVC, which is the value of your servlet-name configuration, be aware of changes in time.
Java code
- ServletContext sc=
- Webapplicationcontext attr = (webapplicationcontext) sc.getattribute (" Org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC ");
2, Listener mode load:
"Web. Xml"
XML code
- <context-param>
- <param-name>contextconfiglocation</param-name>
- <param-value>/web-inf/applicationcontext</param-value >
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener< /Listener-class>
- </Listener>
"Jsp/servlet" can be achieved in this way
Java code
- ServletContext context = Getservletcontext ();
- Webapplicationcontext ApplicationContext = webapplicationcontextutils. Getwebapplicationcontext (context);
3, the general method to, artifact Ah, the former 1, 22 methods are not universal, can be discarded.
In the configuration file, add:
XML code
- <!--for holding ApplicationContext, you can use the static method of Springcontextholder.getbean (' xxxx ') to get the Spring bean object--
- <beans class="Com.xxxxx.SpringContextHolder" lazy-init="false" />
Java code
- Import Org.springframework.context.ApplicationContext;
- Import Org.springframework.context.ApplicationContextAware;
- /**
- * Save the spring applicationcontext with a static variable to remove the Applicaitoncontext from any code anywhere.
- *
- */
- Public class Springcontextholder implements Applicationcontextaware {
- Private static ApplicationContext ApplicationContext;
- /**
- * Implement the context injection function of the Applicationcontextaware interface and store it in a static variable.
- */
- Public void Setapplicationcontext (ApplicationContext applicationcontext) {
- Springcontextholder.applicationcontext = ApplicationContext; //Nosonar
- }
- /**
- * Gets the applicationcontext stored in the static variable.
- */
- Public static ApplicationContext Getapplicationcontext () {
- Checkapplicationcontext ();
- return applicationcontext;
- }
- /**
- * Gets the bean from static variable ApplicationContext, automatically transforms to the type of the assigned value object.
- */
- @SuppressWarnings ("unchecked")
- Public static <T> T Getbean (String name) {
- Checkapplicationcontext ();
- Return (T) Applicationcontext.getbean (name);
- }
- /**
- * Gets the bean from static variable ApplicationContext, automatically transforms to the type of the assigned value object.
- */
- @SuppressWarnings ("unchecked")
- Public static <T> T Getbean (class<t> clazz) {
- Checkapplicationcontext ();
- Return (T) Applicationcontext.getbeansoftype (clazz);
- }
- /**
- * Clear ApplicationContext static variables.
- */
- Public static void Cleanapplicationcontext () {
- ApplicationContext = null;
- }
- Private static void Checkapplicationcontext () {
- if (ApplicationContext = = null) {
- Throw New IllegalStateException ("Applicaitoncontext not injected, please define Springcontextholder in Applicationcontext.xml" );
- }
- }
- }
Go: How to get spring-managed beans