Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.1 - Mixed configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.1 - Mixed configuration

    Hello,

    I am trying to convert a portion of my configuration into Java configuration and the configuration class looks like this -

    @Configuration
    public class AppConfig {

    @Bean
    public org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean entityManagerFactory(){
    org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean entityManagerFactory = new org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean();
    entityManagerFactory.setDataSource(dataSource());
    entityManagerFactory.setPersistenceUnitName("App1" );
    entityManagerFactory.setJpaDialect(jpaDialect());
    entityManagerFactory.setJpaVendorAdapter(jpaVendor Adapter());
    entityManagerFactory.setPersistenceXmlLocation("/WEB-INF/persistence.xml");
    return (LocalContainerEntityManagerFactoryBean) entityManagerFactory.getObject();


    }

    @Bean
    public DataSource dataSource() {
    org.springframework.jndi.JndiObjectFactoryBean dataSource = new org.springframework.jndi.JndiObjectFactoryBean();
    dataSource.setJndiTemplate(jndiTemplate());
    dataSource.setJndiName("java:comp/env/jdbc/app1_datasource");
    return (DataSource) dataSource.getObject();

    }

    @Bean
    public org.springframework.jndi.JndiTemplate jndiTemplate(){
    org.springframework.jndi.JndiTemplate jndiTemplate = new org.springframework.jndi.JndiTemplate();
    Properties environment = Environment.getProperties();
    environment.setProperty("java.naming.factory.initi al", "weblogic.jndi.WLInitialContextFactory");
    environment.setProperty("java.naming.provider.url" , "t3://localhost:7001");
    jndiTemplate.setEnvironment(environment);
    return jndiTemplate;
    }

    @Bean
    public JpaDialect jpaDialect(){
    org.springframework.orm.jpa.vendor.HibernateJpaDia lect jpaDialect = new org.springframework.orm.jpa.vendor.HibernateJpaDia lect();
    return jpaDialect();
    }

    @Bean
    public org.springframework.orm.jpa.vendor.HibernateJpaVen dorAdapter jpaVendorAdapter(){
    org.springframework.orm.jpa.vendor.HibernateJpaVen dorAdapter vendorAdapter = new org.springframework.orm.jpa.vendor.HibernateJpaVen dorAdapter();
    vendorAdapter.setDatabasePlatform("org.hibernate.d ialect.SQLServerDialect");
    vendorAdapter.setGenerateDdl(true);
    vendorAdapter.setShowSql(true);
    return vendorAdapter;
    }

    @Bean
    public org.springframework.flex.core.io.JpaHibernateConfi gProcessor jpaConfigProcessor(){
    org.springframework.flex.core.io.JpaHibernateConfi gProcessor jpaConfigProcessor = new org.springframework.flex.core.io.JpaHibernateConfi gProcessor();
    jpaConfigProcessor.setEntityManagerFactory(null);
    return jpaConfigProcessor;
    }
    }

    but when I try to use this configuration to startup my application -

    I get this exception -

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'remoteService' defined in file [D:\STS26\workspace\app1\WebContent\WEB-INF\classes\com\appDAOclass]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.transaction.config.internalTr ansactionAdvisor': Cannot resolve reference to bean 'org.springframework.transaction.annotation.Annota tionTransactionAttributeSource#0' while setting bean property 'transactionAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.transaction.annotation.Annota tionTransactionAttributeSource#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'auditAdvisor' defined in ServletContext resource [/WEB-INF/app-servlet.xml]: Cannot resolve reference to bean 'testAdvice' while setting bean property 'advice'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'testAdvice': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private com.dao.TestDAO com.aop.testAdvice.TestDAO; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/AppConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionSt oreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean com.AppConfig.entityManagerFactory()] threw exception; nested exception is java.lang.IllegalArgumentException: DataSource must not be null

    Can someone let me know if something is missing?

    Thanks & Regards
    SK

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable ...

    You should call the afterPropertiesSet method on the JndiObjectFactoryBean before calling getObject.

    Comment


    • #3
      Hello,

      I am facing this problem of assigning the datasource to EntityManagerFactory definition.

      When I use the XML configuration

      Code:
      <bean id="entityManagerFactory"
      		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      		<property name="persistenceUnitName" value="Test" />
      		<property name="dataSource" ref="dataSource" />
      		<property name="jpaDialect" ref="jpaDialect" />
      		<property name="jpaVendorAdapter" ref="vendorAdapter" />
      		<property name="persistenceXmlLocation" value="/WEB-INF/persistence.xml"/>
      	</bean>
      
      <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      			<property name="jndiTemplate" ref="jndiTemplate"/>
      		<property name="jndiName">
      		 <value>java:comp/env/jdbc/test_datasource</value>
      		 </property>
      	</bean>
      everything works fine. But when I try to do the same in Java config as mentioned earlier it fails to accept the datasource -

      Code:
      @Bean
          public EntityManagerFactory entityManagerFactory(){
          	org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean entityManagerFactory = new org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean();
          	entityManagerFactory.setDataSource(dataSource());
          	entityManagerFactory.setPersistenceUnitName("test");
          	entityManagerFactory.setJpaDialect(jpaDialect());
          	entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter());
          	entityManagerFactory.setPersistenceXmlLocation("/WEB-INF/persistence.xml");
          	return (EntityManagerFactory) entityManagerFactory.getObject();
          
          	
          }
      
        @Bean
          public DataSource dataSource() { 
          	org.springframework.jndi.JndiObjectFactoryBean dataSource = new org.springframework.jndi.JndiObjectFactoryBean();
          	try{
          	
          	dataSource.setJndiTemplate(jndiTemplate());
          	dataSource.setJndiName("java:comp/env/jdbc/test_datasource");
          	dataSource.afterPropertiesSet();
          	
          	
          	}
          	catch(Exception e){
          		
          	}
          	return (DataSource)dataSource;
      }
      Can you please let me know if I am missing something?

      Also, how do I use @ExternalBean with Spring 3.1?

      Thanks
      SK

      Comment


      • #4
        Have you actually read my post????

        Originally posted by mdeinum
        You should call the afterPropertiesSet method on the JndiObjectFactoryBean before calling getObject.

        Comment


        • #5
          Thanks Marten I had missed this as well.

          Comment

          Working...
          X