Autowire环境界面以弹出社交展示示例始终返回NULL

时间:2013-06-25 18:55:05

标签: spring dependency-injection spring-social

我是Spring and Spring社交新手。我试图修改现有的spring社交展示示例,将数据源更改为指向本地MySQL数据库的Apache Basic Data Source。但是,当我通过autowire / inject Environment类从application.properties文件中获取jdbc url时,我总是得到null值

AppConfig类与Spring展示GitHub上的MainConfig相同,只是我添加了环境注入

@Configuration
@ComponentScan(basePackages = "org.springframework.social.showcase")
@PropertySource("classpath:org/springframework/social/showcase/config/application.properties")
@EnableTransactionManagement
public class AppConfig {

    @Autowired
    Environment environment;

    @Bean(destroyMethod = "shutdown")
    public DataSource dataSource() {
        environment.getProperty("jdbc.url");
....

我确信application.properties包含一个名为“jdbc.url”的属性。 Spring应用程序启动时发生空指针异常。下面是stacktrace:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource org.springframework.social.showcase.config.AppConfig.dataSource()] threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:181)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570)
    ... 60 more
Caused by: java.lang.NullPointerException
    at org.springframework.social.showcase.config.AppConfig.dataSource(AppConfig.java:55)
    at org.springframework.social.showcase.config.AppConfig$$EnhancerByCGLIB$$a0d39c08.CGLIB$dataSource$0(<generated>)
    at org.springframework.social.showcase.config.AppConfig$$EnhancerByCGLIB$$a0d39c08$$FastClassByCGLIB$$3d304ff8.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:286)
    at org.springframework.social.showcase.config.AppConfig$$EnhancerByCGLIB$$a0d39c08.dataSource(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)
    ... 61 more

我正在使用Tomcat 7.0.40。 Spring框架版本为3.2.2.release,与春季社交展示版本相同。如果您需要更多信息来帮助我解决此问题,请与我们联系。非常感谢!!

下一步,我将为Spring社交展示示例添加JPA支持,因为Spring数据JPA提供了许多优秀的功能,如JPA存储库。如果有人能给我这个指导,我会非常感激。

我已将此问题发布到http://forum.springsource.org/showthread.php?139141-Autowire-Environment-interface-to-spring-social-showcase-example-always-return-NULL!!如果您需要更多详细信息,可以查看此处。

谢谢!

1 个答案:

答案 0 :(得分:6)

这种“奇怪”行为通常发生在游戏中BeanFactoryPostProcessor时。事实上,spring-social-sample包含一个配置错误,Spring在启动时甚至会报告它:

WARN : org.springframework.context.annotation.ConfigurationClassEnhancer - @Bean method MainConfig.propertyPlaceHolderConfigurer is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details

将属性占位符工厂方法标记为static将修复它:

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}