我有一个 bean,它基本上是一个使用@Transactional
注释进行事务处理的服务。我正在使用休眠,这里是 applicationContext.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:p="http://www.springframework.org/schema/p"
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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db"/>
<property name="user" value="username"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="annotatedClasses">
<list>
<value>com.myprojects.User</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userService" class="com.myprojects.service">
<property name="template" ref="hibernateTemplate"/>
</bean>
</beans>
我是spring和hibernate的新手,当我在这里浏览spring transaction management的文档时,我尝试像上面的例子一样实现一个简单的基于事务的bean,但是在部署它时,我得到了beancreationexception。我是否缺少任何配置?
堆栈跟踪:
2013 年 9 月 29 日晚上 9:25:29 org.apache.catalina.core.StandardContext loadOnStartup 严重:Servlet /myWorld 抛出 load() 异常 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 [com.myprojects. myworld.daoimpl.UsersDAOImpl] 找到依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注释:在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:986) 在 org.springframework.beans 的 {@org.springframework.beans.factory.annotation.Autowired(required=true)}。 org.springframework.beans.factory.support.DefaultListableBeanFactory 中的 factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856)。
我有一个自动连接服务的控制器:
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private service s;
//few methods like getUser/postUser are written here
}
服务:
@Transactional
public class service {
private HibernateTemplate template;
public void updateUser(User u) {
Session session = template.getSessionFactory().getCurrentSession();
u.update(session);
}
}
用户等级:
public class User {
public void updateUser(Session session) {
Transaction tx = session.beginTransaction();
try {
//carry out update user
} catch(Exception e) {
tx.rollback();
} finally {
session.close();
}
}
}
休眠配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
myproject-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<context:component-scan base-package="com.myprojects"/>
<context:annotation-config/>
<tx:annotation-driven/>
<mvc:annotation-driven/>
</beans>
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfiguration</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>myproject</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myproject-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myproject</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
</web-app>