热加载
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
添加依赖后,在ide里面重启应用,后续修改后马上可以生效
不被热部署的文件
1、/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates
2、指定文件不进行热部署 spring.devtools.restart.exclude=static/,public/
3、手工触发重启 spring.devtools.restart.trigger-file=trigger.txt 改代码不重启,通过一个文本去控制
注意点:生产环境不要开启这个功能,如果用java -jar启动,springBoot是不会进行热部署的
xml、properties、json、yaml
配置文件参数的映射(属性和实体)
配置文件加载:
方法1:
- 1、Controller上面配置 @PropertySource({"classpath:xxxxx.properties"})
- 不加{}也可以
- 2、增加属性 @Value("${test.name}")
- private String name;
@Controller
@PropertySource("classpath:application.properties")
public class FileController {
@RequestMapping("/api/v1/gopage")
public Object getPage() {
return "index";
}
///User/ligang/Desktop/person/springboot/src/main
//images的路径
//注意 后面要加 /
//
// private static final String filepath="/Users/ligang/Desktop/";
//使用配置文件的方式解决硬编码问题
@Value("${web.file.path}")
private String filepath;
......
application.properties
#文件上传路径配置
web.file.path=/Users/ligang/Desktop/
方法2:
实体类配置:
- 1、添加 @Component 注解;
- 2、使用 @PropertySource 注解指定配置文件位置; @PropertySource(value="classpath:application.properties")
- 3、使用 @ConfigurationProperties(prefix="test") 注解,设置相关属性;
- 4、必须 通过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。
- @Autowired
- private ServerSettings serverSettings;
常见问题:
1、配置文件注入失败,Could not resolve placeholder
解决:根据springboot启动流程,会有自动扫描包没有扫描到相关注解,
默认Spring框架实现会从声明@ComponentScan所在的类的package进行扫描,来自动注入,
因此启动类最好放在根路径下面,或者指定扫描包范围
spring-boot扫描启动类对应的目录和子目录
2、注入bean的方式,属性名称和配置文件里面的key一一对应,就不用加@Value 这个注解
如果不一样,就要加@value("${XXX}")
@Component
@ConfigurationProperties
@PropertySource(value="classpath:application.properties")
public class ServerSetting {
//名称
@Value("${test.name}")
private String name;
//域名地址
@Value("${test.domain}")
private String domain;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
}
注入使用:
application.properties文件中的配置
#测试配置文件注入 在getController中返回显示
test.name=xijie
test.domain=www.baidu.com
@Autowired
private ServerSetting serverSetting;
@GetMapping("/v1/test_properties")
public Object testProperties(){
return serverSetting;
方法2:通过名称映射
在加入前缀的时候,就不需要加@Value("${test.name}")
@ConfigurationProperties(prefix="test")
private String name
相当于test.name 映射到配置文件中到属性
@Component
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:application.properties")
public class ServerSetting2 {
//名称
private String name;
//域名地址
private String domain;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
}
方法3:不加(prefix="xxx")
这样要一一对应(变量名称要与配置文件中到变量一致)
@Component
@ConfigurationProperties
@PropertySource(value="classpath:application.properties")
public class ServerSetting3 {
//名称
private String name;
//域名地址
private String domain;
.......
application.properties
name=name
domain=domain
@Autowired
private ServerSetting3 serverSetting3;
@GetMapping("/v1/test_properties3")
public Object testProperties3(){
return serverSetting3;
}