You
can refer the properties by using their name. See the below
example.
application.properties
app.name=MyApp app.version=1.5 app.description=${app.name} : ${app.version} is running using spring boot application
As
you see, I am referring ‘app.name’ and ‘app.version’ in ‘app.description’ property.
ConfigBean.java
package com.sample.myApp.model; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ConfigBean { @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; @Value("${app.description}") private String appDescription; public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getAppVersion() { return appVersion; } public void setAppVersion(String appVersion) { this.appVersion = appVersion; } public String getAppDescription() { return appDescription; } public void setAppDescription(String appDescription) { this.appDescription = appDescription; } }
Application.java
package com.sample.myApp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.sample.myApp.model.ConfigBean; @SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args); ConfigBean configBean = applicationContext.getBean(ConfigBean.class); String applicationName = configBean.getAppName(); String applicationVersion = configBean.getAppVersion(); String applicationDescription = configBean.getAppDescription(); System.out.println("**********************************************"); System.out.printf("Application Name : %s\n", applicationName); System.out.printf("Application Version : %s\n", applicationVersion); System.out.printf("Application Description : %s\n", applicationDescription); System.out.println("**********************************************"); applicationContext.close(); } }
When
you ran ‘Application.java’, you can able to see below messages in the console.
********************************************** Application Name : MyApp Application Version : 1.5 Application Description : MyApp : 1.5 is running using spring boot application **********************************************
No comments:
Post a Comment