我配置了Spring Aop和Transaction。 当我运行项目测试时。事务不起作用,并且我得到了如下日志输出:“未注册同步,因为未激活同步”
当我运行LoginServiceImpl.findMemberInfo()方法时,我成功注册了事务。但是,我运行LoginServiceImpl.insertMember,注册失败
root-context.xml如下所示:
<context:component-scan base-package:"com.quapenus">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- transactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- txAdvice -->
<tx:annotation-driven mode="aspectj" />
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="find*" />
<tx:method name="insert*" />
</tx:attributes>
</tx:advice>
<!-- Aspect -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.quapenus.*.service.*Impl.*(..))" />
<aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice" />
</aop:config>
servlet上下文,如下所示:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="jsonMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />
<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="contentType" value="application/json;charset=UTF-8" />
<property name="objectMapper" ref="jsonMapper" />
</bean>
LoginServiceImpl如以下所示:
@Service
public class LoginServiceImpl implements LoginService {
Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
@Autowired
private LoginDao loginDao;
@Override
public Member findMemberInfo(String username) throws UsernameNotFoundException {
Member member = loginDao.findMemberInfo(username);
if(member == null){
throw new UsernameNotFoundException("아이디가 존재하지 않습니다.");
}
return member;
}
@Override
public List<GrantedAuthority> findRoles(String username) throws NotFoundException {
List<GrantedAuthority> roles = loginDao.findRoles(username);
if(roles.size() == 0){
throw new NotFoundException("해당 역할이 없습니다.");
}
return roles;
}
@Override
public void insertMember(Member member) throws RollbackException {
int result = loginDao.insertMember(member);
if(result == 1){
throw new RollbackException("Test Tx");
}
}
}
LoginDao如下所示:
@Repository
public class LoginDao {
@Autowired
private SqlSession sqlSession;
public Member findMemberInfo(String username) {
return sqlSession.selectOne("com.quapenus.common.sql.LoginMapper.findMemberInfo", username);
}
public List<GrantedAuthority> findRoles(String username) {
return sqlSession.selectList("com.quapenus.common.sql.LoginMapper.findRoles", username);
}
public int insertMember(Member member) {
return sqlSession.insert("com.quapenus.common.sql.LoginMapper.insertMember", member);
}
}
LoginServiceImpl.insertMember()方法日志,如下所示:
org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@77b976a0] was not registered for synchronization because synchronization is not active
17:13:01.100 [http-nio-8080-exec-6] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
LoginServiceImpl.findMemberInfo()方法日志,如下所示:
org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
17:58:07.052 [http-nio-8080-exec-9] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession
github存储库:https://github.com/Sujin1135/Quapenus