Announcement Announcement Module
Collapse
No announcement yet.
Autowire Environment interface to spring social showcase example always return NULL!! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowire Environment interface to spring social showcase example always return NULL!!

    hi, I am new to Spring and Spring social. I was trying to modify the existing spring social showcase example to change the data source to Apache Basic Data Source pointing to local MySQL database. However, when I autowire/inject Environment class to get the jdbc url from application.properties file, I always got null value

    The AppConfig class is the same as the MainConfig on spring showcase GitHub except I added environment injection

    Code:
     
    @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");
    ....
    I am sure the application.properties contains a property named "jdbc.url". The null pointer exception happened when spring application is starting. Below is the stacktrace:

    Code:
     
    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
    I am using Tomcat 7.0.40. The spring framework version is 3.2.2.release, same as the one in spring social showcase. Please let me know if you need further information to help me with this issue. Thanks a lot!!

    For the next step, I am going to add JPA support to the spring social showcase example since spring data JPA provides many good features like the JPA repository. If someone can give me the guidance on this one, I will really appreciate that.

    Ross

  • #2
    Please post your actual code instead of a modified sample...

    Comment


    • #3
      Hi Marten, my actual code is almost the same as spring showcase example, except I changed the file name from MainConfig to AppConfig. I thought it would be a good start for my project based on a working example. You can find the source code on https://github.com/SpringSource/spri...ocial-showcase You can simply reproduce the issue by putting
      Code:
      @Autowired
      Environment environment;
      into the org.springframework.social.showcase.config.MainCon fig class. Then try to use the environment property in
      Code:
      @Bean(destroyMethod = "shutdown")
      	public DataSource dataSource() {
                      environment.getProperty("jdbc.url");
      		EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
      		factory.setDatabaseName("spring-social-showcase");
      		factory.setDatabaseType(EmbeddedDatabaseType.H2);
      		factory.setDatabasePopulator(databasePopulator());
      		return factory.getDatabase();
      	}
      If you set a breakpoint at environment.getProperty("jdbc.url"); this line, you will see environment variable is null so that you will get a NPE during spring startup.

      Hope this clarifies how to reproduce the issue.

      Comment


      • #4
        I was certainly able to recreate your problem. And, I'm ashamed to say that I don't know why the injected Environment is null. Now you've given me a puzzle to solve.

        But to be clear, this has more to do with Spring itself than with Spring Social. The raw question is "Why is the injected Environment null?". I don't believe Spring Social has anything to do with it other than that you found this in the Spring Social Showcase.

        Comment


        • #5
          Originally posted by habuma View Post
          I was certainly able to recreate your problem. And, I'm ashamed to say that I don't know why the injected Environment is null. Now you've given me a puzzle to solve.

          But to be clear, this has more to do with Spring itself than with Spring Social. The raw question is "Why is the injected Environment null?". I don't believe Spring Social has anything to do with it other than that you found this in the Spring Social Showcase.
          Good to see your reply habuma. First I would say the spring social showcase example is awesome but it needs to be fine tuned to suit my needs.

          Second, I am not blaming Spring Social for causing this issue. It is just because I have another project which uses Spring 3.2.2 as well and environment injection works fine there. Therefore I had a feeling that this problem might be somehow related to the application context initialization sequence but I am not sure. I also tried to inject ApplicationContext using
          Code:
          @Autowired
          ApplicationContext applicationContext;
          Then get environment using
          Code:
          applicationContext.getEnvironment();
          . This throws a NPE as well showing applicationContext is NULL.

          Btw, it was interesting to see the showcase example did environment injection in the ExplicitConfig (cannot remember the exact class name) class. That class was not turned on since the example is using simple profile.

          Anyways, I can easily by-pass this problem for now by hard code jdbc.url to the data source, although it is very bad practice :P. Next I am gonna integrate JPA and Hibernate to the example. There may be some other problems....

          Thanks,

          Comment


          • #6
            I think I resolved the problem, after reading A LOT of documents.

            Changing the code
            Code:
            @Bean
            	public PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
            		return new PropertySourcesPlaceholderConfigurer();
            	}
            To

            Code:
            @Bean
            	public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
            		return new PropertySourcesPlaceholderConfigurer();
            	}
            This is very tricky but it was said in Spring framework API doc. I saw some place in the API doc saying that it has to be static. Unfortunately I forgot the link. Basic idea here is that PropertySourcesPlaceholderConfigurer has to be static in order to kick in very early in the context initialization process.

            Comment

            Working...
            X