Spring 无法识别 @Transactional 注释

巴马格洛特1990

我需要帮助。Spring 似乎无法识别我用 @Transactional 注释的方法。我看了很多解决方案的来源,但找不到一个。而且我需要准确地注释 dao 方法,而不是服务(我知道这是最佳实践)。PS抱歉我的英语不好。

我的 appInitializer:

package com.dreamteam.datavisualizator.common.configurations;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{ServletContext.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{ApplicationContext.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

我的servletContext:

package com.dreamteam.datavisualizator.common.configurations;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@ComponentScan(basePackages = "com.dreamteam.datavisualizator")
@EnableWebMvc
@EnableTransactionManagement
public class ServletContext extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

我的应用程序上下文:

package com.dreamteam.datavisualizator.common.configurations;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
public class ApplicationContext {

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl(System.getenv("SQL_JDBC_URL"));
        dataSource.setUsername(System.getenv("SQL_LOGIN"));
        dataSource.setPassword(System.getenv("SQL_PASSWORD"));
        return dataSource;
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(getDataSource());
    }

    @Bean(name="generalTemplate")
    public JdbcTemplate getJdbcTemplate(){
        return new JdbcTemplate(getDataSource());
    }

    @Bean(name="simpleCallTemplate")
    public SimpleJdbcCall getSimpleJdbcCall(){
        return new SimpleJdbcCall(getDataSource());
    }
}

我的 Dao 类(在这个方法中可以有更多的 SimpleJdbcCall/ 调用,但例如):

@Repository("userDaoImpl")
public class UserDAOImpl implements UserDAO {
    private enum UserColumnName {ID, FIRST_NAME, LAST_NAME, EMAIL}

    @Autowired
    private JdbcTemplate generalTemplate;

    @Autowired
    private SimpleJdbcCall simpleCallTemplate;

    //...

    @Transactional
    public BigInteger createObject(BigInteger object_id, String name) {
        simpleCallTemplate.withFunctionName(INSERT_OBJECT);
        SqlParameterSource in = new MapSqlParameterSource()
                .addValue("obj_type_id", object_id)
                .addValue("obj_name", name);
        return simpleCallTemplate.executeFunction(BigDecimal.class, in).toBigInteger();
    }

    //...

    private String INSERT_OBJECT = "insert_object";
}
喝醉的爸爸

您的配置似乎是正确的。我相信它不起作用,因为您没有指定 rollbackFor

@Transactional(value = "transactionManager", rollbackFor = java.lang.Exception.class)
public BigInteger createObject(BigInteger object_id, String name) {
    simpleCallTemplate.withFunctionName(INSERT_OBJECT);
    SqlParameterSource in = new MapSqlParameterSource()
            .addValue("obj_type_id", object_id)
            .addValue("obj_name", name);
    return simpleCallTemplate.executeFunction(BigDecimal.class, in).toBigInteger();
}

现在,如果您的方法中出现 java.lang.Exception 类型的异常,它将回滚所有更改

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring忽略@Transactional注释

Spring @Transactional注释被忽略

Spring忽略@Transactional注释

Spring @Transactional注释的行为很奇怪

如何扩展Spring注释@Transactional

Spring @Transactional注释不起作用

Spring @Transactional 注释不起作用

关于方法上的Spring @Transactional注释的一些说明

如何在mybatis-spring中使用@Transactional注释?

Spring Data JPA上的嵌套@Transactional注释行为

Spring Boot 2 @Transactional注释使Autowired字段为空

Spring交易支持JPA + Hibernate + @ Transactional注释的奇怪行为

使用try catch块时的Spring @Transactional注释

@Transactional注释rollbackFor值在Spring Boot中不起作用

Spring @Transactional无法正常工作

Cloud Foundry无法识别@Transactional

@Transactional注释在哪里?

JDBC Spring数据@Transactional无法正常工作

Spring Boot @ControllerAdvice + @Transactional无法正常工作

Spring Boot 应用程序中的存储库无法识别 @Autowired 注释

IntelliJ:无法识别Spring注释/不触发自动导入。手工进口工作

Spring框架在用限定符注释标记后无法识别组件

使用构造型注释创建 Bean 时,Spring Context 似乎无法识别 Bean。抛出 NoSuchBeanDefinitionException

在不与数据库对话的方法中使用Spring的@Transactional注释是否有意义?

在Java 8默认接口方法上使用Spring @Transactional注释是否安全?

Spring @Transactional注释不能与自动装配一起使用?

从服务类中调用Spring @Transactional时,此方法不适用于带注释的方法

Envers审计表在使用@Transactional注释的spring-boot集成测试中不会回滚

为什么我们需要在 Spring Data JPA 中使用 @Transactional 注释 Service 类