1.開發的時候經常需要加載不同的環境,比如本地開發環境dev,生產環境product。如果需要手動去修改的話就太麻煩了,自己實現了maven資源替換,然后多環境下的配置文件管理的demo,在此貼出來。
2.實現需求:
根據本地or開發配置文件,加載不同的配置,如果使用本地數據庫demodb,zhangsan,123456才能登錄成功;
如果使用生產環境數據庫productdb,wangwu,123455才能登錄成功;
代碼如下:
1.父項目myssm-pom下兩套配置,dev.properties為本地開發配置,product.properties為生產環境配置;
dev.properties:
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/demodb username=root password=root
product.properties:
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/productdb username=root password=root
2.myssm-pom.xml配置:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cy.myssm</groupId> <artifactId>myssm-pom</artifactId> <version>1.0.0</version> <modules> <module>../myssm-web</module> <module>../myssm-service</module> <module>../myssm-dao</module> <module>../myssm-support</module> </modules> <packaging>pom</packaging> <!-- 環境配置 --> <profiles> <!-- 本地環境 --> <profile> <id>dev</id> <activation> <!-- 默認激活dev --> <activeByDefault>true</activeByDefault> </activation> <build> <!-- 指定當前profile環境下,屬性文件路徑 --> <filters> <filter>../${project.parent.artifactId}/properties/dev.properties</filter> </filters> </build> </profile> <!-- 生產環境 --> <profile> <id>product</id> <build> <filters> <filter>../${project.parent.artifactId}/properties/product.properties</filter> </filters> </build> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <useDefaultDelimiters>false</useDefaultDelimiters> <delimiters> <delimiter>$[*]</delimiter> </delimiters> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <resources> <resource> <!-- 要替換屬性的文件目錄 --> <directory>${basedir}/src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>
說明:
1) profile下面標簽相關概念:
id:profile的標示
activeByDefault: 我們可以在profile中的activation元素中指定激活條件,當沒有指定條件,然后指定activeByDefault為true的時候就表示當沒有指定其他profile為激活狀態時,該 profile就默認會被激活。所以當我們調用mvn package的時候上面的dev將會被激活,但是當我們使用mvn package –P product的時候將激活product,而這個時候 dev將不會被激活。
filters:比較重要,指定當前profile環境下,屬性文件路徑;
2)resouces標簽下面屬性:
directory: 配置那個目錄下的文件通過${key} (el表達式,默認的,但是我這里改成$[]了)會被替換成屬性值,resource目錄下,我們一般放jdbc連接,或配置文件;
includes: 指定那個目錄下那個文件
filtering: 這個配置的意思是過濾上面指定屬性文件中的占位符,占位符是${變量名稱}這樣的形式,maven會自動讀取配置文件,然后解析其中的占位符,使用上面pom文件中定義 的屬性進行替換
exclueds: 在resource目錄下,有很多文件,但用些文件不希望替換,則可以通過<excluede>指定
filters: 這里的filters與<profile>的filter意思一樣,都是指屬性文件地址,這個如果上面定義<profile>的時候指定了,這里就不需要了,但有些開發習慣是在<profile>不定義,然后在 <build>里指定。
3) maven-resources-plugin
useDefaultDelimiters不使用默認的分割,默認的是將${}即el表達式替換掉;我這里將所有basedir下面的src/main/resources下面的文件中,只要含有$[key]的表達式替換成myssm-pom下面已經配置好的開發/生產配置;
4) datasource.properties:
driverClassName=$[driverClassName] url=$[url] username=$[username] password=$[password]
3.sprint-context.xm配置:

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 自動掃描 --> <context:component-scan base-package="com.cy" /> <import resource="spring-mq.xml"/> <mvc:annotation-driven/> <!-- 屬性文件加載 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties/datasource.properties</value> </list> </property> <property name="placeholderPrefix" value="@{" /> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" /> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="@{driverClassName}"/> <property name="url" value="@{url}"/> <property name="username" value="@{username}"/> <property name="password" value="@{password}"/> </bean> <!-- 配置mybatis的sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mappers.xml文件 --> <property name="mapperLocations" value="classpath:mappers/*.xml"></property> <!-- mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cy.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
為了方便,將整段配置貼下來了;
說明:
1).使用PropertyPlaceholderConfigurer加載屬性配置文件datasource.properties,但是這里使用前綴@{來引用屬性配置,而默認是${}即el表達式;
2).systemPropertiesModeName的選擇有三種:
SYSTEM_PROPERTIES_MODE_FALLBACK: 如果給定的配置文件沒有該屬性,檢查系統配置文件;
SYSTEM_PROPERTIES_MODE_NEVER: 從不檢查系統屬性
SYSTEM_PROPERTIES_MODE_OVERRIDE: 先檢查系統配置文件,如果沒有該屬性,再檢查給定的配置文件;
3).現在就可以使用@{key}來引用datasource.properties中的屬性配置啦;比如上面dataSource中的driverClassName,url等;
4.測試代碼:

package com.cy.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import com.cy.entity.User; import com.cy.service.UserService; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/user") public class UserController { private static Logger logger = Logger.getLogger(UserController.class); @Autowired private UserService userService; @Value("@{driverClassName}") private String driverClassName; @Value("@{url}") private String url; @Value("@{username}") private String username; @Value("@{password}") private String password; /** * 用戶登錄 * @param user * @param request * @return */ @RequestMapping("/login") public String login(User user, HttpServletRequest request){ logger.info("login入參:username:"+user.getUsername()+",passowrd:"+user.getPassword()); User resultUser=userService.login(user); if(resultUser==null){ request.setAttribute("user", user); request.setAttribute("errorMsg", "用戶名或密碼錯誤!"); return "index"; }else{ System.err.println("username:" +user.getUsername() +"登錄成功!"); HttpSession session=request.getSession(); session.setAttribute("currentUser", resultUser); return "redirect:/success.jsp"; } } /** * 測試@Value注解 注入的值是否生效 */ @RequestMapping("/testAnnotationValue") public void testAnnotationValue(){ logger.info("driverClassName:" +driverClassName); logger.info("url:" +url); logger.info("username:" +username); logger.info("password:" +password); } }
1)idea中maven projects工具欄中,如果使用默認dev profile,啟動項目
zhangsan能登錄,console打印;
2018-08-23 20:50:19,237 [http-bio-8888-exec-3] INFO [com.cy.controller.UserController] - driverClassName:com.mysql.jdbc.Driver 2018-08-23 20:50:19,237 [http-bio-8888-exec-3] INFO [com.cy.controller.UserController] - url:jdbc:mysql://localhost:3306/demodb 2018-08-23 20:50:19,237 [http-bio-8888-exec-3] INFO [com.cy.controller.UserController] - username:root 2018-08-23 20:50:19,237 [http-bio-8888-exec-3] INFO [com.cy.controller.UserController] - password:root
2) 如果勾選product,maven就使用product的配置, 這時候使用的是productdb,只有wangwu才能登錄了。