如果在项目启动的时候就要根据数据库、或者其他类型的数据来源 来设置某个bean的属性,可以用@PostConstruct来实现
@Component
@Data
public class MytestConfig {
private HashMap<String, String> map = new HashMap();
//初始化map数据
@PostConstruct
public void init(){
//假设从数据库读取数据 放入map进行初始化
map.put("name","test");
map.put("age","5");
}
}
单元测试
@Autowired
MytestConfig mytestConfig;
@Test
void testPostConstructor(){
System.out.println(mytestConfig.getMap());
}
输出