Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
Properties not resolved from EnvironmentValueResolver after context creation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Properties not resolved from EnvironmentValueResolver after context creation

    I have the following bean configuration.

    Code:
    @EnvironmentValueSource
    @Configuration
    public class ApplicationConfiguration extends ConfigurationSupport {
    
        protected @ExternalValue("database_type") String databaseType;
    
        protected @ExternalValue("driver") String driverClass;
    
        protected @ExternalValue("url") String jdbcUrl;
    
        protected @ExternalValue("jdbc_username") String user;
    
        protected @ExternalValue("jdbc_password") String password;
    
        @Bean
        protected DataSource dataSource() {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            try {
                dataSource.setDriverClass(this.driverClass);
            } catch (PropertyVetoException pve) {
    
            }
            dataSource.setJdbcUrl(this.jdbcUrl);
            dataSource.setUser(this.user);
            dataSource.setPassword(this.password);
            return dataSource;
        }
        
    }
    I am setting these properties as system variables in my application code.

    Code:
    public class SpringContext {
        private static JavaConfigApplicationContext context;
    
        static {        
            context = new JavaConfigApplicationContext(JpaConfiguration.class, DataSourceConfiguration.class, ApplicationConfiguration.class);
        }
    
        public static Engine makeEngine(Configuration configuration) {
            Properties properties = new Properties();
            properties.setProperty("database_type", configuration.getDatabaseType().toString());
            properties.setProperty("driver", configuration.getDriver());
            properties.setProperty("url", configuration.getUrl());
            properties.setProperty("jdbc_username", configuration.getJdbcUsername());
            properties.setProperty("jdbc_password", configuration.getJdbcPassword());
            System.setProperties(properties);        
            return new Engine(configuration);
        }
    }
    I get the following error when I try to create the context.

    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'config.JpaConfiguration': Initialization of bean failed; nested exception is org.springframework.config.java.valuesource.ValueResolutionException: Cannot resolve property for name 'database_type' against resolvers [PropertiesValueResolver,PropertiesValueResolver,PropertiesValueResolver,PropertiesValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:287)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:903)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:817)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
    	... 45 more
    Caused by: org.springframework.config.java.valuesource.ValueResolutionException: Cannot resolve property for name 'database_type' against resolvers [PropertiesValueResolver,PropertiesValueResolver,PropertiesValueResolver,PropertiesValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver,EnvironmentValueResolver]
    	at org.springframework.config.java.valuesource.CompositeValueResolver.resolve(CompositeValueResolver.java:59)
    	at org.springframework.config.java.internal.factory.ExternalValueInjectingBeanPostProcessor.postProcessAfterInitialization(ExternalValueInjectingBeanPostProcessor.java:112)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:361)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1343)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    	... 66 more
    Appreciate your insight into this issue.

    Thanks!
    Arul

  • #2
    Arul,

    Please submit this as a bug with a test case that reproduces the behavior. we'll be able to look at it quickly and efficiently that way.

    Thanks for taking the time.

    - Chris

    Comment


    • #3
      Hi Chris,

      I created a bug report (jira.springframework.org/browse/SJC-199) for this issue with a testcase.

      Please review this bug if this is a supported scenario in SJC. I used the latest nightly build for testing this.

      Thanks!
      Arul

      Comment


      • #4
        Hi Chris,

        Did you had a chance to look into this bug report?

        Is there any workaround for this approach?

        Thanks!
        Arul

        Comment


        • #5
          I've updated SJC-199. See it for details.

          Comment


          • #6
            Thanks Chris for implementing SystemPropertiesValueSource annotation.

            While this comes handy in a standalone JSE environment where only one component sets the system property in JVM and making sure no body overwrites, I am not sure whether this would be the case when it comes to a JEE environment where there are many components loaded into the JVM. For instance, I do not have control over the system property used in one component could possibly get overridden by a different component running within the same JVM.

            Is there any other way where I can dynamically configure using JavaConfig before initializing the context?

            Appreciate your inputs.

            Cheers,
            Arul

            Comment

            Working...
            X