Spring Boot IoC(六)使用properties配置文件

版权声明:From Lay https://blog.csdn.net/Sadlay/article/details/83277158

上一篇 Spring Boot IoC(五)Bean生命周期

六、使用properties配置文件

为了使用application.properties文件,需要添加属性文件依赖

添加依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

配置属性

使用@Value

test.driverName=drivername
test.url=url
test.username=username
test.password=password

新增一个使用配置的测试类DataBaseProperties,使用@Value注解配合SpringEl表达式来从配置文件中读取信息为属性注入

@Component
public class DataBaseProperties {

	@Value("${test.driverName}")
	private String driverName = null;

	@Value("${test.url}")
	private String url = null;

	private String username = null;

	private String password = null;

	public void setDriverName(String driverName) {
		System.out.println(driverName);
		this.driverName = driverName;
	}

	public void setUrl(String url) {
		System.out.println(url);
		this.url = url;
	}

	@Value("${test.username}")
	public void setUsername(String username) {
		System.out.println(username);
		this.username = username;
	}

	@Value("${test.password}")
	public void setPassword( String password) {
		System.out.println(password);
		this.password = password;
	}

	public String getDriverName() {
		return driverName;
	}

	public String getUrl() {
		return url;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}
}

输出

drivername
url
username
password

使用@CongigurationProperties

我们可以通过@ConfigurationProperties注解使配置减少

@Component
@ConfigurationProperties("test")
public class DataBasePropertiesTwo{

	private String driverName = null;

	private String url = null;

	private String username = null;

	private String password = null;

	public void setDriverName(String driverName) {
		System.out.println(driverName);
		this.driverName = driverName;
	}

	public void setUrl(String url) {
		System.out.println(url);
		this.url = url;
	}

	public void setUsername(String username) {
		System.out.println(username);
		this.username = username;
	}

	public void setPassword( String password) {
		System.out.println(password);
		this.password = password;
	}

	public String getDriverName() {
		return driverName;
	}

	public String getUrl() {
		return url;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}
}

注解@ConfigurationProperties中配置字符串test,将与POJO的属性名称组成属性的全限定名去配置文件里查找,这样就能够将对应的属性读入到POJO中。

使用新的配置文件名

为了更好的配置,我们可以选择使用新的属性文件。例如数据库的可以配置在test.properties文件中。

我们使用@PropertySource去定义对应的属性文件,把它加载到Spring的上下文中。

自定义配置文件test.properties

test1.driverName=drivername11
test1.url=url11
test1.username=username11
test1.password=password11

启动类加注解@PropertySource

@SpringBootApplication
@PropertySource(value= {"classpath:test.properties"},ignoreResourceNotFound=true)

value:可以配置多个文件。使用classpath前缀,意思是去类文件路径下找到属性文件。

扫描二维码关注公众号,回复: 3902355 查看本文章

ignoreResourceNotFound:是否忽略配置文件找不到的问题,默认为false。没找到就会报错。

下一篇 Spring Boot IoC(七)条件装配Bean

猜你喜欢

转载自blog.csdn.net/Sadlay/article/details/83277158