2017-04-04 72 views
0

上午实现自定义所有的Spring数据JPA库像自定义内注射豆所有库

@NoRepositoryBean 
public interface TenantAwareRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> { 

@Override 
Page<T> findAll(Pageable pageable); 
} 

并实现它像

@Slf4j 
public class TenantAwareRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements UserAwareRepository<T, ID>{ 

    @Autowired 
    private ResourceServerTokenServices defaultTokenServices; 

    private final EntityManager entityManager; 

    public TenantAwareRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { 
     super(entityInformation, entityManager); 

     // Keep the EntityManager around to used from the newly introduced methods. 
     this.entityManager = entityManager; 
    } 

    public Page<T> findAll(Pageable pageable){ 
     OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication(); 
     OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails(); 
     log.info("Token = {}", details.getTokenValue()); 
     return null; 
    } 

} 

可惜defaultTokenServices不会被注入并null。如何在自定义所有存储库实现中注入spring bean。

+2

怎么样把一个春天的注释,比如' TenantAwareRepositoryImpl上的@ Component'或'@ Repository'? –

回答

0

我想自定义行为添加到所有存储库:

THS的问题是,TenantAwareRepositoryImpl类应该implementTenantAwareRepository如下所示:

@Component 
@Slf4j 
public class TenantAwareRepositoryImpl<T, ID extends Serializable> 
    extends SimpleJpaRepository<T, ID> implements TenantAwareRepository<T, ID>{ 
    //add your code here 
} 
+0

您的回答假设我正试图将自定义行为添加到单个存储库。那么我试图将自定义行为添加到所有的存储库,并遵循文档中提到的步骤。定制存储库实现工作,并能够设置一个断点和调试。但问题是我们无法在自定义存储库实现中注入Spring bean。 – FFL

+0

您可以尝试将'@ Component'添加为TenantAwareRepositoryCustomImpl的类级别注释吗? – developer

+0

我明白了,看看上面的答案 – developer