spring3整合hibernate使用XML配置事务处理时总报错,请高手进来看看。
XML配置如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="com.shrx" />
<context:annotation-config />
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.shrx.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" />
</tx:attributes>
</tx:advice>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring" />
<property name="username" value="root" />
<property name="password" value="zhaoxin" />
</bean>
<bean id="sf"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.shrx.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="heibernat.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="u" class="com.shrx.dao.impl.UserDAOImpl"></bean>
<bean id="user" class="com.shrx.model.User"></bean>
<bean id="userService" class="com.shrx.service.UserService"></bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean>
</beans>
package com.shrx.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.shrx.dao.UserDAO;
import com.shrx.model.User;
public class UserDAOImpl implements UserDAO {
private SessionFactory sf;
public SessionFactory getSf() {
return sf;
}
@Resource
public void setSf(SessionFactory sf) {
this.sf = sf;
}
@Override
public void save(User user) {
Session session=sf.openSession();
session.save(user);
System.out.println("OK");
}
}
package com.shrx.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.shrx.dao.UserDAO;
import com.shrx.model.User;
public class UserService {
private UserDAO userdao;
public UserDAO getUserdao() {
return userdao;
}
public void setUserdao(UserDAO userdao) {
this.userdao = userdao;
}
public void add(User user){
userdao.save(user);
}
}
测试类是:
package com.shrx.service.test;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.shrx.dao.impl.UserDAOImpl;
import com.shrx.model.User;
import com.shrx.service.UserService;
public class UserServiceTest {
@Test
public void addTest(){
ApplicationContext cx=new ClassPathXmlApplicationContext("beans.xml");
UserService service=(UserService)cx.getBean("userService");
User user=(User)cx.getBean("user");
UserDAOImpl u=(UserDAOImpl)cx.getBean("u");
service.setUserdao(u);
service.add(user);
}
}
报的异常:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 14 in XML document from class path resource [beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The prefix "context" for element "context:component-scan" is not bound.