Reputation: 61
In my index.html page, a variable in the script is hard coded. I want to get it from application.properties file but have no idea how to. It would helpful if anyone could provide me a solution.
Upvotes: 6
Views: 5270
Reputation: 6540
I have attached the example. Hope to help.
Application
@SpringBootApplication
public class Application {
public static void main(String... args) {
SpringApplication.run(Application.class);
}
}
PropertiesController
@RestController
public class PropertiesController {
@Autowired
private UIProperty uiProperty;
@RequestMapping("properties")
public UIProperty getProperties() {
return uiProperty;
}
}
UIProperty
@Component
@ConfigurationProperties(prefix = "ui.label")
public class UIProperty {
private String user;
private String password;
public void setUser(String user) {
this.user = user;
}
public String getUser() {
return user;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
application.properties
ui.label.user=user
ui.label.password=password
database.user=
database.password=
Upvotes: 3
Reputation: 2555
I'd create a RestController to expose ConfigurationProperties. But be sure to properly secure it as well as limit in its scope not to disclose confidential data like db access credentials.
Upvotes: 0