由于@Value,在Spring应用程序中创建bean时出错

时间:2019-03-14 08:57:53

标签: spring spring-boot spring-annotations

我有一个作为组件的类。在此类中,我尝试从application.properties填充属性。我使用@Value注释相同。 当我添加@value批注时,它将引发以下错误。 setter方法上的@Value导致应用程序引发Injection of autowired dependencies failed;异常。

以下是课程:

@Component
public class AgsProbeConstants {

 public static final String COMPONENT_KEY = "component";
 public static final String API_KEY = "api";
 public static final String CALLEE_KEY = "callee";
 public static final String RESPONSE_CODE_KEY = "response_code";
 public static final String ERROR_RESPONSE_CODE_KEY = "error_reason_code";
 public static final String HOST_KEY = "host";
 public static final String METRIC_VALUE = "api.connect.http.count";

 public String pushUrl;

 public String getPushUrl() {
    return pushUrl;
 }

 @Value("${property.push.url}")
 public void setPushUrl(String url) {
    pushUrl = url;
 }
}

当我直接运行webApp时,这可以很好地工作并选择值。但是,当我执行Maven安装时,它将引发错误。错误如下:

Error creating bean with name 'agsProbeConstants': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'property.push.url' in value "${property.push.url}"


Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'property.push.url' in value "${property.push.url}"


java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.3.RELEASE.jar:5.1.3.RELEASE]

以下是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
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2Dialect
# 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.push.url=http://localhost:4242/write

我无法找出问题所在。为什么Maven构建失败。

感谢您的帮助。

0 个答案:

没有答案