import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @description: TestPropertiesInit 属性数据初始化:
* 实现InitializingBean接口,重写方法afterPropertiesSet()方法,可以在项目启动时进行加载
* @author:
* @create: 2020.2.25
*/
@Component("TestPropertiesInit")
public class TestPropertiesInit implements InitializingBean {
private static Logger logger = LoggerFactory.getLogger(TestPropertiesInit.class);
//项目启动加载数据存入map中
public static Map<String, String> map = new HashMap<String, String>();
public static Map<String, String> getMap() {
return map;
}
public static void setMap(Map<String, String> map) {
TestPropertiesInit.map = map;
}
private ConstantMapper constantMapper;
public ConstantMapper getConstantMapper() {
return constantMapper;
}
//采用set方式获取对象,不能采用autowire,项目启动时未初始化完成spring对象
@Resource
public void setConstantMapper(ConstantMapper constantMapper) {
this.constantMapper = constantMapper;
}
@Override
public void afterPropertiesSet() throws Exception {
logger.info("----初始化系统参数开始---------------start");
List<Constant> constants = this.constantMapper.queryAllCode();
for (Constant c : constants) {
map.put(c.getCode(), c.getValue());
//System.out.println("map====="+map.toString());
}
//System.out.println(map);
logger.info("----初始化系统参数结束--------success-------end" + map.toString());
}
}
————————————————
版权声明:本文为CSDN博主「kangfu_521」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kang1011/article/details/105034951/
05-15
3285
02-10
417
08-25
2939
04-04
1767
08-20
563
12-16
1109
10-28
6434