Mani Pushkal Siriki
Mani Pushkal Siriki

Reputation: 61

Getting a value in application.properties file using javascript in spring boot

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

Answers (2)

Eddú Meléndez
Eddú Meléndez

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

daniel.eichten
daniel.eichten

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

Related Questions