SSH 事务采用java5注解无法获取Service实例bean
我采用ssh JAVA5注解进行事务配置: <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
service:
package com.xxx.prjta.service.Impl;import java.util.List;import org.springframework.transaction.annotation.Transactional;import com.xx.prjta.dao.IProductsDAO;
import com.xx.prjta.dao.IProviderDAO;
import com.xx.prjta.po.Products;
import com.xx.prjta.service.IProductService;
import com.xx.prjta.utils.PageSupport;@Transactional
public class ProductService implements IProductService {
private IProductsDAO productsDAO;
private IProviderDAO providerDAO;
public List<Products> queryProducts() {
return productsDAO.findListByHSQL("from Products", null);
}
public PageSupport<Products> queryProducts(PageSupport<Products> ps) {
return productsDAO.findPageSupportByHSQL("from Products", "select count(*) from Products", null, ps);
}
public IProductsDAO getProductsDAO() {
return productsDAO;
} public void setProductsDAO(IProductsDAO productsDAO) {
this.productsDAO = productsDAO;
} public IProviderDAO getProviderDAO() {
return providerDAO;
} public void setProviderDAO(IProviderDAO providerDAO) {
this.providerDAO = providerDAO;
}
}直接获取接口可以调用得到,但获取BEAN时就会提示出错:
测试代码:
String[] configLocations = {"applicationContext.xml", "userBeans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
IProductService proService = (IProductService)ctx.getBean("procductService"); 这样可以正常获取,可以得到事务并进行数据库操作。
但这样时不行: String[] configLocations = {"applicationContext.xml", "userBeans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
ProductService proService = (ProductService )ctx.getBean("procductService"); 提示:
Exception in thread "main" java.lang.ClassCastException: $Proxy5 cannot be cast to com.xxx.prjta.service.Impl.ProductService
at com.xx.prjta.test.Test.main(Test.java:37)
请问一下,使用 java5注解进行事务配置我该如何才能获取ProductService的bean实例啊?
我采用ssh JAVA5注解进行事务配置: <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
service:
package com.xxx.prjta.service.Impl;import java.util.List;import org.springframework.transaction.annotation.Transactional;import com.xx.prjta.dao.IProductsDAO;
import com.xx.prjta.dao.IProviderDAO;
import com.xx.prjta.po.Products;
import com.xx.prjta.service.IProductService;
import com.xx.prjta.utils.PageSupport;@Transactional
public class ProductService implements IProductService {
private IProductsDAO productsDAO;
private IProviderDAO providerDAO;
public List<Products> queryProducts() {
return productsDAO.findListByHSQL("from Products", null);
}
public PageSupport<Products> queryProducts(PageSupport<Products> ps) {
return productsDAO.findPageSupportByHSQL("from Products", "select count(*) from Products", null, ps);
}
public IProductsDAO getProductsDAO() {
return productsDAO;
} public void setProductsDAO(IProductsDAO productsDAO) {
this.productsDAO = productsDAO;
} public IProviderDAO getProviderDAO() {
return providerDAO;
} public void setProviderDAO(IProviderDAO providerDAO) {
this.providerDAO = providerDAO;
}
}直接获取接口可以调用得到,但获取BEAN时就会提示出错:
测试代码:
String[] configLocations = {"applicationContext.xml", "userBeans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
IProductService proService = (IProductService)ctx.getBean("procductService"); 这样可以正常获取,可以得到事务并进行数据库操作。
但这样时不行: String[] configLocations = {"applicationContext.xml", "userBeans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
ProductService proService = (ProductService )ctx.getBean("procductService"); 提示:
Exception in thread "main" java.lang.ClassCastException: $Proxy5 cannot be cast to com.xxx.prjta.service.Impl.ProductService
at com.xx.prjta.test.Test.main(Test.java:37)
请问一下,使用 java5注解进行事务配置我该如何才能获取ProductService的bean实例啊?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货