小编因为这个问题被坑了整整一天,一直在纠结SpringBoot集成MyBatis Plus的问题,然后网上各种解决方案,均不是。
问题
Springboot集成Shiro导致事务不生效!
解决方案(推荐方案二)
方案一
在Realm中的注入Service上添加@Lazy注解,进行懒加载即可,如:
UserRealm.java
@Resource
@Lazy
private SysUserService sysUserService;
方案二
ShiroConfig.java
/**
* @return org.apache.shiro.mgt.SessionsSecurityManager
* @description 配置security并设置userReaml,避免xxxx required a bean named 'authorizer' that could not be found.的报错
* @author Dongyun
* @date 2020-03-03 08:43:59
*/
@Bean
public SessionsSecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
return securityManager;
}
/**
* @return com.dongyunit.eims.shiro.UserRealm
* @description 将自定义的Realm配置加入容器
* @author Dongyun
* @date 2020-03-03 08:44:38
*/
@Bean
public UserRealm userRealm() {
UserRealm userRealm = new UserRealm();
return userRealm;
}
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
ApplicationContext context = event.getApplicationContext();
DefaultWebSecurityManager manager = (DefaultWebSecurityManager) context.getBean("securityManager");
AuthorizingRealm realm = (AuthorizingRealm) context.getBean("userRealm");
realm.setCredentialsMatcher(credentialsMatcher());
manager.setRealm(realm);
}
注:启动类无需添加@EnableTransactionManagement注解!因为Springboot默认已开启。
评论前必须登录!
注册