Why @Cachable works on not public method in Spring Boot 3.0

I’m using Spring Boot 3.x, and the @Cacheable annotation is working on methods that are not public. This is strange because the documentation clearly states:

Method visibility and cache annotations

When you use proxies, you should apply the cache annotations only to methods with public visibility. If you do annotate protected, private, or package-visible methods with these annotations, no error is raised, but the annotated method does not exhibit the configured caching settings. Consider using AspectJ (see the rest of this section) if you need to annotate non-public methods, as it changes the bytecode itself.

Here is my sample service:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Service
public class CacheService {
@Cacheable("cache1")
String foo() {
System.out.println("string");
return "string";
}
}
</code>
<code>@Service public class CacheService { @Cacheable("cache1") String foo() { System.out.println("string"); return "string"; } } </code>
@Service
public class CacheService {

    @Cacheable("cache1")
    String foo() {
        System.out.println("string");
        return "string";
    }
}

And a very simple test:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@SpringBootTest
class ApplicationTests {
@Autowired
private CacheService cacheService;
@Test
void test2() {
cacheService.foo();
cacheService.foo();
cacheService.foo();
cacheService.foo();
cacheService.foo();
cacheService.foo();
}
}
</code>
<code>@SpringBootTest class ApplicationTests { @Autowired private CacheService cacheService; @Test void test2() { cacheService.foo(); cacheService.foo(); cacheService.foo(); cacheService.foo(); cacheService.foo(); cacheService.foo(); } } </code>
@SpringBootTest
class ApplicationTests {
    @Autowired
    private CacheService cacheService;

    @Test
    void test2() {
        cacheService.foo();
        cacheService.foo();
        cacheService.foo();
        cacheService.foo();
        cacheService.foo();
        cacheService.foo();
    }

}

In Spring Boot 2.7, the console output shows 6 “string” messages, but in 3.0.0 (and above), it only shows 1 “string”.

Do you have any idea what changed, and where in the documentation this behavior is described?

This came up during a company presentation, and I was confident I understood the behavior of this annotation, so I’m looking for arguments to justify it 🙂

EDIT

Here’s the example with branch spring-2.7 and spring-3.0.

You are not using Spring’s default JDK interface proxies but are directly proxying a class, i.e. the result will be a CGLIB proxy, which basically is a subclass of the original class. I.e., non-public methods can and will be proxied and delegated to their original target methods, as long as they are not private. This is the expected behaviour for Spring proxies. For reference:

NOTE

Due to the proxy-based nature of Spring’s AOP framework, calls within the target object are, by definition, not intercepted. For JDK proxies, only public interface method calls on the proxy can be intercepted. With CGLIB, public and protected method calls on the proxy are intercepted (and even package-visible methods, if necessary). However, common interactions through proxies should always be designed through public signatures.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật