讲解使用@value注解配置文件自动映射到属性和实体类
1、配置文件加载
方式一
1、Controller上面配置
@PropertySource({"classpath:resource.properties"})
2、增加属性
@Value("${test.name}")
private String name;
@PropertySource({"classpath:application.properties"})
@Controller
@EnableAutoConfiguration
public class FileController {
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory=new MultipartConfigFactory();
factory.setMaxFileSize("10MB");
factory.setMaxRequestSize("100MB");
return factory.createMultipartConfig();
}
@RequestMapping("/")
public Object index(){
return "index";
}
// private static final String filePath="D:\\programme\\java\\xdclass_springboot\\src\\main\\resources\\static\\images\\";
@Value("${web.images-path}")
private String filePath;
@RequestMapping(value = "/upload")
@ResponseBody
public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request){
//若不加MultipartFile file,文件名应与前端文件名一致
String name = request.getParameter("name");
System.out.println("用户名:"+name);
System.out.println("路径:"+filePath);
方式二:实体类配置文件
application.properties
web.images-path=D:/programme/java/xdclass_springboot/src/main/resources/static/images/
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.images-path}\
#测试配置文件注入
test.domain=www.xdclass.net
test.name=springboot
步骤:
1、添加 @Component 注解;
2、使用 @PropertySource 注解指定配置文件位置;
3、使用 @ConfigurationProperties 注解,设置相关属性;
package net.xdclass.demo.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource({"classpath:application.properties"})
@ConfigurationProperties(prefix = "test")
public class ServerSettings {
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;
}
public ServerSettings() {
}
@Value("test.name")
private String name;
@Value("test.domain")
private String domain;
}
4、必须 通过注入IOC对象Resource 进来 , 才能在类中使用获取的配置文件值。
@Autowired
private ServerSettings serverSettings;
package net.xdclass.demo.controller;
import net.xdclass.demo.domain.ServerSettings;
import net.xdclass.demo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
//@Controller @RestController实现以下页面不需要写@ResponseBody就能实现
@RestController
//@PropertySource({"classpath:application.properties"})
public class SampleController {
@RequestMapping("/home")
public Object home(){
return "templates/index.html";
}
@RequestMapping("/test")
public Map<String,String> testMap(){
Map<String,String> map = new HashMap<>();
map.put("name","xdclass");
return map;
}
@GetMapping("/testjson")
public Object testjson(){
return new User(111,"abc1230","10001000",new Date());
}
@Autowired
private ServerSettings ServerSettings;
@GetMapping("/v1/test")
public Object testPeroperties(){
return ServerSettings;
}
}
例子:
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:resource.properties")
public class ServerConstant {
常见问题:
1、配置文件注入失败,Could not resolve placeholder
解决:根据springboot启动流程,会有自动扫描包没有扫描到相关注解,
默认Spring框架实现会从声明@ComponentScan所在的类的package进行扫描,来自动注入,
因此启动类最好放在根路径下面,或者指定扫描包范围
spring-boot扫描启动类对应的目录和子目录
2、注入bean的方式,属性名称和配置文件里面的key一一对应,就用加@Value 这个注解
如果不一样,就要加@value("${XXX}")