使用PlatformTransactionManager的BeanNotOfRequiredTypeException

问题描述 投票:0回答:1

我是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很新,我仍然没有很多想法,如果我写了一些完全愚蠢的东西,那就很抱歉:-)

spring spring-mvc exception transactions autowired
1个回答
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">
© www.soinside.com 2019 - 2024. All rights reserved.