簡體   English   中英

spring Cglib 很有用,但 @Transactional 類不代理

[英]spring Cglib is useful but a class @Transactional don't proxy

不知道事務怎么不行,折騰了半天,試了各種方案,發現不是動態代理,所以@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' 的主消息不符合被所有 BeanPostProcessor 處理的條件(例如:不符合自動代理的條件)

    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

@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;


    }

感謝您閱讀;)

您確定UserServiceUserService2上的注釋完全相同嗎?

您的UserService2類確實是代理的,所以我只需刪除 UserService 並重命名 UserService2 ;)

當我更改 ShiroConfiguration 時添加這個

@Autowired
private UserService userService;

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

並且 MShiroRealm 更改為使用構造函數注入 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM