spring3整合hibernate使用XML配置事务处理时总报错,请高手进来看看。

z343986392 2010-07-30 03:38:43
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.
...全文
569 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
死牛胖子 2011-11-15
  • 打赏
  • 举报
回复
context命名空间没有定义
xmlns:context="http://www.springframework.org/schema/context"
licip 2010-07-31
  • 打赏
  • 举报
回复
每一个版本的语法是不一样的。所以要注意。
qq9209999 2010-07-31
  • 打赏
  • 举报
回复
你再建个spring2.5然后替换3.0就不会错了
变态把~~~
有时候是3.0的包没有导进去~~~麻烦哈
sustbeckham 2010-07-30
  • 打赏
  • 举报
回复
spring3?
2.5时候这样写是没错的 3就不知道了



<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">

luomt_1987 2010-07-30
  • 打赏
  • 举报
回复
这个只是spring的配置问题。不关hibernate事,你的hibernate配置没问题。你查查看。。
jack914 2010-07-30
  • 打赏
  • 举报
回复
<context:component-scan base-package="com.shrx" />
这句。。。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧