一、配置文件

ewcrm.uploadPath=D:/ewcrm_web/uploader/
ewcrm.wLoginExpDays=30
二、加载配置文件类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* "ewcrm"为配置文件中键的前缀
* 这里用lombok的@Data,不然需要添加get方法
* @PropertySource为添加文件地址
* @author chhuang
* @create 2020-11-08-10:53
*/
@Configuration
@ConfigurationProperties(prefix = "ewcrm", ignoreUnknownFields = false)
@PropertySource("classpath:config/config.properties")
@Data
@Component
public class EwcrmProperties {
private String uploadPath;
private Integer wLoginExpDays;
}
三、在需要的时候使用
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
EwcrmProperties ewcrmProperties;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//映射图片保存地址
registry.addResourceHandler(EwcrmConstants.UPLOAD_URL+"**")
.addResourceLocations("file:"+ewcrmProperties.getUploadPath());
}
}
该博客介绍了如何在Spring Boot应用中加载配置文件,使用`@ConfigurationProperties`注解绑定配置,并通过`WebMvcConfigurer`接口映射静态资源。配置文件路径为`classpath:config/config.properties`,配置项包括`ewcrm.uploadPath`和`ewcrm.wLoginExpDays`,在需要时用于设置资源处理器。
873

被折叠的 条评论
为什么被折叠?



