我所使用的SpringMVC是基于注解的,Controller是用@Controller注解标记的,而service和dao都是用的@Component标记的,由于配置上的一点小失误导致事务没有加上去,所以才会提示FlushMode为只读。解决方法是,把service和Controller分开管理,不用通用的Component,这样在加载SpringMVC配置文件的时候就只实例化Controller对象,在加载spring配置文件的时候就只实例化service对象。总之,如果事务是加在service上,那么就需要把service和Controller加以区别,而dao对象在什么时候初始化都可以,可以继续使用通用的Component注解。
修改后的配置文件的写法如下:
SpringMVC配置文件:
<context:component-scan base-package="com.tiantian" > <!-- 将service排除在外,意味着这个时候不初始化service --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>
spring配置文件:
<context:component-scan base-package="com.tiantian" > <!-- 将controller排除在外,意味着这个时候不初始化controller --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
本文详细介绍了在SpringMVC中,如何通过组件扫描排除特定类型的注解,来实现Controller与Service对象的独立管理,解决事务配置错误导致的FlushMode为只读的问题。通过修改配置文件,确保Service对象仅在Spring配置文件中实例化,而Controller对象则在SpringMVC配置文件中管理,以避免事务冲突。
232

被折叠的 条评论
为什么被折叠?



