Cherish蔡
Cherish蔡

Reputation: 33

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

Don't know how the transaction doesn't work, I will struggle for a long time, tried a variety of solutions, found that is not being dynamic proxy, so @Transactional didn't work


And then do a complete copy for UserService to 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());

    }

}

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

Go against the problem of present in front of the lack of experience a rookie, kneel down! For help

Same code in UserService and UserService2

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

Main message with 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.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)

My ShiroConfiguration

@Slf4j

@Configuration public class 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;
}

}

and MShiroRealm

@Slf4j
public class MShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;


    }

Thank you for reading ;)

Upvotes: 2

Views: 1933

Answers (2)

Cherish蔡
Cherish蔡

Reputation: 33

When I change the ShiroConfiguration add this

@Autowired
private UserService userService;

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

and the MShiroRealm changes to Use Constructor to inject UserService

private final UserService userService;

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

The UserService can be proxy, Thanks!

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

Upvotes: 1

Frederik Heremans
Frederik Heremans

Reputation: 658

Are you sure UserService and UserService2 have exactly the same annotations on them?

Your UserService2 class is indeed proxied, so I'd just delete UserService and rename the UserService2 ;)

Upvotes: 1

Related Questions