What are the benefits of Spring multi-configuration files?
Splitting configuration files according to purpose and function can improve the readability and maintenance of configuration files, such as separate configurations with few changes such as configuration transaction management and data sources and configuration beans.
Several ways Spring reads configuration files:
1. Read using the ApplicationContext method provided by Spring itself
In Java programs, you can use ApplicationContext two implementation classes ClassPathXmlApplicationContext and FileSystemXmlApplicationContext to read multiple configuration files, and their constructors can receive an array of configuration files.
For example: ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations); is similar to the way of creating ApplicationContext using FileSystemXmlApplicationContext, the only difference is that the paths of the two search configuration files are different: ClassPathXmlApplicationContext searches configuration files through the CLASSPATH path: FileSystemXmlApplicationContext searches configuration files in the current path.
Method 1: Save ApplicationContext object during initialization
Code:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); illustrate:
This method is suitable for stand-alone applications using the Spring framework, where programs require Spring to be manually initialized through configuration files.
Method 2: Get ApplicationContext object through the tool class provided by Spring
Code:
import org.springframework.web.context.support.WebApplicationContextUtils; ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc) ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc) ac1.getBean("beanId"); ac2.getBean("beanId"); illustrate:
This method is suitable for B/S systems using Spring framework, obtaining the ApplicationContext object through the ServletContext object, and then obtaining the required class instance through it.
The difference between the above two tool methods is that the former throws an exception when the acquisition fails, and the latter returns null.
Method 3: Inherited from abstract class ApplicationObjectSupport
illustrate:
The abstract class ApplicationObjectSupport provides the getApplicationContext() method, which can easily obtain the ApplicationContext. When Spring is initialized, the ApplicationContext object will be injected through the setApplicationContext(ApplicationContext context) method of the abstract class.
Method 4: Inherited from abstract class WebApplicationObjectSupport
illustrate:
Similar to the above method, call getWebApplicationContext() to get WebApplicationContext
Method 5: Implement the interface ApplicationContextAware
illustrate:
Implement the setApplicationContext(ApplicationContext context) method of this interface and save the ApplicationContext object. When Spring is initialized, the ApplicationContext object is injected through this method.
The above methods are suitable for different situations, please choose the corresponding method according to the specific situation.
2. Loading when using web project startup
What configuration files are automatically loaded in web.xml:
<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring/spring-core.xml</param-value></context-param> <servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB- INF/spring/spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
When there are multiple numbers, you can use * to replace them.
<servlet> <servlet-name>app</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml,/WEB-INF/user_spring.xml</param-value></context-param> <load-on-startup>1</load-on-startup> </servlet>
3. Import other configuration files into the Xml configuration file
Configure the application service to load in /WEB-INF/applicationContext.xml. You can use import to import other configuration files in applicationContext.xml.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"><import resource="spring-servlet.xml"/><import resource="spring-security.xml"/><import resource="spring-hibernate.xml"/><import resource="spring-redis.xml"/><import resource="spring-redis.xml"/><beans>