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://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:mysql://localhost: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://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://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 {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
public List getProductList() {
logger.info("Getting products with IBAtis!");
return this.getSqlMapClientTemplate().queryForList("getProductList",null);
//return this.getSqlMapClientTemplate().queryForList("getLamp","Lamp");
}
public void increasePrice(Product prod, int pct) {
logger.info("Increasing price by with IBAtis " + pct + "%");
prod.setPrice(new Double(prod.getPrice().doubleValue() * (100.0 + pct)/100));
this.getSqlMapClientTemplate().update("increasePrice",prod);
}
}
When i start up Tomcat, I see output in the logs like this:
Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springappControllerExcel' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Can't resolve reference to bean 'prodMan' while setting property 'productManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prodMan' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Can't resolve reference to bean 'prodManDao' while setting property 'productManagerDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prodManDao' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Can't resolve reference to bean 'sqlMapClient' while setting property 'sqlMapClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlMapClient' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/"/WEB-INF/sql-map-config.xml"]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prodMan' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Can't resolve reference to bean 'prodManDao' while setting property 'productManagerDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prodManDao' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Can't resolve reference to bean 'sqlMapClient' while setting property 'sqlMapClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlMapClient' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/"/WEB-INF/sql-map-config.xml"]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prodManDao' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Can't resolve reference to bean 'sqlMapClient' while setting property 'sqlMapClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlMapClient' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/"/WEB-INF/sql-map-config.xml"]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlMapClient' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/"/WEB-INF/sql-map-config.xml"]
java.io.FileNotFoundException: Could not open ServletContext resource [/"/WEB-INF/sql-map-config.xml"]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:89)
at org.springframework.orm.ibatis.SqlMapClientFactoryBean.afterPropertiesSet(SqlMapClientFactoryBean.java:209)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1037)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:305)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:236)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:159)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveReference(AbstractAutowireCapableBeanFactory.java:945)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveValueIfNecessary(AbstractAutowireCapableBeanFactory.java:879)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:820)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:648)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:236)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:159)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveReference(AbstractAutowireCapableBeanFactory.java:945)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveValueIfNecessary(AbstractAutowireCapableBeanFactory.java:879)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:820)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:648)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:236)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:159)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveReference(AbstractAutowireCapableBeanFactory.java:945)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveValueIfNecessary(AbstractAutowireCapableBeanFactory.java:879)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:820)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:648)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:236)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:159)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:261)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:317)
at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:131)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:283)
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