gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:24 25 4
gpt4 key购买 nike

我正在尝试使用 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 工具套件运行时,第一次在控制台中成功打印欢迎消息。

“****开发人员简介称为*****开发”

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

代码:

@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) 或带有 null 值的 @webservice 类

请帮我解决这个问题。

最佳答案

我重现了场景,但我在实现中做了一些更改

这是我的 application.yml

spring:
profiles:
active:
- dev

这是我的 application-dev.yml

welcome:
message: Development

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

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

关于java - Spring Boot应用程序(特定于配置文件).properties返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50710346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com