Spring Boot配置文件(二)

自动配置:
    Spring Boot 提供了默认的配置,如默认的 Bean ,去运行 Spring 应用。它是非侵入式的,只提供一个默认实现。
 大多数情况下,自动配置的 Bean 满足了现有的业务场景,但如果自动配置做的不够好,还需要覆盖配置。

Spring Boot 提供了对应用进行自动化配置。
    会根据类路径中的jar包、类,为jar包里的类自动配置,这样可以极大的减少配置的数量
    相比以前 XML 配置方式,很多显式方式是不需要的。大多数默认的配置足够实现开发功能,从而更快速开发。

Spring Boot 不仅从 application.properties 获取配置,还可以在程序中多种设置配置属性。按照以下列表的优先级排列:
 1.命令行参数
 2.java:comp/env 里的 JNDI 属性
 3.JVM 系统属性
 4.操作系统环境变量
 5.RandomValuePropertySource 属性类生成的 random.* 属性
 6.应用以外的 application.properties(或 yml)文件
 7.打包在应用内的 application.properties(或 yml)文件
 8.在应用 @Configuration 配置类中,用 @PropertySource 注解声明的属性文件
 9.SpringApplication.setDefaultProperties 声明的默认属性
 可见,命令行参数优先级最高.可以在测试或生产环境中快速地修改配置参数值,而不需要重新打包和部署应用。
 自定义属性:
 工程如下:
   
 maven加载的包:
	<!-- Spring Boot 启动父依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.1.RELEASE</version>
	</parent>

	<dependencies>
		<!-- Spring Boot Web 依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- Spring Boot Test 依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- Junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-test</artifactId>
			<version>1.4.2.RELEASE</version>
			<scope>test</scope>
		</dependency>


	</dependencies>

 (1)属性文件
  application-dev.properties 开发环境 (以Key-Value的方式)
##\u5B66\u751F\u65B0 dev
student.province=zhangsan
student.city=HangZhou
student.desc=dev\: I'm living in ${student.province} ${student.city}
   application-prod.properties 生产环境
##\u5B66\u751F\u65B0 
student.province=\u5F20\u4E09
student.city=BeiJin
student.desc=dev\: I'm living in ${student.province} ${student.city}
   application.properties (多环境配置)
##Spring Profiles Active
spring.profiles.active=dev
#spring.profiles.active=prod
说明: spring.profiles.active 属性指定加载哪个属性文件; 

(2)实体类
//用 @Component 作为 Bean 注入到 Spring 容器中。
@Component
@ConfigurationProperties(prefix="student")//将配置文件中以 student 前缀的属性值自动绑定到对应的字段中。
//@ConfigurationProperties(prefix="home") //改成home来调用这个配置文件application.yml
public class StudentBean {
      //setXXX and getXXX
	private String province; // 省份
	private String city; // 城市
	private String desc; // 描述

	@Override
	public String toString() {
		return "StudentBean [province=" + province + ", city=" + city
				+ ", desc=" + desc + "]";
	}

(3)springBoot启动类
@SpringBootApplication
public class Application implements CommandLineRunner {
	
	//注入学生类
	@Autowired
	private StudentBean studentBean;

	/**
	 * 程序的入口点 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		SpringApplication.run(Application.class, args);
	}

	/**
	 *运行输出结果
	 */
	@Override
	public void run(String... arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(studentBean+"\n");

	}

}

(4)运行springboot!
  从控制台中可以看出,是加载了 application-dev.properties 的属性输出:
 
 将 spring.profiles.active 设置成 prod,重新运行Application.java
 从控制台中可以看出,是加载了 application-prod.properties 的属性输出:

****************************************************************************************************************
  其他实体类应用:
@Component
@ConfigurationProperties(prefix="user")
public class UserBean {
	private Long id; // 编号
	private int age;// 年龄
	private String username;// 姓名
	private String uuid;// 用户 UUID
application.yml文件
说明: application.properties 配置中文值的时候,读取出来的属性值会出现乱码问题。但是 application.yml 不会出现乱码问题。
     原因是,Spring Boot 是以 iso-8859 的编码方式读取 application.properties 配置文件。
## 家属性
home:
  province: 江苏省
  city: 南京市
  desc: 我家住在${home.province}的${home.city}
  
## 随机属性
user:
  id: ${random.long}
  age: ${random.int[1,200]}
  username: 软件工程师是${random.value}
  uuid: ${random.uuid}
注意: 键值对冒号后面,必须空一格。
说明:Spring Boot 通过 RandomValuePropertySource 提供了很多关于随机数的工具类。可以生成随机字符串、随机 int 、随机 long、某范围的随机数。

测试类:(必须放在com.hlx.springboot.entity包下哦!)如工程图所示
/**
 * 自定义配置文件测试类
 * 
 * @author Administrator
 * 
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestProperties {

	// 获得日志对象
	private final Logger logger = LoggerFactory.getLogger(TestProperties.class);

	// 注入学生类
	@Autowired
	private StudentBean studentBean;

	// 注入用户类
	@Autowired
	private UserBean userBean;

	// 测试学生
	@Test
	public void testStudent() {
		logger.info("\n\n"+studentBean + "\n");
	}

	// 测试用户
	@Test
	public void testUser() {
		logger.info("\n\n"+userBean + "\n");
	}
}

运行效果:
 

多次运行,可以发现每次输出不同 User 属性值








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值