上午實現自定義所有的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。
怎麼樣把一個春天的註釋,比如' TenantAwareRepositoryImpl上的@ Component'或'@ Repository'? –