0
votes

I'm using the latest version of spring and using the caching concept. My (rest) service class seems to not be injected with the caching annotation. If I remove them it works perfectly however I don't use cache which is not what I want.

Application:

@SpringBootApplication
@EnableCaching
public class MyApplication{

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("MyCache")));
        return cacheManager;
    }

The service:

@CacheConfig(cacheNames = "MyCache")
@Service
public class MyServiceImpl implements MyService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final String errormessage = "Error getting books";
    @Autowired
    private UserRepositoryCrud userRepository;

    public MyServiceImpl () {

    }

    @Override
    @Cacheable(value = "MyCache", key = "#description", unless = "#result?.size() > 0")
    public final List<Books> getBooks(String description) {
        logger.debug("Starting getBooksService");
        //service implementation ...

(I also have a Restcontroler that only call this service) When calling this method getBooks, I got a nullpointer on the logger, but when debugging I realize that everything is null, even the errormessage string... If I remove the @Cacheable annotation, it then works but I do not have the cache working which is not what I want.

Do you know what can be wrong ?

Thanks a lot,

1
That sample looks relatively small. Can you share the project or a link to a repo so that we can run this ourselves? - Stephane Nicoll
Hello, it might be difficult to share, can i post other classes or conf file you would need to have a look? - Thiago

1 Answers

0
votes

Guys I found the issue... because the method was final!!! I spent a lot of time to find this!