我的Spring Boot应用程序中具有与应用程序相关的属性。奇怪的问题是,有时加载属性时没有任何问题,但大多数情况下它们会引发异常。
这是我的春季靴子类,上面带有@Configuration
注释。尝试调试该问题,但找不到这种奇怪行为的任何原因。
@Configuration
public class RedisConfig {
private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
@Value("${redisurl}")
private String redisURL;
@Value("${redisport}")
private String redisPort;
@Bean
public JedisConnectionFactory redisConnectionFactory() {
logger.info("--redisURL-" + redisURL);
logger.info("--redisPort-" + redisPort);
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redisURL);
redisConnectionFactory.setPort(Integer.parseInt(redisPort));
logger.info("--connected to redis--");
return redisConnectionFactory;
}
redisurl=cst-prd-007
redisport=6379
redispassword=
感谢您的帮助。
Stacktrace:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.redis.RedisHealthIndicatorConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'redisurl' in value "${redisurl}"
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'redisurl' in value "${redisurl}"
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:834)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationB
编辑:
我也尝试这样做。没用
@Autowired
Environment env;
@Bean
public JedisConnectionFactory redisConnectionFactory() {
logger.info("--redisURL-" + redisURL);
logger.info("--redisPort-" + redisPort);
redisURL = env.getRequiredProperty("redis.url");
redisPort = env.getRequiredProperty("redis.port");
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redisURL);
redisConnectionFactory.setPort(Integer.parseInt(redisPort));
logger.info("--connected to redis--");
return redisConnectionFactory;
}
使用以下方法后问题已解决
@Component
public class RedisConf {
@Value("${redis.url}")
String url;
@Value("${redis.port}")
int port;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
答案 0 :(得分:0)
您可以尝试使用Spring Framework API中的@Value
而不是使用Environment
注释。
喜欢这个
@Configuration
public class RedisConfig {
private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
private String redisURL, redisPort;
@Autowired
private Environment env;
@Bean
public JedisConnectionFactory redisConnectionFactory() {
redisURL = env.getRequiredProperty("redisurl");
redisPort = env.getRequiredProperty("redisport");
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redisURL);
redisConnectionFactory.setPort(Integer.parseInt(redisPort));
logger.info("--connected to redis--");
return redisConnectionFactory;
}
答案 1 :(得分:0)
使用以下方法即可解决问题
@Component
public class RedisConf {
@Value("${redis.url}")
String url;
@Value("${redis.port}")
int port;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}