Spring solution for problems that the spring configuration file cannot find due to Web configuration

Source: Internet
Author: User

When integrating a technology into spring, we often find that the following error is reported:

Org.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' Logincontroller ': injection of autowired dependencies failed; Nested exception is org.springframework.beans.factory.BeanCreationException:Could not autowire field:

Many of the methods have not been solved until later, because spring's configuration files are not placed in the

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

caused by the <context-param>:

There are two specific solutions:

1. Put all the XML configuration files in <param-value>, generally speaking, the comma after the XML can be added or not (the general system can recognize):
<listener>    <listener-class>org.springframework.web.context.contextloaderlistener</ listener-class>  </listener>    <context-param>    <param-name>contextconfiglocation </param-name>     <param-value>    classpath:spring-dao.xml,   classpath:spring-service.xml,    classpath:springmvc-servlet.xml    </param-value>  </context-param>

  

2. Use * to match XML files with certain similarities and put them in <param-value>, which reduces the redundancy of the code to some extent:
<listener>    <listener-class>org.springframework.web.context.contextloaderlistener</ listener-class>  </listener>    <context-param>    <param-name> Contextconfiglocation</param-name>        <param-value>classpath*:spring-*.xml</param-value>      </context-param>
Principle Introduction:

1, define the Contextconfiglocation parameter in Web. Xml. Spring uses this parameter to load the. All comma-delimited and all-in. If this parameter is not available, spring defaults to load web-inf/ Applicationcontext.xml file.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
Classpath*:conf/spring/applicationcontext_core*.xml,
Classpath*:conf/spring/applicationcontext_dict*.xml,
Classpath*:conf/spring/applicationcontext_hibernate.xml,
Classpath*:conf/spring/applicationcontext_staff*.xml,
Classpath*:conf/spring/applicationcontext_security.xml
Classpath*:conf/spring/applicationcontext_modules*.xml
Classpath*:conf/spring/applicationcontext_cti*.xml
Classpath*:conf/spring/applicationcontext_apm*.xml
</param-value>
</context-param>

The contextconfiglocation parameter defines the Spring configuration file to be loaded. The principle is described as follows:
, the use of servletcontextlistener implementation.
Spring provides an implementation class Contextloaderlistener for Servletcontextlistener, which can be used as a
Used for listener, it will automatically find the Applicationcontext.xrnl file under web-inf/when it is created. Because
This, if there is only one configuration file, and the file name is Applicationcontext.xml, only the Web. xml
Add the following code to the file:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
If more than one configuration file needs to be loaded, consider using the <context-para element to determine the configuration file's
Filename. Because Contextloaderlistener is loaded, a parameter named Contextconfiglocation is found.
Therefore, the parameter name should be contextconfiglocation when configuring Context-param.
The Web. xml file with multiple configuration files is as follows:
<1--XML File Header II
<?xml version= "L.O" encoding= "Gong 80-8859-1"?>
< WEB.XM1 file DTD and other information
<! DOCTYPE Web-app
Public "-//sun Microsystems. Work Port C.//DTD Web application 2.3//en "
' Http://java.sun.com/dtd/web-app_2_3.dtd ' >
<web-app>
<! a certain number of configuration files >
<context-param>
The <1--parameter is named contextconfiglocation...〉
<param-name>contextConfigLocation</param-name>
<! between more than one configuration file, separated by two
<param-value>/web-worker Nf/daocontext.xml./web-inf/application
Context.xml</param-value>
</context-param>
<!--using listener to create Applicat oncontext instances--
<listener>
&LT;LISTENER-CLASS&GT;ORG.SPR worker Ngframework.web.context.ContextLoader
Listener</listener-class>
</listener>
</web-app>
If the configuration file is not contextconfiglocation specified, Spring automatically finds application
Context. XRNL configuration file. If there is a contextconfiglocation, the configuration file determined by this parameter is used.
A string specified by this parameter, and Spring's Contextloaderlistener is responsible for breaking the string into multiple
Config file, comma ",", Space "" and semicolon ";" Can be used as a separator for a string.
If there is neither applicationcontext. XRNL file, nor do you use the Contextconfiglocation parameter
configuration file, or contextconfiglocation determined configuration file does not exist. Will cause spring to fail
Loading a configuration file or not creating a ApplicationContext instance as expected

The same effect can be achieved by configuring a servlet that spring sets for loading.
Implemented with the Load-on-startup Servlet.
Spring provides a special class of Servllet: Contextloaderservlet. When the servlet is started, it will
Automatically finds the applicationcontext of Web-in Kusakabe. XML file.
Of course, in order for the Contextloaderservlet to start with the application, you should configure this servlet to
The value of the Load-on-startup Servleto Load-on-startup is a little bit more appropriate, because it is guaranteed to application
Context first created. If there is only one configuration file and the file name is ApplicationContext. XML, then in
Add the following code to the Web. xml file:
<servlet>
<servlet-name>context</servlet Mouth-arne>
<servlet-class>org.springframework.web.context.contextloaderservlet</
Servlet-class>
<load-on-startup>l</load-o Mouth-startup>
</servlet>

。 The web with multiple profiles and the NL file is as follows:
<!--file header for an XML file--
<?xml version= "1.0" encoding= "Gong so-8859-1"?>
<! --Web. xml file dtd and other information →
<! DOCTYPE Web-app
Public "-//sun Microsystems, Port c.//dtd Web application 2.3//en"
' Http://java.sun.com/dtd/web-app_2_3.dtd ' >
<web-app>
< ' Determine multiple profiles one >
<context-param>
<!--parameter named contextconfiglocation-->
<param-name>contextConfigLocation</param-name>
<!--multiple profiles with, separating one)
<param-value>/web-Nf/daocontext.xml,! web-worker Nf/applicationcontext.
Xml</param-value>
</context-param>
<! a load-on-startup Servlet to create a applicat oncontext instance
<servlet>
<servlet-narne>context</servlet-narne>
<servlet-class>org.springframework.web.context.contextloader
Servlet</servlet-class>
<! a little bit more appropriate, will first load a
<load-on-startup>l</load-on-startup>
</servlet>
</web-app>


2, using the match character

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

Hibernate, for example, puts hibernate-related configuration in the Applicationcontext-hibernate.xml file, while some global-related information is placed in Applicationcontext.xml, Other configurations are similar. This allows you to load, without having to write with a space or comma separated!


3 If you are using struts to load multiple spring profiles. The following configuration is actually a contextconfiglocation variable.

Struts-config.xml, add this.
<plug-in classname= "Org.springframework.web.struts.ContextLoaderPlugIn" >
<set-property property= "Contextconfiglocation"
Value= "/web-inf/applicationcontext.xml,
/web-inf/action-servlet.xml,,,,,,, "/>

4, if non-EE application direct program loading.

ApplicationContext act = new Classpathxmlapplicationcontext (new string[]{"Bean1.xml", "Bean2.xml"});

Beandefinitionregistry reg = new Defaultlistablebeanfactory ();

Xmlbeandefinitionreader reader = new Xmlbeandefinitionreader (reg);

Reader.loadbeandefinitions (New Classpathresource ("Bean1.xml"));

Reader.loadbeandefinitions (New Classpathresource ("Bean2.xml"));

Beanfactory bf = (beanfactory) reg;

Spring solution for problems that the spring configuration file cannot find due to Web configuration

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.