英文:
How to process property file values during server start up in Spring?
问题
我在我的Spring Boot应用的application.properties文件中有这个条目:
myapp.urls = url1,url2,url3
在我的组件的一个方法中,我创建了一个数组,代码如下:
String myArray[] = properties.getmyAppUrls().split(",");
我希望这个数组的创建逻辑只执行一次。我知道我们可以使用@PostConstruct注解来实现这一点。除此之外,有没有其他方法可以在服务器启动时实现这个逻辑?
我希望在服务器启动期间从属性文件中读取并构造这个数组,并在我的组件中使用它。
英文:
I have this entry in application.properties of my spring boot app:
myapp.urls = url1,url2,url3
In a method in my component, I am creating an array like below:
String myArray[] = properties.getmyAppUrls().split(",");
I want this array creation logic execute only once. I know we can achieve this using post construct. Is there any other way we could achieve this like during server start up?
I want this array constructed reading from a properties file during server start up and i want to use this in my component.
答案1
得分: 0
你可以使用Spring EL来完成这项任务:
@Value("#{'${myapp.urls}'.split(',')}")
private List<String> myAppUrls;
我建议将这段代码移到一个Configuration
类中,然后你可以在需要的地方进行自动装配。
英文:
You can use Spring EL to do the job:
@Value("#{'${myapp.urls}'.split(',')}")
private List<String> myAppUrls;
I'd recommend to move this to a Configuration
class, then you can autowire it everywhere you need it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论