Java 在Hibernate Spring中添加TransactionManager

Java 在Hibernate Spring中添加TransactionManager,java,spring,hibernate,transactionmanager,Java,Spring,Hibernate,Transactionmanager,添加事务管理器时出错。怎么了?:/ 很少有可能的答案是因为缺少一些hibernate库。然而,似乎所有这些都会持续下去。如何克服这个问题? 我还想向我的数据库添加一些测试数据。在哪个类中插入它更好? 多谢各位 错误: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [

添加事务管理器时出错。怎么了?:/ 很少有可能的答案是因为缺少一些hibernate库。然而,似乎所有这些都会持续下去。如何克服这个问题? 我还想向我的数据库添加一些测试数据。在哪个类中插入它更好? 多谢各位

错误:

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception 
parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested
 exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/aop 
       http://www.springframework.org/schema/aop/spring-aop-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/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">

    <context:annotation-config />
    <context:component-scan base-package="miniVLE.controller" />
    <context:component-scan base-package="miniVLE.service" />
    <context:component-scan base-package="miniVLE.beans" />
    <context:component-scan base-package="miniVLE.dao" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <!-- Declare a view resolver-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


    <!-- Connects to the database based on the jdbc properties information-->
    <bean id="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name ="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name ="url" value="jdbc:derby://localhost:1527/minivledb"/>
        <property name ="username" value="root"/>
        <property name ="password" value="123" />
    </bean>

     <!-- Declares hibernate object -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
        <!-- A list of all the annotated bean files, which are mapped with database tables-->
        <property name="annotatedClasses">
            <list>
                <value> miniVLE.beans.Course </value>
                <value> miniVLE.beans.Student </value>
                <value> miniVLE.beans.Department </value>  
                <value> miniVLE.beans.Module </value>  
                <value> miniVLE.beans.TimeSlot </value> 
            </list>
        </property>
    </bean>

    <!-- Declare a transaction manager-->
    <bean   id="hibernateTransactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEController' defined in file 
[C:\Users\1\Documents\NetBeansProjects\com3014_mini_VLE\build\web\WEB-
INF\classes\miniVLE\controller\miniVLEController.class]: BeanPostProcessor before 
instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: 
org/aspectj/lang/annotation/Aspect
调度程序Servlet:

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception 
parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested
 exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/aop 
       http://www.springframework.org/schema/aop/spring-aop-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/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">

    <context:annotation-config />
    <context:component-scan base-package="miniVLE.controller" />
    <context:component-scan base-package="miniVLE.service" />
    <context:component-scan base-package="miniVLE.beans" />
    <context:component-scan base-package="miniVLE.dao" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <!-- Declare a view resolver-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


    <!-- Connects to the database based on the jdbc properties information-->
    <bean id="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name ="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name ="url" value="jdbc:derby://localhost:1527/minivledb"/>
        <property name ="username" value="root"/>
        <property name ="password" value="123" />
    </bean>

     <!-- Declares hibernate object -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
        <!-- A list of all the annotated bean files, which are mapped with database tables-->
        <property name="annotatedClasses">
            <list>
                <value> miniVLE.beans.Course </value>
                <value> miniVLE.beans.Student </value>
                <value> miniVLE.beans.Department </value>  
                <value> miniVLE.beans.Module </value>  
                <value> miniVLE.beans.TimeSlot </value> 
            </list>
        </property>
    </bean>

    <!-- Declare a transaction manager-->
    <bean   id="hibernateTransactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEController' defined in file 
[C:\Users\1\Documents\NetBeansProjects\com3014_mini_VLE\build\web\WEB-
INF\classes\miniVLE\controller\miniVLEController.class]: BeanPostProcessor before 
instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: 
org/aspectj/lang/annotation/Aspect
服务:

@Repository
public class MiniVLEDAOImplementation implements MiniVLEDAO{

    // Used for communicating with the database
    @Autowired
    private SessionFactory sessionFactory;

@Override
public void addStudentToDB(Student student) {         
    sessionFactory.getCurrentSession().saveOrUpdate(student);        
}....
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StudentService implements IStudentService{

    @Autowired
    MiniVLEDAOImplementation dao;    

    public StudentService() {
         System.out.println("*** StudentService instantiated");
    }

    @Override
    public Student getStudent(String urn){
        Student s = dao.getStudentFromDB(urn);
        return s;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addStudent(Student student) {
        dao.addStudentToDB(student);
    }...
@Controller
public class miniVLEController {

    @Autowired
    StudentService studentService;
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: miniVLE.service.StudentService miniVLE.controller.miniVLEController.studentService; nested exception is java.lang.IllegalArgumentException: Can not set miniVLE.service.StudentService field miniVLE.controller.miniVLEController.studentService to sun.proxy.$Proxy536
控制器:

@Repository
public class MiniVLEDAOImplementation implements MiniVLEDAO{

    // Used for communicating with the database
    @Autowired
    private SessionFactory sessionFactory;

@Override
public void addStudentToDB(Student student) {         
    sessionFactory.getCurrentSession().saveOrUpdate(student);        
}....
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StudentService implements IStudentService{

    @Autowired
    MiniVLEDAOImplementation dao;    

    public StudentService() {
         System.out.println("*** StudentService instantiated");
    }

    @Override
    public Student getStudent(String urn){
        Student s = dao.getStudentFromDB(urn);
        return s;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addStudent(Student student) {
        dao.addStudentToDB(student);
    }...
@Controller
public class miniVLEController {

    @Autowired
    StudentService studentService;
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: miniVLE.service.StudentService miniVLE.controller.miniVLEController.studentService; nested exception is java.lang.IllegalArgumentException: Can not set miniVLE.service.StudentService field miniVLE.controller.miniVLEController.studentService to sun.proxy.$Proxy536
在添加aopalliance-1.0.jar之后获得下一个 错误:

@Repository
public class MiniVLEDAOImplementation implements MiniVLEDAO{

    // Used for communicating with the database
    @Autowired
    private SessionFactory sessionFactory;

@Override
public void addStudentToDB(Student student) {         
    sessionFactory.getCurrentSession().saveOrUpdate(student);        
}....
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StudentService implements IStudentService{

    @Autowired
    MiniVLEDAOImplementation dao;    

    public StudentService() {
         System.out.println("*** StudentService instantiated");
    }

    @Override
    public Student getStudent(String urn){
        Student s = dao.getStudentFromDB(urn);
        return s;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addStudent(Student student) {
        dao.addStudentToDB(student);
    }...
@Controller
public class miniVLEController {

    @Autowired
    StudentService studentService;
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: miniVLE.service.StudentService miniVLE.controller.miniVLEController.studentService; nested exception is java.lang.IllegalArgumentException: Can not set miniVLE.service.StudentService field miniVLE.controller.miniVLEController.studentService to sun.proxy.$Proxy536
解决方案之一是将
添加到dispatcher servlet中。 下一个错误:

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception 
parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested
 exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/aop 
       http://www.springframework.org/schema/aop/spring-aop-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/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">

    <context:annotation-config />
    <context:component-scan base-package="miniVLE.controller" />
    <context:component-scan base-package="miniVLE.service" />
    <context:component-scan base-package="miniVLE.beans" />
    <context:component-scan base-package="miniVLE.dao" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <!-- Declare a view resolver-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


    <!-- Connects to the database based on the jdbc properties information-->
    <bean id="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name ="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name ="url" value="jdbc:derby://localhost:1527/minivledb"/>
        <property name ="username" value="root"/>
        <property name ="password" value="123" />
    </bean>

     <!-- Declares hibernate object -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
        <!-- A list of all the annotated bean files, which are mapped with database tables-->
        <property name="annotatedClasses">
            <list>
                <value> miniVLE.beans.Course </value>
                <value> miniVLE.beans.Student </value>
                <value> miniVLE.beans.Department </value>  
                <value> miniVLE.beans.Module </value>  
                <value> miniVLE.beans.TimeSlot </value> 
            </list>
        </property>
    </bean>

    <!-- Declare a transaction manager-->
    <bean   id="hibernateTransactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEController' defined in file 
[C:\Users\1\Documents\NetBeansProjects\com3014_mini_VLE\build\web\WEB-
INF\classes\miniVLE\controller\miniVLEController.class]: BeanPostProcessor before 
instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: 
org/aspectj/lang/annotation/Aspect

您需要添加aopalliance.jar依赖项。 如果您使用maven:

<dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
</dependency>

奥帕林
奥帕林
1

问题升级后:

需要找到缺少的
aspectjrt-1.x.x.jar
以克服上一个错误,然后要克服org.aspectj.util.PartialOrder.PartialComparable错误,您需要获得
aspectjtools-1.x.x.jar

在这里我找到了我需要的所有额外的库

如果我没有使用Maven怎么办?:)您可以从下载它,然后将其添加到您的类路径中。您是否有@Autowired StudentService StudentService;也请参考这个问题,只是一个建议。如果你还没有使用maven,你应该看看它!我现在甚至在最小的项目中使用它,它非常好地支持您解决此类问题。有时会发生一些事情导致WTH???,但过一段时间后,你就会习惯于解决这些问题:)