1.1、java中sping项目默认会读取message.properties文件 (主要用于国际化,错误提示用,也可扩展用)
如下配置代码类似之前xml文件,beans - bean注入 , 代码如下
import java.io.IOException;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
//springmvc配置,此配置必须有,beans工厂类注入,不然异常处理时不能解析json
@Configuration
public class SpringMvcConfig extends WebMvcConfigurerAdapter{
//自定义ObjectMapper,处理springmvc序列化
@Bean
public ObjectMapper jacksonObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
// 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化 ,只对VO起作用,Map List不起作用
// Include.Include.ALWAYS 默认
// Include.NON_DEFAULT 属性为默认值不序列化
// Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
// Include.NON_NULL 属性为NULL 不序列化
objectMapper.setSerializationInclusion(Include.NON_NULL);
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object value, JsonGenerator jg, SerializerProvider sp)
throws IOException, JsonProcessingException {
// 所有null字段,重写为空字符串
// jg.writeString("");
sp.getDefaultNullKeySerializer();
}
});
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return objectMapper;
}
//加载properties配置文件,默认message,后续有追加在配置 - 注入bean
@Bean(name = "messageSource")
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource rb = new ReloadableResourceBundleMessageSource();
rb.setBasenames("classpath:message","classpath:message-auth","classpath:message-cms");//为国际化和参数校验指定一个目录
return rb;
}
//日志文件过滤 - 注入bean
@Bean
public Filter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(5120);
return filter;
}
//构建方法级别验证 - 注入bean
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor(LocalValidatorFactoryBean localValidatorFactoryBean) {
MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor();
postProcessor.setValidator(localValidatorFactoryBean);
return postProcessor;
}
//文件上传临时路径 - 注入bean
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setLocation("/gomeo2o/data/uploadTemp");
return factory.createMultipartConfig();
}
//文件校验 - 注入bean
@Bean(name ="validator")
public LocalValidatorFactoryBean localValidatorFactoryBean(){
LocalValidatorFactoryBean lfb = new LocalValidatorFactoryBean();
lfb.setValidationMessageSource(messageSource());
return lfb;
}
}
1.2、默认message.properties文件自定义内容后,代码应用获取实现
首先 message.properties 文件内容为:
#店铺
shop.create.parameter.error=the shop info is invalid.
shop.create.open.error=the error occor when shop is created.
shop.userName=的美店
代码应用时,务必从新扫描获取
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import com.gomeo2o.facade.vshop.entity.VshopInfo;
@Component
@ConfigurationProperties(prefix = "shop")
@PropertySource("classpath:message.properties")
public class ShopInfoVo extends VshopInfo{
private String userName;
public String getUserName() {
return userName;
}
}
1.3、默认message.properties文件自定义内容后,代码应用获取实现
首先 message.properties 文件内容为:
#店铺
shop.create.parameter.error=the shop info is invalid.
shop.create.open.error=the error occor when shop is created.
shop.userName=的美店
代码应用中也可通过org.springframework.context.MessageSource包进行调用,应用如下:
@Autowired
MessageSource messageSource;
String me=messageSource.getMessage("cms.home.orgCode.notNull", null, Locale.CHINA);
System.out.println(" ==="+me);
2、java中自定义properties文件进行 配置文件内容功能 应用
A、现在自定义 urlMapper.properties 内容配置文件,内容如下
## 预览
urlMapper.urls[0]=preview###${meidian.h5.address_0}/my_mshop.html
## 已入账收益
urlMapper.urls[1]=haveEarnings###${meidian.h5.address_1}/views/profit/profit.html#1
## 商品管理
urlMapper.urls[2]=shopManage###${meidian.h5.address_0}/goods_manage.html
B、配置获取配置文件内容文件类UrlProperties,具体代码如下
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
//URL映射配置读取
@Component
@ConfigurationProperties("urlMapper")
@PropertySource("classpath:urlMapper.properties")
public class UrlProperties {
private List<String> urls = new ArrayList<>();
public List<String> getUrls() {
return urls;
}
}
获取配置文件内容并进行解析封装 (可有可无,根据情况自定义) 代码如下,类:UrlSource
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//移动端跳转H5链接映射配置Bean
@Configuration
public class UrlSource {
@Resource
private UrlProperties properties;
private Map<String, String> urlMap = new HashMap<>();
//解析URL映射并生成Bean
@Bean("urlSources")
public UrlSource getUrlSource(){
UrlSource source = new UrlSource();
if(urlMap.isEmpty()){
String[] urlSplit = null;
for(String url: properties.getUrls()){
urlSplit = url.split("###");
urlMap.put(urlSplit[0], urlSplit[1].replace("@", "$"));
}
source.setUrlMap(urlMap);
}
return source;
}
public Map<String, String> getUrlMap() {
return urlMap;
}
private void setUrlMap(Map<String, String> urlMap) {
this.urlMap = urlMap;
}
}
具体开发中应用,直接引用类,进行使用即可!