4

我是 Spring 和 Spring 社交的新手。我试图修改现有的春季社交展示示例,将数据源更改为指向本地 MySQL 数据库的 Apache 基本数据源。但是,当我自动装配/注入环境类以从 application.properties 文件中获取 jdbc url 时,我总是得到空值

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 应用程序启动时发生空指针异常。下面是堆栈跟踪:

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 social show 中的版本相同。如果您需要更多信息来帮助我解决此问题,请告诉我。非常感谢!!

下一步,我将在 spring 社交展示示例中添加 JPA 支持,因为 spring data JPA 提供了许多好的特性,比如 JPA 存储库。如果有人能给我这方面的指导,我将不胜感激。

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

谢谢!

4

1 回答 1

7

这种“奇怪”的行为通常发生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();
}
于 2013-06-25T22:31:12.533 回答