Spring 缓存未创建代理 class
Spring Cache not creating proxy class
我正在尝试在组件 bean 上添加缓存,但 spring 没有创建代理 class。如果我将 @Cacheable 放在 repo 上,那么它就可以正常工作。我目前只是在每次调用后查看 cacheManager 内部来验证这一点。我在这里做错了什么??
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("clientids");
}
}
@Service
@RequiredArgsConstructor
public class CacheDelegator {
private final RepoUser getFromRepo;
public ClientReference getId(String email) {
return getFromRepo.getFromRepo(email).orElseThrow(() ->
new ClientNotFoundException(ExceptionType.NOT_FOUND));
}
}
@Component
@RequiredArgsConstructor
public class RepoUser {
private final ClientRepository repository;
@Cacheable("clientids")
Optional<ClientReference> getFromRepo(String email) {
return repository.findClientIdByEmail(email);
}
}
@Repository
public interface ClientRepository extends JpaRepository<ClientReference, Long> {
Optional<ClientReference> findClientIdByEmail(String email);
}
我知道这是一种迂回的做法。不过,我正在尝试了解如何创建缓存拦截器。
根据上面的 Deinum。我的方法需要 public.
我正在尝试在组件 bean 上添加缓存,但 spring 没有创建代理 class。如果我将 @Cacheable 放在 repo 上,那么它就可以正常工作。我目前只是在每次调用后查看 cacheManager 内部来验证这一点。我在这里做错了什么??
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("clientids");
}
}
@Service
@RequiredArgsConstructor
public class CacheDelegator {
private final RepoUser getFromRepo;
public ClientReference getId(String email) {
return getFromRepo.getFromRepo(email).orElseThrow(() ->
new ClientNotFoundException(ExceptionType.NOT_FOUND));
}
}
@Component
@RequiredArgsConstructor
public class RepoUser {
private final ClientRepository repository;
@Cacheable("clientids")
Optional<ClientReference> getFromRepo(String email) {
return repository.findClientIdByEmail(email);
}
}
@Repository
public interface ClientRepository extends JpaRepository<ClientReference, Long> {
Optional<ClientReference> findClientIdByEmail(String email);
}
我知道这是一种迂回的做法。不过,我正在尝试了解如何创建缓存拦截器。
根据上面的 Deinum。我的方法需要 public.