maven下多环境配置文件、属性管理 & Spring使用@value初始化字段值
2017-09-08 11:32
861 查看
说明一下,很多项目把配置文件与项目偶合在一起,比如与第三方的各种私密配置信息都与项目耦合在一起,导致什么结果,任何一个该项目的开发人员都能知道生产环境的各种配置,而且开发人员离职后一般都会把项目copy在自己的硬盘上,各种私密的配置信息很容易泄露。
好的架构,会把配置文件从项目中解耦,配置文件由各自不同的人员维护(开发环境有开发者维护,测试和生产由运维维护),生产的必须有运维专业人员操作和读取。做法有很多,不就是要读取指定位置的配置文件么。可以这么做,一个专门管理配置文件的插件config,config对外提供api,api使用@Component,一个配置文件定义一个javabean,在项目启动初始化的时候xml转java,项目中其它位置想读取配置时引用config插件。配置文件可以放在任何地方,远程或本地,可能放在服务器(tomcat)所在根目录的一个位置/var/项目名称/config/。
好开始正题
这是一个maven项目
使用spring的@value注入属性,@value 指定的值在env.properties中
方法一:属性配置文件的全量替换,使用maven的copy命令
建议先看方法二
maven自带属性 http://www.xuebuyuan.com/2038385.html
方法一完成
方法二
完成
eclipse中执行maven构建命令
右击工程,选择”Debug As”或”Run As”,再选择”Maven build”
在Goals中输入compile
Profiles输入dev或sit或product
maven常用命令http://blog.csdn.net/u011939453/article/details/43017865
好的架构,会把配置文件从项目中解耦,配置文件由各自不同的人员维护(开发环境有开发者维护,测试和生产由运维维护),生产的必须有运维专业人员操作和读取。做法有很多,不就是要读取指定位置的配置文件么。可以这么做,一个专门管理配置文件的插件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>local</id> <properties> <env>local</env> <env.overwrite>false</env.overwrite> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>dev</id> <properties> <env>dev</env> <env.overwrite>true</env.overwrite> </properties> </profile> <profile> <id>uat</id> <properties> <env>uat</env> <env.overwrite>true</env.overwrite> </properties> </profile> <profile> <id>prd</id> <properties> <env>prd</env> <env.overwrite>true</env.overwrite> </properties> </profile> </profiles> <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <!--<phase>generate-sources</phase>--> <phase>prepare-package</phase> <goals> <goal>run</goal> </goals> <configuration> <!--<target >--> <!--<echo message="开始拷贝${env}环境配置文件" />--> <!--</target>--> <target name="env-target" if="${env.overwrite}"> <!--<delete file="${basedir}/target/classes/env.properties" verbose="true" deleteonexit="true"/>--> <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主要内容与方法一的不同 <!-- 在maven构建的时候 选择其中一个<id>xx</id>,不选择则使用默认的,由<activeByDefault>true</activeByDefault>指定 --> <!-- 使用<properties>可以在pom中随便自定义变量 --> <profiles> <profile> <id>dev</id> <!-- 自定义变量 env 值为dev --> <properties> <env>dev</env> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>sit</id> <!-- 自定义变量 sit 值为sit --> <properties> <env>sit</env> </properties> </profile> <profile> <id>uat</id> <!-- 自定义变量 uat 值为uat --> <properties> <env>uat</env> </properties> </profile> <profile> <id>product</id> <!-- 自定义变量 product 值为product --> <properties> <env>product</env> </properties> </profile> </profiles> <build> <!-- java源文件 ,标准的maven项目目录包含src/mainjava src/main/resource src/main/filters src/main/test--> <sourceDirectory>src/main/java</sourceDirectory> <!-- 多环境配置文件 ${env} env是上面自定义的变量 --> <!-- 取这一个环境的配置文件作为过滤文件--> <filters> <filter>src/main/filters/${env}.properties</filter> </filters> <resources> <!-- 定义资源 --> <resource> <!-- 指定资源所在目录(配置文件,比如spring/springmvc等等)文件目录 --> <directory>src/main/resources</directory> <!-- 包括配置文件下所有的子目录 --> <includes> <include>**/*</include> </includes> </resource> <!-- 定义资源二 --> <resource> <directory>src/main/resources</directory> <!-- 这个资源包括所有目录的xml文件和properties文件 --> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <!-- 对这个资源包含的文件使用上面的过滤文件过滤 把这个${xxx}替换为真实的值 --> <filtering>true</filtering> </resource> </resources> <!-- 构建完成文件输出位置--> <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.26</version> <configuration> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>80</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <webAppConfig> <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor> </webAppConfig> <stopPort>9966</stopPort> <stopKey>jetty-stop</stopKey> <scanIntervalSeconds>10</scanIntervalSeconds> </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
相关文章推荐
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring4深入理解IOC&DI02----Bean配置--自动装配、bean之间的继承与依赖、使用外部属性文件
- Springboot使用maven的profiles实现多环境配置文件切换
- spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- 使用maven管理不同环境的配置文件
- 基于maven管理的项目中spring+mybatis配置数据源读取不到properties属性文件的问题
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- 使用 MAVEN 进行不同环境的服务配置文件管理
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- 使用maven管理spring3.1.2 及spring mvc ,hibernate4.3.6集成开发的依赖环境
- Spring学习笔记(18)----使用Spring配置文件实现事务管理
- Spring使用环境变量控制配置文件加载
- jbpm使用配置(spring+hibernate+maven项目环境)
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- Spring整合Hibernate开发 - 使用事务管理器操作数据库 - 主配置文件