Results 1 to 4 of 4

Thread: Unable to combine AnnotationDrivenTx with ExternalValue. Possible bug?

  1. #1
    Join Date
    Nov 2008
    Posts
    2

    Default 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. #2
    Join Date
    Apr 2007
    Posts
    307

    Default

    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.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  3. #3
    Join Date
    Nov 2008
    Posts
    2

    Default

    Chris,

    Thanks for quick response. Works like a charm.

  4. #4
    Join Date
    Apr 2007
    Posts
    307

    Default

    Note this issue has been resolved and should be available in the latest M5 nightly build.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •