@Value 2 Profiles Applicationcontext.xml and Xxx-servlet.xml for spring that are not value-derived

Source: Internet
Author: User

Configuration files are often used in projects, which are commonly defined as properties, and are typically configured in the spring configuration file for ease of use:

< Context:property-placeholder         ignore-unresolvable = "true"  Location = "Classpath*:/application.properties" />

This allows you to directly fetch the value of the variable defined in the properties file by using @value ("${name}") directly in the program code.

However, in a project found a situation, in the controller can not get this value, the direct output of the ${name} string, and did not parse out the value, but in the service is able to fetch. It's kind of weird. The variable in the properties file is apparently not introduced in the controller, and is treated as a normal string. Suddenly think of this project has 2 configuration files, 1 in the Web-inf under the springmvc-servlet.xml,1 under the classpath under the Applicationcontext.xml, Where applicationcontext.xml is defined in placeholder.

To tell the truth before I did not notice the difference between the configuration file, always thought that just put the location is not the same, with this opportunity to find some information. Let's look at a configuration that introduces spring in Web. xml:

<!--SPRINGMVC configuration start-up
<servlet> <Servlet-name>Springmvc</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <!--You can customize the location and name of the Servlet.xml configuration file by default to the Web-inf directory, with the name [<servlet-name>]-servlet.xml, such as 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 ends--
<!--Spring Configuration start - 
<Listener>
<Listenerclass>Org.springframework.web.context.ContextLoaderListener</Listener-class>
</Listener>
<!--Specifies the directory where the spring bean's configuration file resides. The default configuration is in the Web-inf directory -
<Context-param>
<Param-name>Contextconfiglocation</Param-name>
<Param-value>Classpath:config/applicationcontext.xml</Param-value>
</Context-param>
<!--spring Configuration ends-

You can see 2 types of Spring configurations and SPRINGMVC configurations. Where the spring configuration is introduced as a listener, The Applicationcontext.xml file under Web-inf is found by default when you do not specify an XML configuration file address. SPRINGMVC is introduced as a servlet, and when no XML configuration file address is specified, it is automatically introduced under Web-inf [ Servlet-name]-servlet.xml file, in this case, springmvc-servlet.xml. The order of introduction is to introduce the spring configuration first and then introduce the SPRINGMVC configuration in the Servlet form.

It is important to note that the SPRINGMVC configuration file can be directly introduced to the bean defined in the spring configuration file by using the ID. But not in turn. Each SPRINGMVC configuration file xxx-servlet.xml corresponds to a servlet definition in Web. Xml. When there are multiple SPRINGMVC profiles, they are not accessible to each other.

In Baidu in other people's posts to see a paragraph should be the official interpretation of the original, I excerpt and rough translation:

Spring lets define multiple contexts in a parent-child hierarchy.Spring allows you to define multiple contexts in a parent-child inheritance relationshipThe applicationcontext.xml defines the beans for the "root webapp context" and i.e. the context associated with the WebApp.The applicationcontext.xml file defines the bean for "root webapp application Context", meaning that its context is the one that WebApp wants to associate.The spring-servlet.xml (or whatever else call it) defines the beans for one servlet's app context. There can many of these in a webapp,
The spring-servlet.xml file (or something else you used to call it) is for a servlet application context to define the bean. There can be more than one configuration file in a WebApp,
One per Spring servlet (e.g Spring1-servlet.xml for servlets spring1, spring2-servlet.xml for servlet spring2).each spring Servlelt (for example: a servlet named Spring1 has a configuration file Spring1-servlet.xml, 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.beans defined in Spring-servlet.xml can refer directly to the bean defined in Applicationcontext.xml, but not in turn.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 counterpart.In more simple cases, the applicationcontext.xml context is unnecessary. It's generally used to contain beans that's shared between all Servlets
in most simple cases, the applicationcontext.xml corresponding context does not have to be. It is commonly used to include those beans that are used to share among all servlets in WebApp.
In a webapp. If you are only having one servlet, then there's not really much point, unless has a specific use for it.
If you have only one servlet, then there's really no need to define applicationcontext.xml unless you have a special application.

Then back to the beginning of the @value not get the value of the problem, it is now clear that because the controller is defined in the SPRINGMVC servlet configuration file, so the use of @value annotations in the controller is found in the SPRINGMVC configuration , it is only required to re-configure the placeholder in the SPRINGMVC configuration again, the controller @value annotations can be taken to the value.

@Value 2 Profiles Applicationcontext.xml and Xxx-servlet.xml for spring that are not value-derived

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.