JAVAEE颠覆者,SpringBoot实战一书学习小记(SpringBoot常规属性配置,日志配置,profile配置)

SpringBoot常规属性配置


当年在Spring环境下,注入properties文件里值的方式,通过@PropertySource指明properties文件的位置,然后通过@Value注入值。在SpringBoot里,我们只需要在application.properties定义属性,然后直接注入@Value注入即可。

示例

author=sola
authorage=27

建立一个Bean

package com.cn.sola.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
public class Author {

	@Value("${author}")
	private String author;
	@Value("${authorage}")
	private String authorage;
	public String getAuthor() {
		return author;
	}
	public String getAuthorage() {
		return authorage;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public void setAuthorage(String authorage) {
		this.authorage = authorage;
	}
	
	
}

下面运行测试一下

package com.cn.sola;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import com.cn.sola.bean.Author;


@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootShiZhanApplicationTests {

	@Autowired
	private Author author;
	
	@Test
	public void contextLoads() {

		System.out.println(author.getAuthor()+":"+author.getAuthorage());
	}

}

类型安全的配置(基于properties)


上例中使用@Value注入每个配置在实际项目中会显的格外麻烦,因为实际配置通常会是许多个,若是用上例的方式则要使用@Value注入很多次。

Springboot还提供了基于类型安全的配置方式,通过@ConfigurationProperties将Properties属性和一个Bean及其属性关联,从而实现类型安全的配置。

添加配置 还是在application.properties上添加,名字与上面起的稍微不同

author.name=sola
author.age=27

此次是前缀一样

package com.cn.sola.bean;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="author")
public class Author {

	private String name;
	private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
}

这样用刚才的测试类也可以输出同样的结果,当然默认配置读取的是application.properties,也可以自己指定properties的位置

-----------------------------------------------------------------------------------------------------------------------------

SpringBoot支持JavaUtilLogging,Log4J,Log4J2和Logback作为日志框架,无论使用哪种日志框架,SpringBoot已为当前使用日志框架的控制台输出及文件输出做好了配置

默认情况下,SpringBoot使用Logback作为日志框架

配置日志

#log
logging.level.root=INFO
logging.level.org.springframework.web=ERROR
logging.level.org.hibernate=ERROR
#logging.path=/home/_xyy_/output/SpringBootDemo/logs  
logging.file=app.log

#logging.level.com.sola.com.sola.service.DeptService=error

profile配置

profile是Spring用来针对不同环境对不同配置提供支持的

全局profile配置使用application-{profile}.properties(如application-prod.properties)

通过在application.properties中设置spring.profiles.active=prod来指定活动的Profile.

下面做一个简单的演示,如我们分为生产(prod)和开发(dev)环境,生产环境端口为8888,开发环境下端口为9999.


dev为

server.port=9999

prod为

server.port=8888

application.properties为

#server.port=9982

spring.profiles.active=prod
#spring.profiles.active=dev
设定为哪个就会读取哪个的配置

猜你喜欢

转载自blog.csdn.net/jiulanhao/article/details/80972316