2017-02-07 4 views
2

トランザクションがどのように動作しないのかわからない、私は長い間闘い、ソリューションのさまざまな試み、動的プロキシではない、そう@TransactionalスプリングCglibは便利ですが、クラス@Transactionalはプロキシをしません


を動作しませんでした。そして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のための完全なコピーを行う

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

タイプの[クラスcn.cherish.mboot.service.UserService]のビーンズ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; 


    } 

は読みいただき、ありがとうございます;)

+0

[これは質問の絵である](http://img.blog.csdn.net/20170207201619134?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FpbWVuZ3l1YW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA= =/dissolve/70/gravity/SouthEast) –

+0

[およびその他](http://img.blog.csdn.net/20170207202519012?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FpbWVuZ3l1YW4=/font/5a6L5L2T/fontsize/400/fill/ I0JBQkFCMA ==/dissolve/70/gravity/SouthEast) –

+0

[このような私の問題の根源は ](http://stackoverflow.com/questions/31403278/spring-boot-1-2-5-configuration-autowired- annotation-null) –

答えて

1

あなたは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の変更を追加変更すると

+0

私はUserServiceの名前をUserService2にコピーします。したがって、それらはまったく同じコードです。エラーなしでコード自体を証明するかどうか。 他のスプリングインジェクションがUserServiceに影響を与えるかどうか? 私は英語のプールです、申し訳ありません! –

+0

私の質問のコメント、2枚の写真をご覧ください。 –

+0

'@CacheConfig(cacheNames =" users ")'と何か関係がありますか?たぶん、彼らは何らかの種類の同じユニークな 'users'名のために競合しているのでしょうか?その結果、豆のうち1つだけが代理され、残りの部分はそのまま残っていますか?長いショット、私は知っている - ちょうど感謝... –

1

) 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 
関連する問題