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
Unable to combine AnnotationDrivenTx with ExternalValue. Possible bug? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to combine AnnotationDrivenTx with ExternalValue. Possible bug?

    Hi,

    First off, to all you JavaConfig developers; thanks for providing this great alternative to the XML Spring configuration. Keep up the good work

    I'm out of luck trying to combine AnnotationDrivenTx with ExternalValue annotations. Consider the following JavaConfig applcation config class;

    (Oh, I'm unable to create a post with 'at' sign due to restrictions of the forum (this is my first post) so I've swapped them with '#' in the code.)

    Code:
    #Configuration
    #AnnotationDrivenConfig
    #AnnotationDrivenTx
    #PropertiesValueSource(locations = {
        "classpath:db.properties"
    })
    public class AppConfig extends ConfigurationSupport {
    
        #ExternalValue("datasource.username") String username;
        #ExternalValue("datasource.password") String password;
        #ExternalValue("datasource.driverClassName") String driverClassName;
        #ExternalValue("datasource.url") String url;
        #ExternalValue("hibernate.dialect") String dialect;
        #ExternalValue("hibernate.hbm2ddl.auto") String hbm2ddlAuto;
    
        #Bean
        public DataSource dataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
    
            System.out.println("in dataSource(): username = " + username);
    
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUrl(url);
            return dataSource;
        }
    
        #Bean
        public SessionFactory sessionFactory() {
            LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    
            Properties properties = new Properties();
            properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
            properties.put("hibernate.hbm2ddl.auto", "create");
            properties.put("hibernate.show_sql", true);
    
            sessionFactoryBean.setHibernateProperties(properties);
    
            sessionFactoryBean.setDataSource(dataSource());
            sessionFactoryBean.setMappingResources(new String[] {
                "test.xml"
            });
    
            return getObject(sessionFactoryBean, SessionFactory.class);
        }
    
        #Bean
        public PlatformTransactionManager transactionManager() {
            return new HibernateTransactionManager(sessionFactory());
        }
    }

    Loading this will cause 'in dataSource(): username = null' to be written to stdout. Trying to load the class with '#AnnotationDrivenTx' commented out, will however yield whatever value set in the db.properties file.

    Any insights?

    Thanks in advance.

  • #2
    This is a bug. I've got a placeholder issue for it at http://jira.springframework.org/browse/SJC-239

    As a workaround, use @ExternalValue methods instead of @ExternalValue fields. This seems to work just fine.

    Thanks for the report, and be sure to add yourself as a watcher to the issue to see when it's resolved.

    Comment


    • #3
      Chris,

      Thanks for quick response. Works like a charm.

      Comment


      • #4
        Note this issue has been resolved and should be available in the latest M5 nightly build.

        Comment

        Working...
        X