一、配置文件
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());
}
}