属性文件如下所示:
url1=path_to_binary1
url2=path_to_binary2
根据this我尝试了以下方法:
@Component
@EnableConfigurationProperties
public class ApplicationProperties {
private Map<String, String> pathMapper;
//get and set
}
在另一个组件中,我自动装配了 ApplicationProperties:
@Autowired
private ApplicationProperties properties;
//inside some method:
properties.getPathMapper().get(appName);
产生NullPointerException
.
如何纠正呢?
update
我根据 user7757360 的建议有正确的:
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="app")
public class ApplicationProperties {
和属性文件:
app.url1=path_to_binary1
app.url2=path_to_binary2
还是不行
Update 2
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="app")
public class ApplicationProperties {
private Map<String, String> app;
和里面application.properties
:
app.url1=path_to_binary1
app.url2=path_to_binary2
还是不行