2

我正在使用java配置,除了@transactional,一切都很好,我花了几天的时间试图弄清楚,但不知道为什么它不起作用,任何帮助将不胜感激。

在 UserController.java 中,我尝试调用userService.testTransactional(user, request);

testTransaction()是UserServiceImpl中的测试方法,"Long.valueOf("Throw RuntimeException");" 是在此方法中将引发异常的行,但无论如何都会添加用户,它应该回滚,但用户记录仍然存在

用户控制器.java

@Controller
public class UserController {

    @Resource(name="userService")
    protected UserService userService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processRegister(Model model, @ModelAttribute RegisterForm registerForm, HttpServletRequest request) throws Exception {

        userService.testTransactional(user, request);

        return "account/register_success";
    }
}

UserServiceImpl.java

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Transactional
    public void testTransaction(User user, HttpServletRequest request) throws Exception {
        long userId = userDao.add(user);
        Long.valueOf("Throw RuntimeException");
    }
}

下面是我的配置

web.xml

<!-- Java-based annotation-driven Spring container definition -->
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<!-- Location of Java @Configuration classes that configure the components that makeup this application -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.myapp.config</param-value>
</context-param>


<!-- Secures the application -->
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher> 
</filter-mapping>

<!-- Handles requests into the application -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <!-- No explicit configuration file reference here: everything is configured in the root container for simplicity -->       
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

AppConfig.java

@Configuration  // Specifies the class as configuration
@EnableWebMvc //Enables to use Spring's annotations in the code
@ComponentScan(basePackages = {"com.myapp.account"}) // Specifies which package to scan
public class AppConfig {
    ...

数据配置.java

@Configuration
@EnableTransactionManagement
public class DataConfig {

    @Bean(destroyMethod="close")
    public ComboPooledDataSource dataSource() throws PropertyVetoException {
        ...
        return cpds;
    }

    @Bean
    public NamedParameterJdbcTemplate jdbcTemplate() throws PropertyVetoException {
        return new NamedParameterJdbcTemplate(dataSource());
    }

    @Bean
    public DataSourceTransactionManager transactionManager() throws PropertyVetoException {
        DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource());
        return dstm;
    }


    @Bean
    public UserDao userDao() throws PropertyVetoException {
        return new UserDao(jdbcTemplate());
    }
4

1 回答 1

1

问题是您没有指定组件扫描和创建bean@EnableTransactionManagement的类。假设该类在,那么组件扫描它应该具有的配置类。适当地改变你的或你的。@ConfigurationUserServiceImplcom.myapp.account@EnableTransactionManagementDataConfigAppConfig

于 2013-10-07T12:33:07.587 回答