@Value在运行webApp时工作正常,但maven构建失败,无法解析值为“ $ {property.push.url}”的占位符'property.push.url'

时间:2019-03-14 12:21:54

标签: spring spring-boot spring-annotations

我有一个作为组件的类。在此类中,我尝试从application.properties填充属性。我在使用@Value注释。

应用程序运行正常,我能够从application.properties文件中获取值。但是,Maven构建因错误而失败

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'agsProbeConstants': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'property.opera.push.url' in value "${property.opera.push.url}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
... 31 more


Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'property.opera.push.url' in value "${property.opera.push.url}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:851)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1185)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:668)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
... 47 more

以下是课程:

@Component
public class AgsProbeConstants {

  public static final String OPERA_COMPONENT_KEY = "component";
  public static final String OPERA_API_KEY = "api";
  public static final String OPERA_CALLEE_KEY = "callee";
  public static final String OPERA_RESPONSE_CODE_KEY = "response_code";
  public static final String OPERA_ERROR_RESPONSE_CODE_KEY = "error_reason_code";
  public static final String OPERA_HOST_KEY = "host";
  public static final String OPERA_METRIC_VALUE = "api.connect.http.count";

  public static String operaPushUrl;

  public String getOperaPushUrl() {
    return operaPushUrl;
  }

  @Value("${property.opera.push.url}")
  public void setOperaPushUrl(String url) {
    operaPushUrl = url;
  }
}

以下是Application.properties文件:

server.port=8080
logging.level.org.springframework.web.servlet: DEBUG

spring.profiles.active=dev
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1

# = JPA / HIBERNATE
# ===============================
spring.jpa.properties.hibernate.generate_statistics=true
logging.level.org.hibernate.stat=debug
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=trace
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
# Naming strategy
spring.jpa.hibernate.naming.implicit- strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

以下是Application-dev.properties:

#DB settings
spring.datasource.url=jdbc:db2://localhost:60020/DB
#spring.datasource.url=jdbc:db2://10.10.10.10:60020/DB        
use this for docker, if facing DNS resolution issue.
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
spring.datasource.username=root
spring.datasource.password=password

# Hibernate ddl auto (create, create-drop, update): with "create-drop" the database
# schema will be automatically created afresh for every start of application
spring.jpa.hibernate.ddl-auto=none
#schema setting for JPA
spring.jpa.properties.hibernate.default_schema=schema1
# Allows Hibernate to generate SQL optimized for a particular DBMS
# Show or not log for each sql query
spring.jpa.show-sql=true

# Scheduler properties
#======================
property.map={VROL:'http://localhost:8080/availability/statuszz', 
VDP:'http://localhost:8080/availability/statuszz'}
property.opera.push.url=http://localhost:4242/write

我无法找出问题所在。任何帮助表示赞赏。

0 个答案:

没有答案