我正在使用java配置,除了@transactional之外一切正常,我花了很多时间试图弄明白,但不知道为什么它不起作用,任何帮助都会非常值得欣赏。
在UserController.java中,我尝试调用 userService.testTransactional(user,request);
testTransaction()是UserServiceImpl中的测试方法,“Long.valueOf(”Throw RuntimeException“);”是在这个方法中抛出异常的行,但无论如何都要添加用户,它应该回滚,但是用户记录仍然存在
UserController.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 {
...
DataConfig.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());
}
答案 0 :(得分:1)
问题是您没有在@EnableTransactionManagement
类上指定@Configuration
组件扫描并创建UserServiceImpl
bean。假设该类在com.myapp.account
中,则组件扫描它的配置类应该具有@EnableTransactionManagement
。适当更改DataConfig
或AppConfig
。