带有PlatformTransactionManager的BeanNotOfRequiredTypeException

时间:2019-03-28 19:42:09

标签: spring spring-mvc exception transactions autowired

我是Spring的新手,我在youtube教程后做了一个登录/注册应用程序,但是我想添加一个允许删除学生的新功能。我在删除方法上使用了@Transactional,并相应地修改了xml文件,但出现此错误:

Message Request processing failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'platformTransactionManager' is expected to be of type 'org.springframework.transaction.PlatformTransactionManager' but was actually of type 'com.infotech.service.impl.StudentServiceImpl'

我的服务班级

@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDAO studentDAO;

    public void setStudentDAO(StudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }

    public StudentDAO getStudentDAO() {
        return studentDAO;
    }

    //other methods

    @Override
    public void delete(String email) {
        getStudentDAO().delete(email);
    }

}

我的DAO课程

@EnableTransactionManagement
@Repository("studentDAO")

public class StudentDAOImpl implements StudentDAO {

    @Autowired
    private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    @Autowired
    private SessionFactory sessionFactory;

    protected Session getSession() {
        return (Session) sessionFactory.getCurrentSession();
    }

    //other methods

    @Transactional("platformTransactionManager")

    public void delete(String email) {
        Student student = (Student) ((HibernateTemplate) getSession()).get(Student.class, email);
        ((HibernateTemplate) getSession()).delete(student);
    }

}

在调度程序servlet中,我定义了InternalResourceViewResolver,dataSource,hibernateTemplate,sessionFactory bean,然后添加了另一个bean

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

<bean id= "platformTransactionManager"class="com.infotech.service.impl.StudentServiceImpl">
</bean>

最后,这是控制器

@Controller
public class MyController {
    @Autowired
    private StudentService studentService;

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    public StudentService getStudentService() {
        return studentService;
    }

    //...RequestMappings...

    @RequestMapping(value = "/delete/{email}", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("email") String email) {
        studentService.delete(email);

        return new ModelAndView("redirect:/view/home");
    }

    ...
}

现在,如何使我的PlatformTransactionManager类型的bean? 但是最重​​要的是,我认为有一种更简单的方法可以从表中删除字段,也许根本不使用@Transaction,所以任何人都可以帮助我理解为什么会收到错误并向我解释什么是@Transactional,以及如果我真的应该使用它在这种情况下? 请记住,我是Spring的新手,我仍然没有很多想法,所以如果我写的东西完全愚蠢,对不起:-)

1 个答案:

答案 0 :(得分:0)

Spring正在寻找事务管理器-它需要PlatformTransactionManager接口的具体实现。正在为您提供服务实现,它不是PlatformTransactionManager,也不是它所需要的。如果您使用的是JDBC,则org.springframework.jdbc.datasource.DataSourceTransactionManager应该可以使用。

尝试更改:

<bean id= "platformTransactionManager" class="com.infotech.service.impl.StudentServiceImpl">

收件人:

<bean id= "platformTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">