maven profile 结合spirng注解使用——实现配置文件最小化
来源:互联网 发布:stm8软件复位 编辑:程序博客网 时间:2024/06/01 09:30
maven profile 结合spirng注解使用——实现配置文件最小化
上一篇文章说的是如何使用maven的profile实现不同环境读取不同的配置(http://blog.csdn.net/wild46cat/article/details/72630678)。
本篇文章就来介绍如何在使用了maven的profile之后,再使用spring来实现对配置文件进行方便的访问。
好,下面上货。
首先新建一个maven项目:
mvn archetype:generate -DarchetypeCatalog=internal
然后修改pom文件
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <springframework.version>4.3.7.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${springframework.version}</version> </dependency> </dependencies> <profiles> <profile> <id>dev</id> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>test</id> <properties> <db.driver>com.oracle.jdbc.Driver</db.driver> </properties> </profile> </profiles> <build> <resources> <!--配置文件路径--> <resource> <directory>src/main/resource</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.xueyou.demo.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
看一下当前目录结构的格式:
App.java
package com.xueyou.demo;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;/** * Hello world! */@Configuration@ComponentScan(basePackages = {"com.xueyou.demo"})@PropertySource(value = {"classpath:appConfig.properties"})public class App { public static void main(String[] args) { System.out.println("Hello World!"); ApplicationContext applicationContext = new AnnotationConfigApplicationContext(com.xueyou.demo.App.class); AppConfig appConfig = applicationContext.getBean(AppConfig.class); System.out.println(appConfig.getAppName()); System.out.println(appConfig.getDriverName()); }}
AppConfig.java
package com.xueyou.demo;import org.springframework.beans.factory.annotation.Value;import org.springframework.beans.factory.config.ConfigurableBeanFactory;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)public final class AppConfig { @Value("${app.name}") private String appName; @Value("${app.driver}") private String driverName; public String getAppName() { return appName; } public String getDriverName() { return driverName; }}
AppConfig.properties
app.driver=${db.driver}app.encoding=${project.build.sourceEncoding}app.name=APPNAME
首先我们需要选择的是profile的激活状态,是dev激活
运行后结果:
切换profile的激活模式,切换成test。
再次运行,查看结果:
总结:
能够从运行结果中看出,当改变profile的激活模式的时候,能够在程序中对配置文件中的值进行动态构建。
阅读全文
0 0
- maven profile 结合spirng注解使用——实现配置文件最小化
- maven profile 结合spirng注解使用——实现配置文件最小化(二)
- Maven profile结合Maven AntRun Plugin实现不同环境配置文件分离打包
- Maven与Spring profile的结合使用
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件(POM中)
- 使用maven profile指定配置文件打包适用多环境
- 使用maven profile 实现不同环境打包
- 使用maven profile实现多环境配置
- maven 使用maven profile实现多环境可移植构建
- Maven提高篇系列之四——使用Profile
- Maven提高篇系列之四——使用Profile
- Maven提高篇系列之四——使用Profile
- Maven提高篇系列之四——使用Profile
- Maven提高篇系列之——使用Profile
- JAVA Calendar详解
- 如何在IDEA下输入命令行参数
- bzoj 2333: [SCOI2011]棘手的操作 离线+线段树
- Spring MVC web.xml配置
- 发财致富尽在现货黄金白银交易
- maven profile 结合spirng注解使用——实现配置文件最小化
- 替换空格
- 新闻客户端
- datagrid java 程序员入门
- linux shell 将多行文件转换为一行
- 【Bzoj3522】Hotel
- 单元测试(二)-桩对象
- 序列式容器
- 第一篇博客