【发布时间】:2013-10-25 14:39:37
【问题描述】:
我在将环境连接到我的 Spring 项目时遇到问题。 在这堂课中
@Configuration
@ComponentScan(basePackages = "my.pack.offer.*")
@PropertySource("classpath:OfferService.properties")
public class PropertiesUtil {
@Autowired
private Environment environment;
@Bean
public String load(String propertyName)
{
return environment.getRequiredProperty(propertyName);
}
}
环境始终为空。
【问题讨论】:
-
尝试使用
@Resource而不是@Autowired。看看有没有帮助。 -
@PauliusMatulionis,不,没有任何改变
-
@LeYar 这是您在
PropertiesUtil中的唯一代码吗?你有PropertySourcesPlaceholderConfigurer或类似的吗? -
你是如何使用这个
PropertyUtil... -
在相同的基于 java 的配置中使用 @Autowired Environment 和 @Bean PropertyPlaceholderConfigurer 会导致 environment == null。实现 EnvironmentAware 接口解决了这个问题。不知道是不是bug。 (春季 4.2.2)
标签: java spring properties environment autowired