我目前面临Spring MVC的一个问题:如果我创建了一个休息控制器并将其标记为@Transient
它没有被ServletDispatcher
映射(404如果被调用),如果我删除注释控制器正确映射,一切都很好。如果我将服务标记为事务并从控制器内部调用它,一切都很好(事务开始并正确完成)。不幸的是,现在将我的服务标记为事务性是不可行的(我正在使用遗留系统)。在撰写本文时,我使用的是最新的春季版本(4.3.9)。
我目前的配置:
<servlet>
<servlet-name>api</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/api-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>api</servlet-name>
<url-pattern>/v2/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
事务管理器配置(资源在我的applicationContext.xml中定义):
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "my.package")
public class TransactionManagerConfiguration {
@Resource(name = "entityManager")
private EntityManagerFactory entityManager;
@Resource(name = "dataSource")
private DataSource dataSource;
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager() {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(entityManager);
tm.setDataSource(dataSource);
return tm;
}
}
我的控制器:
@RestController
@RequestMapping("/accounts")
public class AccountsController {
@GetMapping
@Transactional
public List<Account> doStuff() {
//stuff...
return stuff;
}
}
我的api-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.1.xsd">
<context:component-scan base-package="my.package"/>
</beans>
我省略了我的applicationContext.xml
,因为它没有任何重要内容(只是一堆BL bean和包扫描)。
答案 0 :(得分:0)
感谢M. Deinum的评论,找到了解决方案。在我的情况下,我不得不将set proxy target class添加为true
@EnableTransactionManagement(proxyTargetClass = true)
您可以在此处找到更多详细信息:https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-ann-requestmapping-proxying