spring Cglib很有用,但是@Transactional类没有代理

时间:2017-02-07 13:32:48

标签: java spring spring-transactions cglib

不知道交易是如何运作的,我会挣扎很久,尝试了各种解决方案,发现这不是动态代理,所以@Transactional没有&#39 ;工作

然后为UserServiceUserService2

制作完整副本
@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());

    }

}

console.debug

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();
    }
}

Bean&#39; userService&#39;的主要消息类型[class cn.cherish.mboot.service.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)

我的ShiroConfiguration

@Slf4j

@Configuration 公共课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;


    }

感谢您阅读;)

2 个答案:

答案 0 :(得分:1)

您确定UserServiceUserService2对它们有完全相同的注释吗?

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

答案 1 :(得分:1)

当我更改ShiroConfiguration时添加此

@Autowired
private UserService userService;

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

并且MShiroRealm更改为Use Constructor以注入UserService

private final UserService userService;

@Autowired
public MShiroRealm(UserService userService) {
    super();
    this.userService = userService;
}

UserService可以是代理,谢谢!

mShiroRealm.getClass() = class cn.cherish.mboot.extra.shiro.MShiroRealm
userService.getClass() = class cn.cherish.mboot.service.UserService$$EnhancerBySpringCGLIB$$a3d37fd7
roleService.getClass() = class cn.cherish.mboot.service.RoleService$$EnhancerBySpringCGLIB$$9b824aa6
permissionService.getClass() = class cn.cherish.mboot.service.PermissionService$$EnhancerBySpringCGLIB$$7e6ad695