@ Value does not get the Value of spring two configuration files applicationContext. xml and xxx-servlet.xml, servletcontext

Source: Internet
Author: User

@ Value does not get the Value of spring two configuration files applicationContext. xml and xxx-servlet.xml, servletcontext

Configuration files are often used in projects and are commonly defined as properties. For ease of use, the following configurations are generally made in the spring configuration file:

<context:property-placeholder        ignore-unresolvable="true" location="classpath*:/application.properties" />

In this way, you can directly use @ Value ("$ {name}") in the program code to get the variable Value defined in The properties file.

However, if this value is not obtained in the Controller, the $ {name} string is output directly, but the value is not parsed, but can be obtained in the service. it's a bit strange. Obviously, the Controller does not seem to introduce the variables in the properties file, but is processed as a normal string .. suddenly thought of this project had 2 configuration files, 1 WEB-INF under the springmvc-servlet.xml, 1 applicationContext under the classpath. xml, where applicationContext. placeholder is defined in xml.

To tell the truth, I didn't pay attention to the difference in this configuration file before. I thought it was just a different place. With this opportunity, I found some information. first, we will introduce spring in the web. configuration in xml:

<! -- Springmvc configuration start -->
<Servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class> <! -- Servlet can be customized. the location and name of the xml configuration file, which is under the WEB-INF directory by default and named [<servlet-name>]-servlet. xml, for example, spring-servlet.xml <init-param> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/spring-servlet.xml </param-value> </init-param> --> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern> *. do </url-pattern> </servlet-mapping>
<! -- Springmvc configuration ended -->
<! -- Start Spring configuration -->
<Listener>
<Listenerclass> org. springframework. web. context. ContextLoaderListener </listener-class>
</Listener>
<! -- Specifies the directory where the configuration file of Spring Bean is located. Default Configuration Under the WEB-INF directory -->
<Context-param>
<Param-name> contextConfigLocation </param-name>
<Param-value> classpath: config/applicationContext. xml </param-value>
</Context-param>
<! -- Spring configuration ended -->

We can see two types of configuration: spring configuration and springmvc configuration. spring configuration is introduced as a listener, and the applicationContext under the WEB-INF is searched by default if the xml configuration file address is not specified. xml file. springmvc is introduced in the form of servlet. When the introduced xml configuration file address is not specified, the [servlet-name]-servlet under the WEB-INF will be automatically introduced. xml file, in this example, springmvc-servlet.xml. spring configuration is introduced first, and spring MVC configuration in servlet form is introduced.

It is worth noting that the spring MVC configuration file can directly introduce the bean defined in the spring configuration file with id, but this is not the case. each springmvc configuration file xxx-servlet.xml corresponds to a web. servlet definition in xml. when multiple springmvc configuration files exist, they cannot access each other.

In other people's posts on Baidu, I saw an official original explanation. I copied it and gave it a rough literal explanation:

Spring lets you define multiple contexts in a parent-child hierarchy. spring allows you to define multiple contexts in The applicationContext in The parent-child inheritance relationship. xml defines the beans for the "root webapp context", I. e. the context associated with the webapp. applicationContext. the xml file defines The bean for "root webapp app context", that is, its context is The spring-servlet.xml (or whatever else you call it) That webapp wants to associate) defines the beans for one servlet's app context. there can be composed of these in a webapp,
The spring-servlet.xml file (or something you'll get used to) defines beans for a servlet application context. There can be multiple configuration files in a webapp,
One per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2 ). each spring's servlelt (for example, a servlet named spring1 has a configuration file spring1-servlet.xml and a servlet named spring2 has a configuration file spring2-servlet.xml ). beans in spring-servlet.xml can reference beans in applicationContext. xml, but not vice versa. the bean defined in the spring-servlet.xml can be referenced directly in applicationContext. the bean defined in xml, but not the other way around. all Spring MVC controllers must go in the spring-servlet.xml context. all springmvc controllers must run in the context of the spring-servlet.xml. in most simple cases, the applicationContext. xml context is unnecessary. it is generally used to contain beans that are shared between all servlets
In most simple cases, the context of applicationContext. xml is not required. It is usually used to include beans for sharing among all servlets in the webapp.
In a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.
If you only have one servlet, there is no need to define applicationContext. xml unless you have a special application.

Back to the problem that @ Value cannot get the Value at the beginning, it is clear that the Controller is defined in the servlet configuration file of springmvc, therefore, the @ Value annotation in the Controller will be searched from springmvc configuration. Therefore, you only need to configure placeholder again in springmvc configuration, the @ Value annotation in Controller can get the Value.

Related Article

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.