2、获取配置文件中的属性
来源:互联网 发布:淘宝 产品对比 违规 编辑:程序博客网 时间:2024/06/06 03:22
spring boot的工程启动的时候,内部文件默认是加载classpath路径或者classpath:/config目录下的application.properties文件的。当然也可以指定加载其它的配置文件,如何获取配置文件中的属性呢?实例如下:
【1】、获取默认classpath下的application.properties配置文件中的属性
application.properties中的内容为:
local.ip=127.0.0.1
一、获取classpath下的application.properties文件中属性
1、直接通过配置文件的环境变量获取
启动类:
@SpringBootApplicationpublic class App { public static void main(String[] args) { ConfigurableApplicationContext app = SpringApplication.run(App.class, args); /*通过getEnvironment函数默认获取到的文件即为application.properties,然后用getProperty函数从文件中获取键的属性*/ System.out.println(app.getEnvironment().getProperty("local.ip"));}
或者创建一个类,用来获取配置属性,例如
package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.core.env.Environment;import org.springframework.stereotype.Component;/*注意:由于@SpringBootApplication中有一个注解@ComponentScan自动扫描启动类所在的包,而PropertiesConfig正好在启动包下,如果 * 该类不与启动类在同一包下,需要该类加载到spring boot的启动源中*/@Componentpublic class PropertiesConfig { @Autowired private Environment env; public void show(){ System.out.println(env.getProperty("local.ip")); }}
启动类中的方法为:
ConfigurableApplicationContext app = SpringApplication.run(App.class, args); app.getBean(PropertiesConfig.class).show();
当运行启动类的时候,输出如下:
127.0.0.1
2、用@Value注解来获取
新建一个配置类,专门用来获取application.properties文件中的属性。
package com.example.demo;import import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.stereotype.Component;/*注意:由于@SpringBootApplication中有一个注解@ComponentScan自动扫描启动类所在的包,而PropertiesConfig正好在启动包下,如果 * 该类不与启动类在同一包下,需要该类加载到spring boot的启动源中*/@Componentpublic class PropertiesConfig { /*通过@Value注解会默认把application.properties中的local.ip键对应的属性自动赋给localIp属性*/ @Value("${local.ip}") private String localIp; public void show(){ System.out.println("localIp=" + localIp); }}
启动类为:
ConfigurableApplicationContext app = SpringApplication.run(App.class, args); app.getBean(PropertiesConfig.class).show();
当运行启动类的时候,输出如下:
127.0.0.1
【2】、获取自定义的properties配置文件属性
当在工程目录下自定义了配置文件,例如在/src/main/resources/
下的/fileConfig
的目录下创建一个jdbc.properties文件,或者在服务器中非工程目录下D:\tmp
创建一个jdbc1.properties配置文件。 如果启动spring boot工程的时候,系统默认是没有加载该配置文件的,因此在系统环境中获取不到该配置文件,若想获取到该配置文件中的属性,可以通过下面两种方式进行获取:在启动的参数中指定启动自定义配置文件;在配置类上指定要加载的自定义配置文件。
自定义jdbc.properties配置文件:
jdbc.username=lzjjdbc.password=123456
自定义jdbc1.properties配置文件:
jdbc.username1=lzjjdbc.password1=123456jdbc.url=jdbc:mysql://localhost:3306/databasename
一、在启动spring boot的参数中进行指定
run as -> run configurations -> arguments
在program arguments框中填入下列参数:
--spring.config.location=classpath:/fileConfig/jdbc.properties, file:D:/tmp/jdbc1.properties
如果配置文件直接在classpath或者classpath下的config目录下,例如app.properties,也可以直接如下配置:
--spring.config.name=app
如按上面配置后,spring boot启动的时候会自动加载指定的文件,下面获取自定义文件中的属性就和获取默认classpath下的application.properties配置文件中的属性的方法完全一致,请参考上面获取方式。
二、在配置类上指定要加载的自定义配置文件
1、例如还是要获取classpath下的/fileConfig/jdbc.properties和D:/tmp/jdbc1.properties的自定义配置文件属性,现在不需要在启动的参数中进行指定了,顶一个配置类,在配置类中进行指定加载的自定义配置文件。
配置类ConfigTest :
package com.example.demo;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.context.annotation.PropertySources;@Configuration@PropertySource("classpath:/fileConfig/jdbc.properties")@PropertySource("file:D:/tmp/jdbc1.properties")/*或者按如下配置:@PropertySources({@PropertySource("classpath:/fileConfig/jdbc.properties"), @PropertySource("file:D:/tmp/jdbc.properties")})*/public class ConfigTest {}
配置完要加载的配置文件后,下面就是要获取自定义的配置文件中的属性了。
创建获取classpath:/fileConfig/jdbc.properties的bean类JdbcTest:
package com.example.demo;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class JdbcTest { @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; public void show(){ System.out.println("username=" + username); System.out.println("password=" + password); }}
创建获取D:/tmp/jdbc1.properties的bean的类JdbcFile :
package com.example.demo;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class JdbcFile { @Value("${jdbc.username1}") private String username; @Value("${jdbc.password1}") private String password; @Value("${jdbc.url}") private String url; public void show(){ System.out.println("username=" + username); System.out.println("password=" + password); System.out.println("url=" + url); }}
启动类如下:
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublic class App { public static void main(String[] args) { ConfigurableApplicationContext app = SpringApplication.run(App.class, args); app.getBean(JdbcTest.class).show(); app.getBean(JdbcFile.class).show(); }}
输出结果为:
username=lzjpassword=123456username=zhangsanpassword=654321url=jdbc:mysql://localhost:3306/databasename
2、上面一种方式通过配置类加载自定义配置文件后,然后通过@value注解从配置类中获取键值。下面一种方式也是先通过配置类加载自定义的配置文件,但是通过@ConfigurationProperties注解自动把配置文件中的键对应的值注入到相应的属性中。例如:
配置类ConfigTest 同上:
package com.example.demo;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.context.annotation.PropertySources;@Configuration@PropertySource("classpath:/fileConfig/jdbc.properties")@PropertySource("file:D:/tmp/jdbc1.properties")/*或者按如下配置:@PropertySources({@PropertySource("classpath:/fileConfig/jdbc.properties"), @PropertySource("file:D:/tmp/jdbc.properties")})*/public class ConfigTest {}
下面创建一个类SourceProperties,用来获取自定义配置文件中的键值,类中的属性一定要和自定义配置文件中的键的名字保持一致,否则不能把配置文件中键对应的值注入到类对象的属性中。在上面的配置类中加载了classpath:/fileConfig/jdbc.properties和file:D:/tmp/jdbc1.properties两个自定义的配置文件,在SourceProperties类中只需要定义两个配置文件中包含的键为类对象的属性,然后通过set方法把配置文件中键对应的值注入到类对象的属性中就可以了。
package com.example.demo;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component/*prefix可以指定配置文件中键的前缀。例如配置文件中有一个键为jdbc.username,配置了prefix="jdbc"后,在下面的属性中只需要定义username就可以了。在值注入的时候,就会自动把配置文件中的jdbc.username对应的值自动注入到username属性中*/@ConfigurationProperties(prefix="jdbc")public class SourceProperties { /*username对应jdbc.properties文件中的jdbc.username键*/ private String username; /*username1对应jdbc1.properties文件中的jdbc.username1键*/ private String username1; /*password对应jdbc.properties文件中的jdbc.password键*/ private String password; /*password1对应jdbc1.properties文件中的jdbc.password1键*/ private String password1; /*url对应jdbc1.properties文件中的jdbc.url键*/ private String url; /*一定要给需要注入的属性设置get方法,否则值注入不进来*/ public void setUsername(String username) { this.username = username; } public void setUsername1(String username1) { this.username1 = username1; } public void setPassword(String password) { this.password = password; } public void setPassword1(String password1) { this.password1 = password1; } public void setUrl(String url) { this.url = url; } public void show(){ System.out.println("---username=" + username); System.out.println("---username1=" + username1); System.out.println("---password=" + password); System.out.println("---password=" + password1); System.out.println("---url=" + url); }}
启动类为:
@SpringBootApplicationpublic class App { public static void main(String[] args) { ConfigurableApplicationContext app = SpringApplication.run(App.class, args); app.getBean(SourceProperties.class).show(); }}
运行启动类,输出结果为:
---username=lzj---username1=zhangsan---password=123456---password=654321---url=jdbc:mysql://localhost:3306/databasename
可见完全取到了自定义配置文件中的键值。
【3】、另外,配置文件中还可以定义变量,可以为配置文件中没有定义的键赋一个默认的值
1、在配置文件中定义变量
下面以spring boot默认加载的application.properties为例,在该配置文件中配置如下,先定义了一个name的键值对,然后又定义了一个local.name的键值对,但local.name中的键值对引用了name的键值对。
name=lzjlocal.name=hello ${name}
启动类为:
@SpringBootApplicationpublic class App { public static void main(String[] args) { ConfigurableApplicationContext app = SpringApplication.run(App.class, args); System.out.println(app.getEnvironment().getProperty("name")); System.out.println(app.getEnvironment().getProperty("local.name")); }}
运行启动类,输出结果为:
lzjhello lzj
可见,获得了配置文件中键对应的值。
2、为配置文件中没有定义的键默认赋一个值。
还是以spring boot默认加载的application.properties为例,其配置内容为:
local.ip=127.0.0.1
配置文件中只配置了local.ip,没有配置local.port,下面我们从配置文件中获取local.port,如果不存在的话,就默认赋一个值。创建一个PropertiesConfig 类,用来获取键值。
package com.example.demo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.core.env.Environment;import org.springframework.stereotype.Component;/*注意:由于@SpringBootApplication中有一个注解@ComponentScan自动扫描启动类所在的包,而PropertiesConfig正好在启动包下,如果 * 该类不与启动类在同一包下,需要该类加载到spring boot的启动源中*/@Componentpublic class PropertiesConfig { /*从配置文件取local.port对应的值,如果不存在,就默认赋一个8888的值。*/ @Value("${local.port:8888}") private Integer localPort; public void show(){ System.out.println("local.port=" + localPort); }}
启动类为:
@SpringBootApplicationpublic class App { public static void main(String[] args) { ConfigurableApplicationContext app = SpringApplication.run(App.class, args); app.getBean(PropertiesConfig.class).show(); }}
运行启动类,输出:
local.port=8888
- 2、获取配置文件中的属性
- Spring中获取外部配置文件中的属性值
- Java获取配置文件中的属性值通用类
- Vs2010中的属性配置文件.
- java工程如何读取配置文件并获取配置文件中的属性值
- 获取配置文件中的值
- Java获取配置文件的属性值通用工具类2
- log4j配置文件中的additivity属性
- log4j配置文件中的additivity属性
- ibatis 配置文件中的<settings>属性
- hibernate配置文件中的catalog属性
- log4j配置文件中的additivity属性
- Hibernate配置文件中的各种属性
- log4j配置文件中的additivity属性
- Mybatis配置文件中的mapUnderscoreToCamelCase属性
- log4j配置文件中的additivity属性
- Java项目和maven项目中如何获取&设置配置文件中的属性
- 获取 .properties 配置文件中的值
- HDOJ 1016 DFS
- SSM
- 桥接模式
- [BZOJ1316]点分治关于定长问题的讨论
- 解决Maven项目中令人头痛的无错误但是有小红叉问题
- 2、获取配置文件中的属性
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- SIGHUP信号与控制终端
- tftp使用
- java如何通过类加载器加载类和jar包
- CSS3动画效果加上简单@media响应式布局 week6学习
- luogu P1044 栈
- Android -- Vold机制简要分析
- 1.docker简单介绍/centOS安装