sprinboot 配置文件

在resources
*.properties
@configuration
@configurationProperties(prefix="com.*") /前缀
@prepertySourec (value ="classpath:") /文件地址
springboot配置文件的读取所引用的j a r

SpringBoot资源文件 配置
属性资源配置:



将资源文件中的属性配置与映射到实体类中
依赖
SpringBoot 映射配置文件
resource.properties
spring资源配置
resource.properties
把property的内容映射成pojo
pom添加配置文件属性 ,configuration-processor
devtools
不能返回成员变量的resource,必须new一个再返回
注入的三个宏
@Configuration
@ConfigurationProperties
@PropertySource
定义@Configuration宏表示会引用配置文件
https://github.com/leechenxiang/imooc-springboot-starter
@Configuration
@ConfigurationProperties(prefix="com.cf.resouce.config")//配置项的前缀
@PropertySource(value="classpath:resource.properties")//配置文件路径
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
资源文件属性配置映射到实体类
依赖:org.springframework.boot-spring-boot-configuration-processor
@Configuration
@ConfigurationProperties(。。。)
@PropertySource()
classpath:项目资源文件根路径
40%
SpringBoot资源文件属性配置
场景1——》资源文件中的属性配置与映射到实体类(然后就可以把这个实体类注入到Controller或Service里)
步骤1:pom文件中,引入configuration-processor依赖,完成资源文件的读取(属性name值为imooc,依次对应)。

步骤2:类上添加@Configuration注解,代表该类会引用资源文件、@ConfigurationProperties用来配置配置文件中的前缀的、@PropertySource代表资源文件的位置、如图

扩展1:classpath,项目打成war包后,只有WEB-INF、WETA-INF这两个包,而WEB-INF只有classes和lib这两个包,lib放置jar包,classes存放的是都是自己新创建的文件。
扩展2:BeanUtils的copyProperties(source,target)——》可以将source对象中属性值赋值给target属性,)前提这两个属性名相同,这两个对象可以是两个类的,也可以是一个类的。
扩展3:解决重启时,报端口占用问题
cmd模式下输入命令:
netstat -ano|findstr 8080,查看占用端口号的进程。
tasklist|findstr "端口号对应数字"结束进程。
taskkill /pid 进程号 -f查看是否成功

断点调试:打断点,可查看执行,F6向下执行,F8将方法返回结果在浏览器上进行显示。
springboot:资源文件属性配置可以通过注解映射到实体类,再将实体类注入到controller或者是service里去
springboot:资源文件属性配置可以通过注解映射到实体类,再将实体类注入到controller或者是service里去
首先,添加依赖
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
```
建resource.properties文件
前缀:com.imooc.opensource;属性:name,website,language
```
com.imooc.opensource.name=imooc
com.imooc.opensource.website=www.imooc.com
com.imooc.opensource.language=java
```
创建实体类Resource,有name,website,language三个属性,getter和setter方法
在Resource实体类头部加注解
@Configration:代表此类会引用资源文件
@ConfigurationProperties(prefix="前缀") : 做映射时,只把前缀后面的属性映射到实体类的字段里去
@PropertySource(value="classpath:resource.properties"):要引用的资源文件的位置
spring-boot-configuration-processor
加载注入properties文件,映射到实体类
@Configuration:表示是会引用资源文件的类
@ConfigurationProperties:指定资源文件的前缀
@PropertySource:指定资源文件的位置
加载注入properties文件,映射到实体类
@Configuration:表示是会引用资源文件的类
@ConfigurationProperties:指定资源文件的前缀
@PropertySource:指定资源文件的位置
SpringBoot 资源文件属性配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
BeanUtils.copyProperties(a, b);
如果两个类字段相同,可以用这一条语句拷贝,不用逐个属性赋值(a的值赋给b)。
BeanUtils.copyProperties
如果两个类字段相同,可以用这一条语句拷贝,不用逐个属性赋值
https://github.com/leechenxiang/imooc-springboot-starter
1.读取资源配置文件中的内容需引入相关jar包:
2.与资源文件映射的实体类需添加以下注解:

springboot 资源文件属性配置
引入依赖
spring-boot-configuration-processor
springboot属性文件映射到实体类:
1、pom中引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<dependency>
2、
读取自己添加的properties配置文件
@Configuration
@ConfigurationProperties(prefix="com.springboot.opensource")
@PropertySource(value="classpath:resource.properties")