maven下多环境配置文件、属性管理 & Spring使用@value初始化字段值

说明一下,很多项目把配置文件与项目偶合在一起,比如与第三方的各种私密配置信息都与项目耦合在一起,导致什么结果,任何一个该项目的开发人员都能知道生产环境的各种配置,而且开发人员离职后一般都会把项目copy在自己的硬盘上,各种私密的配置信息很容易泄露。

好的架构,会把配置文件从项目中解耦,配置文件由各自不同的人员维护(开发环境有开发者维护,测试和生产由运维维护),生产的必须有运维专业人员操作和读取。做法有很多,不就是要读取指定位置的配置文件么。可以这么做,一个专门管理配置文件的插件config,config对外提供api,api使用@Component,一个配置文件定义一个javabean,在项目启动初始化的时候xml转java,项目中其它位置想读取配置时引用config插件。配置文件可以放在任何地方,远程或本地,可能放在服务器(tomcat)所在根目录的一个位置/var/项目名称/config/。

好开始正题
这是一个maven项目
使用spring的@value注入属性,@value 指定的值在env.properties中
方法一:属性配置文件的全量替换,使用maven的copy命令
建议先看方法二

// step 1 这样一个类
@Service("commonService")
public class CommonService implements org.springframework.beans.factory.InitializingBean{
    @Value("${account.source}")
    private String source;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化完成");
    }
}
// step 2 
//src/main/resources下有一个配置文件env.properties,文件内容如下
account.source=123456
//step 3
/*
src/main/filters 下有3个配置文件dev.properties,uat.properties,prd.properties,文件内容都是
account.source=wqeir231234
*/

maven自带属性 http://www.xuebuyuan.com/2038385.html

//step 4
//项目的pom.xml中有 主要使用了copy命令-文件全量copy
<profiles>
        <profile>
            <id>localid>
            <properties>
                <env>localenv>
                <env.overwrite>falseenv.overwrite>
            properties>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
        profile>
        <profile>
            <id>devid>
            <properties>
                <env>devenv>
                <env.overwrite>trueenv.overwrite>
            properties>
        profile>
        <profile>
            <id>uatid>
            <properties>
                <env>uatenv>
                <env.overwrite>trueenv.overwrite>
            properties>
        profile>
        <profile>
            <id>prdid>
            <properties>
                <env>prdenv>
                <env.overwrite>trueenv.overwrite>
            properties>
        profile>
    profiles>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-pluginartifactId>
                <version>1.8version>
                <executions>
                    <execution>
                        
                        <phase>prepare-packagephase>
                        <goals>
                            <goal>rungoal>
                        goals>
                        <configuration>
                            
                            
                            

                            <target name="env-target" if="${env.overwrite}">
                                
                                <echo message="开始拷贝${env}环境配置文件:从${basedir}/src/main/filters/${env}.properties 拷贝到 ${basedir}/target/classes/env.properties" />
                                <copy file="${basedir}/src/main/filters/${env}.properties" tofile="${basedir}/target/classes/env.properties" overwrite="true" force="true"/>
                            target>
                        configuration>
                    execution>
                executions>
            plugin>      
        plugins>
    build>

方法一完成

方法二

// step 1 这样一个类
@Service("commonService")
public class CommonService implements org.springframework.beans.factory.InitializingBean{
    @Value("${account.source}")
    private String source;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化完成");
    }
}
// step 2 这儿所有变量都使用${变量名}作为占位符,maven在构建的时候替换为真正的值
//src/main/resources目录或子目录下有多个配置文件
//文件一 env.properties,文件内容如下
account.source=${account.source}
/*
文件二 src/main/resources/spring下有applicationContext.xml、applicationContext-shiro.xml、spring-mvc.xml等,其中内容分别是
*/
jdbcUrl=${jdbcUrl}
shiro_loginUrl=${shiroLoginUrl}
cache=${cache}
//step 3
/*
src/main/filters 下有3个配置文件dev.properties,uat.properties,product.properties,文件内容都是
account.source=wqeir231234
jdbcUrl=www.baidu.com/mysql
shiro_loginUrl=www.baidu.com/login
cache=www.baidu.com/cache
*/
//step 4 pom.xml主要内容与方法一的不同

 
<profiles>
    <profile>
        <id>devid>
        
        <properties>
            <env>devenv>
        properties>
        <activation>
            <activeByDefault>trueactiveByDefault>
        activation>
    profile>
    <profile>
        <id>sitid>
        
        <properties>
            <env>sitenv>
        properties>
    profile>
    <profile>
        <id>uatid>
        
        <properties>
            <env>uatenv>
        properties>
    profile>
    <profile>
        <id>productid>
        
        <properties>
            <env>productenv>
        properties>
    profile>
profiles>

<build>
    
    <sourceDirectory>src/main/javasourceDirectory>
    
    
    <filters>
        <filter>src/main/filters/${env}.propertiesfilter>
    filters>
    <resources>
        
        <resource>
            
            <directory>src/main/resourcesdirectory>
            
            <includes>
                <include>**/*include>
            includes>
        resource>
        
        <resource>
            <directory>src/main/resourcesdirectory>
            
            <includes>
                <include>**/*.xmlinclude>
                <include>**/*.propertiesinclude>
            includes>
            
            <filtering>truefiltering>
        resource>
    resources>
    
    <outputDirectory>src/main/webapp/WEB-INF/classesoutputDirectory>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jettygroupId>
            <artifactId>maven-jetty-pluginartifactId>
            <version>6.1.26version>
            <configuration>
                <connectors>
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                        <port>80port>
                        <maxIdleTime>60000maxIdleTime>
                    connector>
                connectors>
                <webAppConfig>
                    <defaultsDescriptor>src/main/resources/webdefault.xmldefaultsDescriptor>
                webAppConfig>
                <stopPort>9966stopPort>
                <stopKey>jetty-stopstopKey>
                <scanIntervalSeconds>10scanIntervalSeconds>
            configuration>
        plugin>
    plugins>
build>

完成
eclipse中执行maven构建命令
右击工程,选择”Debug As”或”Run As”,再选择”Maven build”
在Goals中输入compile
Profiles输入dev或sit或product
maven常用命令http://blog.csdn.net/u011939453/article/details/43017865

你可能感兴趣的:(maven,spring,配置文件)