spring boot application(特定于配置文件).properties返回null

时间:2018-06-05 23:37:02

标签: java spring spring-mvc spring-boot

我正在尝试使用springboot将配置文件特定的属性文件值加载到我的@webservice类。我按照以下步骤进行操作,

1)

created application.properties with profile to be active
spring.profiles.active=dev

2)

Created application-dev.properties with following values
username = XXXXXXX
password = XXXXXXX
host = XXXXXXX
port = XXXXXXX
welcome.message = Development

3)

 Created @Springbootapplication class

4)

Created @configuration class for appconfig

5)使用

开始在appconfig类中读取application-dev.properties的值

代码:

public class AppConfig {
@Value("${welcome.message}")
 private String welcomeMsg;
 @Profile("dev")
@Bean
public MessageSource messageSourceDev() {
System.out.println("****Dev Profile called*****"+welcomeMsg);

当我从spring工具套件运行时,第一次欢迎消息在控制台中成功打印。

" **** Dev Profile名为***** Development"

但是当我尝试从我创建的@webservice类中读取属性值时,通过soap UI,

代码:

@WebService(serviceName = "SampleIF", endpointInterface   = "com.tc.services.sample.cxf.SampleIF")
public class SampleIFImpl implements SampleIF {
@Value("${welcome.message}")
private String welcomeMsg;
    @Override
public Contact CreateContract(Details details) {
    System.out.println("****Welcome Message Val*****"+welcomeMsg);

它通过抛出以下异常在appconfig.class失败

1)

   org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'welcome.message' in value "${welcome.message}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:379)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1348)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)

2)或具有空值的@webservice类

请帮我解决问题。

2 个答案:

答案 0 :(得分:1)

我复制了escenario,但我在实现中做了一些更改

这是我的application.yml

spring:
  profiles:
    active:
    - dev

这是我的application-dev.yml

welcome:
  message: Development

最后我直接从需要值的类中使用变量

@Value("${welcome.message}")
private String welcomeMsg;

答案 1 :(得分:-1)

我认为你应该在AppConfig类上添加@PropertySource(“classpath:application-dev.properties”)。