Results 1 to 10 of 10

Thread: spring jndi

  1. #1

    Default spring jndi

    hi
    i developed code using spring hibernate to connect to DB
    in my applciationcontext.xml my code is like this
    Code:
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:jdbc.properties</value>
        </property>
    </bean>
    
    <bean id="dataSource" destroy-method="close"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    i read the username ,password , url from external jdbc.properties file.
    As now we are using websphere to deploy the application. they have configured datasource on websphere.
    how do my application connects to data source now.
    do i need to change applciationcontext.xml to use jndi ?
    can somebody help me please.

    Thanks
    AD

  2. #2
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi AD

    Extract (move) your current 'dataSource' bean definition into it's own distinct file (called 'dataSource-local.xml' for example).

    Create another Spring XML file called say 'dataSource-production.xml', and use the JndiObjectFactoryBean to define a bean called 'dataSource'. Use the guide in this section to help you do that.

    http://static.springframework.org/sp...dy-schemas-jee

    http://www.springframework.org/docs/...ctoryBean.html

    The all you have to do is reference whichever file you want to use in whatever environment, choosing one or the other as appropriate.

    Cheers
    Rick

  3. #3

    Default

    hi rick

    Thanks for reply. if i follow this what you say, can i be still able to use DAO code which i wrote to save ,get records from db? just by calling DAO bean from aplicationcontext.xml ?

    AD

  4. #4
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    can i be still able to use DAO code which i wrote to save ,get records from db?
    Yes.

    That is one of the sweet-as-a-nut features of the dependency injection principle. Nothing to do with Spring per-se, just DI itself.

    Cheers
    Rick

  5. #5

    Default

    hi rick

    sorry being dumb. At the moment my session transaction is in application context.xml if i move this datasource to another .xml file
    how do i call this
    now my applciationcontext.xml is this
    Code:
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:jdbc.properties</value>
        </property>
    </bean>
    
    <bean id="dataSource" destroy-method="close"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!-- Hibernate SessionFactory -->
        
           <!--
          Setup Hibernate based on config file 
          classpath:com/example/jsf/hibernate/example/resources/hibernate.cfg.xml and properties
          defined by hibernateProperties.
        -->
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource">
            <ref local="dataSource"/>
            </property>
            <property name="configLocation">
                <value>classpath:conf/RequestHibernate.cfg.xml</value>
            </property>
            <property name="hibernateProperties">
            <ref local="hibernateProperties"/>
            </property>
        </bean>
            
            
            <!-- <property name="hibernateProperties">-->
            <bean id="hibernateProperties" 
              class="org.springframework.beans.factory.config.PropertiesFactoryBean">
             <property name="properties">
            
            <props>
               <!-- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>-->
               <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect </prop>
                <prop key="hibernate.show_sql">true</prop>
              <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
            </property>
        </bean>
    
        <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
            <ref local="sessionFactory"/>
            </property>
        </bean>
    
        <!-- This is the base definition for all Hibernate based DAOs -->
        
        <bean id="FooRequestDAO" class="com.foo.impl.FooRequestDAO">
            <property name="sessionFactory">
            <ref local="sessionFactory"/>
            </property>
        </bean>
    in my javacode i call this as

    String[] paths = { "conf/ApplicationContext.xml" };
    ctx = new ClassPathXmlApplicationContext(paths);
    dao = (FooRequestDAO) ctx.getBean("FooRequestDAO");

    then dao.save or any function..

    can you explain me how can i split this please and also calling in my java code.

    Thanks
    AD

  6. #6
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    'conf/ApplicationContext.xml'

    Code:
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:jdbc.properties</value>
        </property>
    </bean>
    
    <!-- Hibernate SessionFactory -->
        
           <!--
          Setup Hibernate based on config file 
          classpath:com/example/jsf/hibernate/example/resources/hibernate.cfg.xml and properties
          defined by hibernateProperties.
        -->
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource">
            <ref local="dataSource"/>
            </property>
            <property name="configLocation">
                <value>classpath:conf/RequestHibernate.cfg.xml</value>
            </property>
            <property name="hibernateProperties">
            <ref local="hibernateProperties"/>
            </property>
        </bean>
            
            
            <!-- <property name="hibernateProperties">-->
            <bean id="hibernateProperties" 
              class="org.springframework.beans.factory.config.PropertiesFactoryBean">
             <property name="properties">
            
            <props>
               <!-- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>-->
               <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect </prop>
                <prop key="hibernate.show_sql">true</prop>
              <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
            </property>
        </bean>
    
        <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
            <ref local="sessionFactory"/>
            </property>
        </bean>
    
        <!-- This is the base definition for all Hibernate based DAOs -->
        
        <bean id="FooRequestDAO" class="com.foo.impl.FooRequestDAO">
            <property name="sessionFactory">
            <ref local="sessionFactory"/>
            </property>
        </bean>
    'conf/DataSource-Test.xml'

    Code:
    <bean id="dataSource" destroy-method="close"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    'conf/DataSource-Prod.xml' (This is just an example, you'll have to configure it for your specific Websphere environment.)

    Code:
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/MyDataSource"/>
    </bean>
    Now when developing locally, you can do this...

    Code:
    String[] paths = { "conf/ApplicationContext.xml", "conf/DataSource-Test.xml" };
    ctx = new ClassPathXmlApplicationContext(paths);
    dao = (FooRequestDAO) ctx.getBean("FooRequestDAO");
    And when deploying into your Websphere environment...

    Code:
    String[] paths = { "conf/ApplicationContext.xml", "conf/DataSource-Test.xml" };
    ctx = new ClassPathXmlApplicationContext(paths);
    dao = (FooRequestDAO) ctx.getBean("FooRequestDAO");
    There are better ways of achieving this, but this should hopefully get you started on the right path.

    Cheers
    Rick

  7. #7

    Default

    you mean

    And when deploying into your Websphere environment...


    Code:
    String[] paths = { "conf/ApplicationContext.xml", "conf/DataSource-Test.xml" };
    ctx = new ClassPathXmlApplicationContext(paths);
    dao = (FooRequestDAO) ctx.getBean("FooRequestDAO");

    that should be conf/datasource-prod.xml right?

    thanks
    AD

  8. #8
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hehe... yes! Doh! Sorry, I copied and pasted.

    Cheers
    Rick 'Sheep Face' Evans

  9. #9

    Default

    hi ricky

    now i wrote a test package which contains appcontext.xml like this
    Code:
    <beans>
      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:jdbc.properties</value>
        </property>
    </bean>
    <!--  Added this interceptor else the association(one-to-many) throws  failed to lazily initialize a collection of role:
           no session or session was closed   -->
       <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
             <property name="sessionFactory">
               <ref bean="sessionFactory"/>
             </property>
        </bean>
        <!-- Hibernate SessionFactory -->
        
           <!--
          Setup Hibernate based on config file 
          classpath:com/example/jsf/hibernate/example/resources/hibernate.cfg.xml and properties
          defined by hibernateProperties.
        -->
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource">
            <value>file:DataSource-test.xml</value>
            </property>
            <property name="configLocation">
                <value>classpath:config/hibernate.cfg.xml</value>
            </property>
            <property name="hibernateProperties">
            <ref local="hibernateProperties"/>
            </property>
        </bean>
            
            
            <!-- <property name="hibernateProperties">-->
            <bean id="hibernateProperties" 
              class="org.springframework.beans.factory.config.PropertiesFactoryBean">
             <property name="properties">
            
            <props>
          
              <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect </prop>
                <prop key="hibernate.show_sql">true</prop>
               <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
            </property>
        </bean>
    
        <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
            <ref local="sessionFactory"/>
            </property>
        </bean>
    
        <!-- This is the base definition for all Hibernate based DAOs -->
        
        <bean id="testdao" class="com.test.testDAO">
            <property name="sessionFactory">
            <ref local="sessionFactory"/>
            </property>
        </bean>
    and my datasource-test.xml is
    Code:
    <beans>
    <bean id="dataSource" destroy-method="close"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    </beans>
    I'm getting error like this now


    log4j:WARN No appenders could be found for logger (org.springframework.util.ClassUtils).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'hibernateInterceptor' defined in class path resource [config/appContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in class path resource [config/appContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateExcep tion; nested PropertyAccessExceptions (1) are:
    PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in class path resource [config/appContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateExcep tion; nested PropertyAccessExceptions (1) are:
    PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    Caused by: org.springframework.beans.PropertyBatchUpdateExcep tion; nested PropertyAccessException details (1) are:
    PropertyAccessException 1:
    org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.co nvertIfNecessary(TypeConverterDelegate.java:224)
    at org.springframework.beans.TypeConverterDelegate.co nvertIfNecessary(TypeConverterDelegate.java:139)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:772)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:606)
    at org.springframework.beans.AbstractPropertyAccessor .setPropertyValue(AbstractPropertyAccessor.java:49 )
    at org.springframework.beans.AbstractPropertyAccessor .setPropertyValues(AbstractPropertyAccessor.java:7 4)
    at org.springframework.beans.AbstractPropertyAccessor .setPropertyValues(AbstractPropertyAccessor.java:5 7)
    at org.springframework.beans.factory.support.Abstract BeanFactory.applyPropertyValues(AbstractBeanFactor y.java:840)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1026)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:809)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:425)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 50)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:141)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:247)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:161)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:245)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:124)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1019)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:809)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:425)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 50)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:141)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:247)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:161)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:273)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:346)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:92)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:77)
    at com.test.test.process(test.java:19)
    at com.test.test.main(test.java:13)


    any idea what's wrong? the wat i mentioned datasource in appconfig.xml is it correct?

    AD

  10. #10
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Quote Originally Posted by akdasari View Post
    The wat i mentioned datasource in appconfig.xml is it correct?
    No.

    Code:
           <!--
          Setup Hibernate based on config file 
          classpath:com/example/jsf/hibernate/example/resources/hibernate.cfg.xml and properties
          defined by hibernateProperties.
        -->
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation">
                <value>classpath:config/hibernate.cfg.xml</value>
            </property>
            <property name="hibernateProperties">
            <ref local="hibernateProperties"/>
            </property>
        </bean>
    Dependencies are resolved cross-file. See here.

    Cheers
    Rick

Posting Permissions

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