Chapter 4 Java framework integration-splitting configuration files and java splitting
This chapter describes how to modify the code in Chapter 3 "Java framework integration-Enterprise Project architecture and multi-environment allocation". The link is as follows:
Http://www.cnblogs.com/java-zhao/p/5115136.html
1. Implementation Method
Set spring under the class path (src/main/resources) in the previous ssmm0-userManagement. split xml into spring. xml and spring-data.xml files, spring. xml remains in the class path in the ssmm0-userManagement, And the spring-data.xml is placed under the class path in the ssmm0-data, as shown in the location after splitting:
The spring. xml before splitting is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/conte Xt 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 "> <! -- Annotation scan --> <context: component-scan base-package = "com. xxx"/> <! -- Configure the fastjson converter --> <mvc: annotation-driven> <mvc: message-converters register-defaults = "true"> <bean class = "com. alibaba. fastjson. support. spring. fastJsonHttpMessageConverter "> </bean> </mvc: message-converters> </mvc: annotation-driven> <! -- Import the data source. The variables are read from the pom of ssmm0. --> <bean id = "xxxDataSource" class = "org. apache. tomcat. jdbc. pool. dataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {jdbc. driverClassName} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean> <! -- Introduce mybatis --> <bean id = "xxxSqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <property name =" dataSource "ref =" xxxDataSource "/> </bean> <bean id =" xxxMapperScannerConfigurer "class =" org. mybatis. spring. mapper. mapperScannerConfigurer "> <! -- Here is why the package name is com. xxx. mapper. user rather than com. xxx. user. mapper. For example, there are two projects: com. xxx. mapper. user and com. xxx. mapper. hotel, value only needs to write com. xxx. mapper. Otherwise, the value must be written to com. xxx. user. mapper, com. xxx. hotel. mapper --> <property name = "basePackage" value = "com. xxx. mapper "/> <property name =" sqlSessionFactoryBeanName "value =" xxxSqlSessionFactory "/> </bean> <! -- Configure velocity --> <bean id = "velocityConfigurer" class = "org. springframework. web. servlet. view. velocity. velocitycycler "> <property name =" resourceLoaderPath "> <value> WEB-INF/templates/</value> </property> <property name =" velocityProperties "> <props> <prop key = "input. UTF-8 </prop> <prop key = "output. encoding "> UTF-8 </prop> </props> </property> </bean> <bean id =" viewResolver "class =" org. springframework. web. servlet. view. velocity. velocityViewResolver "> <property name =" suffix "value = ". vm "/> <property name =" contentType "value =" text/html; charset = UTF-8 "/> <property name =" datatelattribute "value =" date "/> <property name =" numberToolAttribute "value =" number "/> </bean> </ beans>View Code
Split spring. xml and spring-data.xml are as follows:
Spring. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/conte Xt 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 "> <! -- Annotation scan --> <context: component-scan base-package = "com. xxx. web"/> <! -- Scan the web only --> <! -- Here you need to introduce the ssmm0-data configured in the spring-data.xml Project (before it can be successful, forget how to configure it) --> <import resource = "classpath: spring-data.xml"/> <! -- Configure the fastjson converter --> <mvc: annotation-driven> <mvc: message-converters register-defaults = "true"> <bean class = "com. alibaba. fastjson. support. spring. fastJsonHttpMessageConverter "> </bean> </mvc: message-converters> </mvc: annotation-driven> <! -- Configure velocity --> <bean id = "velocityConfigurer" class = "org. springframework. web. servlet. view. velocity. velocitycycler "> <property name =" resourceLoaderPath "> <value> WEB-INF/templates/</value> </property> <property name =" velocityProperties "> <props> <prop key = "input. UTF-8 </prop> <prop key = "output. encoding "> UTF-8 </prop> </props> </property> </bean> <bean id =" viewResolver "class =" org. springframework. web. servlet. view. velocity. velocityViewResolver "> <property name =" suffix "value = ". vm "/> <property name =" contentType "value =" text/html; charset = UTF-8 "/> <property name =" datatelattribute "value =" date "/> <property name =" numberToolAttribute "value =" number "/> </bean> </ beans>View Code
Spring-data.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" 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/sprin G-context-3.2.xsd "> <! -- Annotation scan --> <context: component-scan base-package = "com. xxx"/> <! -- Import the data source. The variables are read from the pom of ssmm0. --> <bean id = "xxxDataSource" class = "org. apache. tomcat. jdbc. pool. dataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {jdbc. driverClassName} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </bean> <! -- Introduce mybatis --> <bean id = "xxxSqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <property name =" dataSource "ref =" xxxDataSource "/> </bean> <bean id =" xxxMapperScannerConfigurer "class =" org. mybatis. spring. mapper. mapperScannerConfigurer "> <! -- Here is why the package name is com. xxx. mapper. user rather than com. xxx. user. mapper. For example, there are two projects: com. xxx. mapper. user and com. xxx. mapper. hotel, value only needs to write com. xxx. mapper. Otherwise, the value must be written to com. xxx. user. mapper, com. xxx. hotel. mapper --> <property name = "basePackage" value = "com. xxx. mapper "/> <property name =" sqlSessionFactoryBeanName "value =" xxxSqlSessionFactory "/> </bean> </beans>View Code
Note:
- The mybatis-related configuration files for data sources not directly related to the controller layer are located in the spring-data.xml file and placed in the class path of the ssmm0-data
- Put the fastjson converter and velocity configurations directly related to the controller layer in the spring. xml file.
Note:
- Spring. xml part of the annotation scan only needs to scan the ssmm0-userManagement, And the spring-data.xml at the annotation scan only needs to scan the package in the ssmm0-data.
- The spring. xml part needs to introduce spring-data.xml (but it does not need to be introduced before this configuration, this one has a friend who understands to give me some advice)
2. Meaning
- Place the spring-data.xml in the ssmm0-data project so that we can test the service, dao, mapper, and so on in the ssmm0-data project.
- In the future, if you modify the configuration of the data source or mybatis, you only need to modify the spring-data.xml, and do not need to modify the spring of each other business module. xml, which is the spring of each business module. the biggest significance of the combination of common configuration codes in xml
- Reduces the repeated configuration code of spring. xml in each business module.
3. Splitting principles
- Configuration information related to your module is placed under your module (that is, ssmm0-data-related configuration is configured in the ssmm0-data project, and ssmm-userManagement-related configuration is configured in the ssmm0-userManagement)
- Common configuration codes are extracted and aggregated.
Note: focus on the first point !!!
Test:
To test the project after splitting the configuration file, you must compile the ssmm0 project "clean compile" (see chapter 1 ),Then change the env of the ssmm0-data project to dev(See Chapter 3), otherwise using the default prod configuration will cause the database connection to fail and then run the project ssmm0-userManagement.