如果文件位于类路径中,这就是我加载application.properties的方式。
@ComponentScan
@Configuration
@PropertySource("classpath:application.properties")
public class MyApplication {
// Have some public methods
}
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyApplication.class);
MyApplication app = context.getBean(MyApplication.class);
// execute methods in app
}
}
我们正在尝试部署将属性文件外部存储在Google Cloud bucket(GCS)上的应用程序。我可以从GCS加载属性文件并将其保存在内存中。如何将属性传递到应用程序上下文并覆盖从类路径加载的属性?
如果有关系,它是一个独立的应用程序,而不是Spring Boot。
答案 0 :(得分:0)
如果您将其存储在内存中,那应该不成问题。摆脱 {
username: "Bobby",
timeline: ["The weekend is finally here!!!!", "Any one have any plans?"],
image: [
"https://images.unsplash.com/photo-1462887749044-b47cb05b83b8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80",
"https://images.unsplash.com/photo-1516383074327-ac4841225abf?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80"
]
},
{
username: "Sally",
timeline: "Javascript is so coooool.",
image:
"https://images.unsplash.com/photo-1542653309-dbc6a80a7484?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1051&q=80"
},
{
username: "John",
timeline: "I am so good at Javascript.",
image:
"https://images.unsplash.com/photo-1499209974431-9dddcece7f88?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80"
},
{
username: "Jonathan",
timeline: "Hello everyone, here at school.",
image:
"https://images.unsplash.com/photo-1551754809-c0a4e5b246ec?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80"
},
{
username: "Matthew",
timeline: "Just got back from the doctor.",
image:
"https://images.unsplash.com/photo-1537977193203-d4fce971cb5e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1044&q=80"
}
];
for (i = 0; i < newsFeed.length; i++) {
output += `
<div class="newsFeedHeader">
<h1>${newsFeed[i].username}'s timeline.</h1>
<p>${newsFeed[i].timeline}</p>
<img src=${newsFeed[i].image} alt='joy image'>
</div>
`;
}
批注并手动注册@PropertySource("classpath:application.properties")
bean。
PropertySourcesPlaceholderConfigurer
答案 1 :(得分:0)
您可以按照以下方式读取外部属性文件。
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value = "${external.properties}", ignoreResourceNotFound = true)
})
运行jar
文件时,请按照以下方式传递external.properties文件路径
java -Dexternal.properties=file:/path_to_properties my-service.jar