读书人

spring3整合hibernate使用XML配置事务

发布时间: 2012-02-19 19:43:37 作者: rapoo

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.


[解决办法]
spring3?
2.5时候这样写是没错的 3就不知道了


XML code
<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop                                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
[解决办法]
你再建个spring2.5然后替换3.0就不会错了
变态把~~~
有时候是3.0的包没有导进去~~~麻烦哈

读书人网 >J2EE开发

热点推荐