在 Spring 引导应用程序启动应用程序时获取 属性 值
Get property value on Startup of application in Spring boot application
我想在我的 spring 启动应用程序中从 application.properties 中获取一个 属性 的值。请看下面的代码。
@Component
public class ContactEntityComp implements InitializingBean, CommandLineRunner
{
@Value("${amqp.routes.get}")
public String routes_get;
@PostConstruct
public void getCountryList() {
System.out.println( " routes_get in PostConstruct- "+routes_get);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println( " routes_get in afterPropertiesSet- "+routes_get);
}
@Override
public void run(String... args) throws Exception {
System.out.println( " routes_get in afterPropertiesSet- "+routes_get);
}
}
我想在 spring 引导应用程序启动时从 application.properties 获取 属性 - amqp.routes.get 的值。
我已经尝试过 1) @postConstruct 注释,2) InitializingBean 接口,3) CommandLineRunner 接口,但我从 application.properties 得到 属性 的空值。
还有其他方法吗?
您应该可以像这样访问它。
@Configuration
@PropertySource("classpath:application.properties")
public class SomeConfigClass {
@Autowired
private Environment env;
@Bean
public Whatever someBeanFunc() {
String desiredProp = env.getProperty("amqp.routes.get");
}
}
如果您试图在一个 class 中访问大量属性,这将特别有用。
我想在我的 spring 启动应用程序中从 application.properties 中获取一个 属性 的值。请看下面的代码。
@Component
public class ContactEntityComp implements InitializingBean, CommandLineRunner
{
@Value("${amqp.routes.get}")
public String routes_get;
@PostConstruct
public void getCountryList() {
System.out.println( " routes_get in PostConstruct- "+routes_get);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println( " routes_get in afterPropertiesSet- "+routes_get);
}
@Override
public void run(String... args) throws Exception {
System.out.println( " routes_get in afterPropertiesSet- "+routes_get);
}
}
我想在 spring 引导应用程序启动时从 application.properties 获取 属性 - amqp.routes.get 的值。 我已经尝试过 1) @postConstruct 注释,2) InitializingBean 接口,3) CommandLineRunner 接口,但我从 application.properties 得到 属性 的空值。 还有其他方法吗?
您应该可以像这样访问它。
@Configuration
@PropertySource("classpath:application.properties")
public class SomeConfigClass {
@Autowired
private Environment env;
@Bean
public Whatever someBeanFunc() {
String desiredProp = env.getProperty("amqp.routes.get");
}
}
如果您试图在一个 class 中访问大量属性,这将特别有用。