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
}
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
public List
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
//解析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
return urlMap;
}
private void setUrlMap(Map
this.urlMap = urlMap;
}
}
具体开发中应用,直接引用类,进行使用即可!