Spring MVC spring Boot 应用程序(特定于配置文件).properties返回null

vuktfyat  于 2022-11-14  发布在  Spring
关注(0)|答案(2)|浏览(132)

I am trying to load profile specific property file values using springboot to my @webservice class. I followed the below steps,
1)

created application.properties with profile to be active
spring.profiles.active=dev
Created application-dev.properties with following values
username = XXXXXXX
password = XXXXXXX
host = XXXXXXX
port = XXXXXXX
welcome.message = Development
Created @Springbootapplication class
Created @configuration class for appconfig
  1. Started reading values of application-dev.properties in the appconfig class using
    code:
public class AppConfig {
@Value("${welcome.message}")
 private String welcomeMsg;
 @Profile("dev")
@Bean
public MessageSource messageSourceDev() {
System.out.println("****Dev Profile called*****"+welcomeMsg);

when i run from spring tool suite, for the first time welcome message printed successfully in console.
"****Dev Profile called*****Development"
But when I try to read the property values from the @webservice class I created,through soap UI,
Code:

@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);

Either it fails at appconfig.class by throwing the following exception
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)
  1. Or @webservice class with null value
    Please help me in resolving the issue.
5q4ezhmt

5q4ezhmt1#

我复制了escenario,但在实现中做了一些更改
这是我的应用程序.yml

spring:
  profiles:
    active:
    - dev

这是我的应用程序-dev.yml

welcome:
  message: Development

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

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

xj3cbfub2#

而不是application.yml尝试给application.yaml它为我工作的相同的问题一样,你的一个

相关问题