Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the advanced configuration methods of the Spring MVC framework

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the advanced configuration methods of the Spring MVC framework. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

Synchronize Bean properties

One possible solution to this problem is to put all host-specific parameters in a normal Java properties file, using Spring's PropertyPlaceHolderConfigurer class, to write these parameters to the Bean property.

Using this solution, we can generate the following properties file (/ Web-INF/JDBC.properties):

Jdbc.driver=org.postgresql.Driver

Jdbcjdbc.url=jdbc:postgresql://localhost/test

Jdbc.user=postgres

Jdbc.password=

Our Bean configuration is as follows:

/ WEB-INF/jdbc.properties

${jdbc.driver}

${jdbc.url}

${jdbc.user}

${jdbc.password}

As mentioned above, we defined an instance of the PropertyPlaceholderConfigurer class and set its location property to our properties file. This class is implemented as a post-processor of the Bean factory and will replace all placeholders (${...} value) with properties defined in the file.

With this technique, we can remove all host-specific configuration properties from applicationContext.XML. In this way, we are free to add a new Bean to the file without worrying about host-specific synchronization. This simplifies production deployment and maintenance.

Synchronous connection

The above technique solves * problems, but this technique is not suitable if you plan to modify the Bean connection between different application deployments. One solution to this problem is to create an additional XML definition file called applicationContext- [hostname] .xml. Where [hostname] is the name of the host where the application is deployed. For example, on a local machine, this file is usually named applicationContext-localhost.xml, but it may be renamed applicationContext- somehost.com.xml at deployment time.

It can be guessed that this file must include all configuration Bean specific to a host. In this article, we will assume that the dataSource bean definition will be located in such a file, rather than the generic applicationContext.xml definition. Of course, this mechanism is not in conflict with the former, but for simplicity and clarity, we will only focus on this approach.

Now that we have a specific configuration, let's discuss how to integrate it into the entire Spring MVC framework configuration concept. There are many ways to achieve this, and we will explain them in detail. But first, we should note that because some Bean may be in separate configuration files, all local references to them in applicationContext.xml must be replaced with global names.

For example, the following reference:

Should be changed to:

After that, we have a lot of ways to add additional resources for configuration. The most obvious of these is the use of tags to include this additional resource in the applicationContext.xml configuration file. When you use it, place the tag at the beginning of the applicationContext.xml file. For example:

All generic XML definitions in stand-alone Bean definition files and normal application context definition files now have host-specific connections. Since most Bean are not host-specific, we can work with applicationContext.xml files as freely as we do with other resources in Web applications and synchronize with them through the appropriate version control system.

However, the above methods also have some disadvantages. If you want to keep different configurations for different XML files, you must still worry about the synchronization of applicationContext.xml, because the names of resources must be changed according to different servers. Although it is a great improvement over the original solution, you only need to change the file name, but this still requires the manual assistance of the developer.

Since the host configuration does not need to be changed so frequently compared to applicationContext.xml, the next step is to move the host configuration to the web.xml file, if possible. Fortunately, we have a solution available. Take a look at the following snippet of web.xml configuration:

Org.springFramework.web.context.ContextLoaderListener

ContextConfigLocation

/ WEB-INF/applicationContext.xml / WEB-INF/applicationContext-somehost.com.xml

As you can see, in addition to the ContextLoaderListener that is common in the web.xml file, we have also added the contextConfigLocation context parameter configuration. This parameter is used to instruct the framework where to find these configuration files. If this parameter is omitted, Spring can only look in applicationContext.xml. Here we also define host-specific configuration files to use.

With this approach, we remove all host-specific configurations from the applicationContext.xml file, thus reducing their synchronization in different application deployments.

If this approach becomes your new habit, you can also make it more flexible. Host-specific configurations can also be removed from the web.xml file by following the following instructions.

To do this, we need to create classes specific to our application context:

Package net.nighttale.spring.util

Import Java.net.InetAddress

Import org.springframework.web.context.support.XmlWebApplicationContext

Public class PerHostXmlWebApplicationContext

Extends XmlWebApplicationContext... {

Protected String [] getDefaultConfigLocations (). {

String hostname = "localhost"

Try... {

Hostname = InetAddress.getLocalHost () .getHostName ()

} catch (Exception e) {

}

String perHostConfiguration = DEFAULT_CONFIG_LOCATION_PREFIX

+ "applicationContext-"

+ hostname

+ DEFAULT_CONFIG_LOCATION_SUFFIX

Logger.debug (

"Adding per host configuration file:"

+ perHostConfiguration

);

If (getNamespace ()! = null). {

Return new String []... {

DEFAULT_CONFIG_LOCATION_PREFIX

+ getNamespace ()

+ DEFAULT_CONFIG_LOCATION_SUFFIX

, perHostConfiguration}

}

Else... {

Return new String []... {

DEFAULT_CONFIG_LOCATION

, perHostConfiguration}

}

}

}

This class extends XmlWebApplicationContext, which is often used as the default value in Spring. The XmlWebApplicationContext class copies the configuration of the Web application from the XML definition file. By default, it can configure applications from the applicationContext.xml and [Servlet-name]-servlet.xml files. The only additional task performed by this class is to get the name of the host on which it resides and add the applicationContext- [hostname] .xml file to the list of configuration files.

To use this class, we need to compile it, include it in the class path, and instruct the Spring framework to use it. The first two steps are very simple, so we won't repeat them here. We can instruct Sping to use it through the contextClass context parameter. In addition to the original configuration in the web.xml file, we can also add the following:

ContextClass

Net.nighttale.spring.util.PerHostXmlWebApplicationContext

If we use this configuration snippet, three files will be used to initialize the framework: [servlet-name]-servlet.xml, applicationContext- [hostname] .xml, and applicationContext.xml.

As you can see, the applicationContext.xml and web.xml files are completely free of any specific configuration details, and you don't have to worry about breaking the configuration when updating your application.

However, this approach has one drawback. Because, whether you will use it or not, you need to have a third configuration file in the application deployment. In this case, a host-specific configuration is not required. For example:

* need to know the specific hostname that the application context class needs to look for. The easiest way to check the host name is to run the following code on the machine:

System.out.println (InetAddress.getLocalHost () .getHostName ())

It can be executed as Java code or as a script with Java-style syntax in your favorite scripting language, such as BeanShell or Groovy. After getting the name of the host, you should create a default / WEB-INF/applicationContext- [hostname] .xml empty folder (as we defined above), and then you can start.

These are all the contents of the article "what are the advanced configuration methods of the Spring MVC framework?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report