java - spring Cglib 很有用,但 @Transactional 类不代理

标签 java spring spring-transactions cglib

不知道事务怎么不行,我纠结了好久,尝试了多种解决方案,发现不是动态代理,所以@Transactional没有工作


然后将 UserService 完整复制到 UserService2

@RunWith(SpringRunner.class)
@SpringBootTest
public class MbootApplicationTests {

    @Autowired
    private UserService userService;
    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UserService2 userService2;

    @Test
    public void contextLoads() {

        System.out.println("userService.getClass() = " + userService.getClass());
        System.out.println("roleService.getClass() = " + roleService.getClass());
        System.out.println("permissionService.getClass() = " + permissionService.getClass());
        System.out.println("userService2.getClass() = " + userService2.getClass());

    }

}

控制台.调试

userService.getClass() = class cn.cherish.mboot.service.UserService
    roleService.getClass() = class cn.cherish.mboot.service.RoleService$$EnhancerBySpringCGLIB$$ef060f5f
    permissionService.getClass() = class cn.cherish.mboot.service.PermissionService$$EnhancerBySpringCGLIB$$d1ee9b4e
    userService2.getClass() = class cn.cherish.mboot.service.UserService2$$EnhancerBySpringCGLIB$$63856500

针对现在菜鸟缺乏经验的问题,跪下!寻求帮助

UserServiceUserService2 中的代码相同

@Slf4j
@Service
@CacheConfig(cacheNames = "users")
@Transactional(readOnly = true)
public class UserService2 extends ABaseService<User, Long> {

    @Autowired
    private UserDAO userDAO;


    @Override
    protected IBaseDAO<User, Long> getEntityDAO() {
        return userDAO;
    }

    @Cacheable(key = "'username_' + #username", unless = "#result==null")
    public User findByUsername(String username) {
        log.debug("username_{}没有缓存", username);
        return userDAO.findByUsername(username);
    }

    public boolean exist(String username) {
        return userDAO.findByUsername(username) != null;
    }

    @Cacheable(key = "'countAllUser'")
    public Long getCount() {
        log.debug("countAllUser没有缓存");
        return userDAO.count();
    }
}

类型为 [class cn.cherish.mboot.service.UserService] 的 Bean“userService”的主消息不符合所有 BeanPostProcessors 的处理条件(例如:不符合自动代理的条件)

    2017-02-08 08:47:10.464  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'userDAO' of type [class org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.464  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'userDAO' of type [class com.sun.proxy.$Proxy114] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.465  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'userService' of type [class cn.cherish.mboot.service.UserService] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.466  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'mShiroRealm' of type [class cn.cherish.mboot.extra.shiro.MShiroRealm] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.486  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'securityManager' of type [class org.apache.shiro.web.mgt.DefaultWebSecurityManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.487  INFO 7064 --- [           main] c.c.mboot.config.ShiroConfiguration      : ##################从数据库读取权限规则,加载到shiroFilter中##################
2017-02-08 08:47:10.499  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'filterChainDefinitionMap' of type [class java.util.LinkedHashMap] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.545  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'authorizationAttributeSourceAdvisor' of type [class org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.620  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$7412c5f3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.851  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cache.annotation.ProxyCachingConfiguration' of type [class org.springframework.cache.annotation.ProxyCachingConfiguration$$EnhancerBySpringCGLIB$$bd24fc11] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.897  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration' of type [class org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration$$EnhancerBySpringCGLIB$$b198c0b5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.934  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.cache-org.springframework.boot.autoconfigure.cache.CacheProperties' of type [class org.springframework.boot.autoconfigure.cache.CacheProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.949  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers' of type [class org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.958  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration' of type [class org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration$$EnhancerBySpringCGLIB$$4c378db3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:10.992  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'ehCacheCacheManager' of type [class net.sf.ehcache.CacheManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:11.018  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'cacheManager' of type [class org.springframework.cache.ehcache.EhCacheCacheManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-08 08:47:11.027  INFO 7064 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'cacheAutoConfigurationValidator' of type [class org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration$CacheManagerValidator] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

我的 Shiro 配置

@Slf4j

@配置 公共(public)类 ShiroConfiguration {

@Bean
public ShiroDialect shiroDialect() {
    return new ShiroDialect();
}

@Bean
public EhCacheManager ehCacheManager() {
    EhCacheManager em = new EhCacheManager();
    em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");
    return em;
}

@Bean(name = "mShiroRealm")
public MShiroRealm mShiroRealm() {
    MShiroRealm realm = new MShiroRealm();
    realm.setCacheManager(ehCacheManager());
    return realm;
}

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
    filterRegistration.setFilter(new DelegatingFilterProxy("shiroFilter"));
    filterRegistration.addInitParameter("targetFilterLifecycle", "true");
    filterRegistration.setEnabled(true);
    filterRegistration.addUrlPatterns("/*");
    return filterRegistration;
}

@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(/*, permissionDao*/) {

    ShiroFilterFactoryBean shiroFilterFactoryBean = new MShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager());
    shiroFilterFactoryBean.setLoginUrl("/login");
    shiroFilterFactoryBean.setSuccessUrl("/admin");
    shiroFilterFactoryBean.setUnauthorizedUrl("/403");

    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap());

    return shiroFilterFactoryBean;
}

@Bean(name = "filterChainDefinitionMap")
public Map<String, String>  filterChainDefinitionMap() {
    filterChainDefinitionMap.put("/admin/**", "authc");

    filterChainDefinitionMap.put("/user/**", "user");
    filterChainDefinitionMap.put("/login", "anon");
    filterChainDefinitionMap.put("/logout", "logout");
    filterChainDefinitionMap.put("/**", "anon");

    return filterChainDefinitionMap;
}

@Bean(name = "securityManager")
public DefaultWebSecurityManager defaultWebSecurityManager() {
    DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
    dwsm.setRealm(mShiroRealm());
    dwsm.setCacheManager(ehCacheManager());
    return dwsm;
}

@Bean(name = "lifecycleBeanPostProcessor")
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
    return new LifecycleBeanPostProcessor();
}

@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
    DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
    daap.setProxyTargetClass(true);
    return daap;
}

@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
    AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
    aasa.setSecurityManager(defaultWebSecurityManager());
    return aasa;
}

}

和 MShiroRealm

@Slf4j
public class MShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;


    }

感谢您的阅读;)

最佳答案

您确定UserServiceUserService2具有完全相同的注释吗?

您的 UserService2 类确实已被代理,因此我只需删除 UserService 并重命名 UserService2 ;)

关于java - spring Cglib 很有用,但 @Transactional 类不代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42091178/

相关文章:

java - Hibernate 在插入子对象时插入重复的父对象

java - 使用 Spring websocket 时无事务

java - 如何将文件从一台电脑复制到另一台电脑

java - 您如何在 Maven pom.xml(来自 Ant)中设置 java.security.policy?

java - GWT-Rpc 如何返回文件以便浏览器弹出下载对话框

java - 如何使JTree选择模型只允许兄弟选择?

spring - 如何禁用 Spring 启动执行器 Elasticsearch 健康请求?

Spring Cloud Bus,如何在/bus/refresh?destination=中指定多个目的地?

spring - 现实世界中的隔离级别用例

java - 与 Spring Data 双向关系的@Transactional 返回 null