Announcement Announcement Module
Collapse
No announcement yet.
Problems with Spring and Ibatis Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with Spring and Ibatis

    Hi all, I´m having a problem with Spring + iBatis
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <!--
      - Application context definition for "springapp" DispatcherServlet.
      -->
    
    <beans>
    
        <bean id="springappControllerExcel" class="web.SpringappControllerExcel">
            <property name="productManager">
            	<ref bean="prodMan"/>
        	</property>
    	</bean>
        <bean id="springappController" class="web.SpringappController">
            <property name="productManager">
                <ref bean="prodMan"/>
            </property>
        </bean>
        <bean id="prodMan" class="bus.ProductManager">
        	<property name="productManagerDao">
                <ref bean="prodManDao"/>
            </property>
        </bean>
    	
        <!-- Obtención de un datasource mediante un pool de conexiones -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName">
    			<value>com.mysql.jdbc.Driver</value>
    		</property>
    		<property name="url"><value>jdbc&#58;mysql&#58;//localhost&#58;3306/springappBisBD</value>
    		</property>
    		<property name="username">
    			<value></value>
    		</property>
    		<property name="password">
    			<value></value>
    		</property>
    	</bean>
        <bean id="priceIncreaseValidator" class="bus.PriceIncreaseValidator"/>
        <bean id="priceIncreaseForm" class="web.PriceIncreaseFormController">
            <property name="sessionForm"><value>true</value></property>
            <property name="commandName"><value>priceIncrease</value></property>
            <property name="commandClass"><value>bus.PriceIncrease</value></property>
            <property name="validator"><ref bean="priceIncreaseValidator"/></property>
            <property name="formView"><value>priceincrease</value></property>
            <property name="successView"><value>hello.htm</value></property>
            <property name="productManager"><ref bean="prodMan"/></property>
        </bean>
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename"><value>messages</value></property>
        </bean>
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/hello.htm">springappController</prop>
                    <prop key="/priceincrease.htm">priceIncreaseForm</prop>
                    <prop key="/enexcel.htm">springappControllerExcel</prop>
                    <prop key="/enpdf.htm">springappControllerExcel</prop>
                </props>
            </property>
        </bean>
        <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
    		<property name="order"><value>1</value></property>
        </bean>
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
            <property
    name="prefix"><value>/WEB-INF/jsp/</value></property>
            <property name="suffix"><value>.jsp</value></property>
        </bean>
        <bean id="productsExcel" class="web.ProductsExcelView"/>
         <bean id="productsPdf" class="web.ProductsPdfView"/>
        
       	<!------------      IBatis  Configuration    --------------->
    
        <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <property name="configLocation">
            	<value>"/WEB-INF/sql-map-config.xml"</value>
            </property>
        </bean>
    
        <!-- I think the problem is here, but i don´t get it -->
         <bean id="prodManDao" class="db.ProductManagerDaoIbatis">
            <property name="dataSource">
            	<ref bean="dataSource"/>
            </property>
            <property name="sqlMapClient">
            	<ref bean="sqlMapClient"/>
            </property>
        </bean>
    
    </beans>
    IBatis's configuration file (/WEB-INF/sql-map-config.xml)
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE sqlMapConfig
        PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
        "http&#58;//www.ibatis.com/dtd/sql-map-config-2.dtd">
    
    <sqlMapConfig>
    
      <settings enhancementEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" />
      <sqlMap resource="bus/Product.xml"/>
    
    </sqlMapConfig>
    Mapping file (/WEB-INF/classes/bus/Product.xml)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMap
        PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
        "http&#58;//www.ibatis.com/dtd/sql-map-2.dtd">
    
    <sqlMap namespace="Product">
      <typeAlias alias="product" type="bus.Product"/>
    
      <select id="getProductList" resultClass="product">
        select id as id,description as description,price as price from products
      </select>
      
      <update id="increasePrice" parameterClass="product">
      	update products set price = #price#
      </update>
    </sqlMap>
    Implementation:
    Code:
    package db;
    
    import bus.Product;
    
    import java.util.List;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
    import org.springframework.orm.ibatis.SqlMapClientTemplate;
    
    public class ProductManagerDaoIbatis extends SqlMapClientDaoSupport implements ProductManagerDao &#123;
    
        /** Logger for this class and subclasses */
        protected final Log logger = LogFactory.getLog&#40;getClass&#40;&#41;&#41;;
    
        public List getProductList&#40;&#41; &#123;
            logger.info&#40;"Getting products with IBAtis!"&#41;;
        	return this.getSqlMapClientTemplate&#40;&#41;.queryForList&#40;"getProductList",null&#41;;
        	//return this.getSqlMapClientTemplate&#40;&#41;.queryForList&#40;"getLamp","Lamp"&#41;;
         &#125;
    
        public void increasePrice&#40;Product prod, int pct&#41; &#123;
            logger.info&#40;"Increasing price by with IBAtis " + pct + "%"&#41;;
            prod.setPrice&#40;new Double&#40;prod.getPrice&#40;&#41;.doubleValue&#40;&#41; * &#40;100.0 + pct&#41;/100&#41;&#41;;
            this.getSqlMapClientTemplate&#40;&#41;.update&#40;"increasePrice",prod&#41;;
        &#125;
    &#125;
    When i start up Tomcat, I see output in the logs like this:
    Code:
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'springappControllerExcel' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Can't resolve reference to bean 'prodMan' while setting property 'productManager'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'prodMan' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Can't resolve reference to bean 'prodManDao' while setting property 'productManagerDao'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'prodManDao' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Can't resolve reference to bean 'sqlMapClient' while setting property 'sqlMapClient'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'sqlMapClient' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Initialization of bean failed; nested exception is java.io.FileNotFoundException&#58; Could not open ServletContext resource &#91;/"/WEB-INF/sql-map-config.xml"&#93;
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'prodMan' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Can't resolve reference to bean 'prodManDao' while setting property 'productManagerDao'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'prodManDao' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Can't resolve reference to bean 'sqlMapClient' while setting property 'sqlMapClient'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'sqlMapClient' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Initialization of bean failed; nested exception is java.io.FileNotFoundException&#58; Could not open ServletContext resource &#91;/"/WEB-INF/sql-map-config.xml"&#93;
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'prodManDao' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Can't resolve reference to bean 'sqlMapClient' while setting property 'sqlMapClient'; nested exception is org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'sqlMapClient' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Initialization of bean failed; nested exception is java.io.FileNotFoundException&#58; Could not open ServletContext resource &#91;/"/WEB-INF/sql-map-config.xml"&#93;
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'sqlMapClient' defined in ServletContext resource &#91;/WEB-INF/springapp-servlet.xml&#93;&#58; Initialization of bean failed; nested exception is java.io.FileNotFoundException&#58; Could not open ServletContext resource &#91;/"/WEB-INF/sql-map-config.xml"&#93;
    java.io.FileNotFoundException&#58; Could not open ServletContext resource &#91;/"/WEB-INF/sql-map-config.xml"&#93;
    	at org.springframework.web.context.support.ServletContextResource.getInputStream&#40;ServletContextResource.java&#58;89&#41;
    	at org.springframework.orm.ibatis.SqlMapClientFactoryBean.afterPropertiesSet&#40;SqlMapClientFactoryBean.java&#58;209&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods&#40;AbstractAutowireCapableBeanFactory.java&#58;1037&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;305&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;223&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;236&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;159&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveReference&#40;AbstractAutowireCapableBeanFactory.java&#58;945&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveValueIfNecessary&#40;AbstractAutowireCapableBeanFactory.java&#58;879&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues&#40;AbstractAutowireCapableBeanFactory.java&#58;820&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean&#40;AbstractAutowireCapableBeanFactory.java&#58;648&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;288&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;223&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;236&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;159&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveReference&#40;AbstractAutowireCapableBeanFactory.java&#58;945&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveValueIfNecessary&#40;AbstractAutowireCapableBeanFactory.java&#58;879&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues&#40;AbstractAutowireCapableBeanFactory.java&#58;820&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean&#40;AbstractAutowireCapableBeanFactory.java&#58;648&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;288&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;223&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;236&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;159&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveReference&#40;AbstractAutowireCapableBeanFactory.java&#58;945&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveValueIfNecessary&#40;AbstractAutowireCapableBeanFactory.java&#58;879&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues&#40;AbstractAutowireCapableBeanFactory.java&#58;820&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean&#40;AbstractAutowireCapableBeanFactory.java&#58;648&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;288&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;223&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;236&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;159&#41;
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons&#40;DefaultListableBeanFactory.java&#58;261&#41;
    	at org.springframework.context.support.AbstractApplicationContext.refresh&#40;AbstractApplicationContext.java&#58;317&#41;
    	at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh&#40;AbstractRefreshableWebApplicationContext.java&#58;131&#41;
    	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext&#40;FrameworkServlet.java&#58;283&#41;
    	at org.springframework.web.servlet.FrameworkServlet
    .
    .
    I see the error is in creating the bean "prodManDao", the rest of configuration file is good. I am testing with Hibernate and it works.
    I think this code is the root cause, but i dont know why
    Code:
         <bean id="prodManDao" class="db.ProductManagerDaoIbatis">
            <property name="dataSource">
            	<ref bean="dataSource"/>
            </property>
            <property name="sqlMapClient">
            	<ref bean="sqlMapClient"/>
            </property>
        </bean>
    I am desperated, any help will be wellcome.
    Thanks in advance

  • #2
    I have found the mistake.
    Code:
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <property name="configLocation">
               <value>"/WEB-INF/sql-map-config.xml"</value>
            </property>
        </bean>
    The quotes in <value> are not correct

    Comment


    • #3
      Great post. Found by problem with my adding Ibatis to the Spring "Product" tutorial. It excaped my attention that "dataSource" needed to be added for dependency injection on my Ibatis Dao implemenation bean.

      Most post when asking about Ibatis and the Spring demo refer you too the "jpetstore" demo ... it looked and looked at that for hours and didn't get a clue till I saw this posting. Thanks ...

      Comment

      Working...
      X