首页 > 解决方案 > 由于@Value,在 Spring 应用程序中创建 bean 时出错

问题描述

我有一个类是一个组件。在这个类中,我试图从 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 构建失败。

任何帮助表示赞赏。

标签: springspring-bootspring-annotations

解决方案


推荐阅读