Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring:对非字符串属性使用@Value_Java_Spring - Fatal编程技术网

Java Spring:对非字符串属性使用@Value

Java Spring:对非字符串属性使用@Value,java,spring,Java,Spring,我有这个 import org.apache.log4j.Logger; @Configuration @PropertySource("classpath:abc.properties") public class AbcConfig { @Value("${test.testLogger}") private Logger logger; @Bean public static PropertySourcesPlaceholderConfigurer propertyC

我有这个

import org.apache.log4j.Logger;

@Configuration
@PropertySource("classpath:abc.properties")
public class AbcConfig {

  @Value("${test.testLogger}")
  private Logger logger;

  @Bean
  public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
    return new PropertySourcesPlaceholderConfigurer();
  }
然后在我的java代码中,我希望在使用属性创建AnnotationContext时传入该类的记录器

context = new AnnotationConfigApplicationContext();
Properties prop = new Properties();
prop.put("test.testLogger", getLogger());
context.getEnvironment().getPropertySources().addLast(new PropertiesPropertySource("test", prop));
context.register(AbcConfig.class);
context.refresh();
不幸的是,当我运行此程序时,会出现以下异常:

Caused by: java.lang.IllegalArgumentException: Cannot convert value [org.apache.log4j.Logger@5f015823] from source type [Logger] to target type [String]
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:59)
at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:427)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:131)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:128)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:73)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:59)
at org.springframework.core.env.AbstractPropertyResolver$1.resolvePlaceholder(AbstractPropertyResolver.java:176)
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:146)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:174)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:143)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:167)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:755)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:778)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 32 more
所以在我看来,@Value的用法只适用于字符串(可能还有整数)。 这是使用Spring 3.2.2。
是否有解决方法(使用记录器或任何其他非字符串对象)?

@Value适用于字符串。它是一个属性读取器。如果需要记录器类型的对象,请使用@Autowired。您可以在主应用程序类中配置bean。阅读以获取更多帮助为什么要将
@Value
用于
记录器
?@Value适用于字符串。它是一个属性读取器。如果需要记录器类型的对象,请使用@Autowired。您可以在主应用程序类中配置bean。阅读以获取更多帮助为什么要将
@Value
用于
记录器