Spring AOP Self-Invocation Problem and a Kotlinic Workaround

Spring AOP Problem In Spring, self-invocation refers to a method within a class calling another method of the same class. This can be problematic when using AOP (Aspect-Oriented Programming) annotations like @Transactional. Spring AOP is proxy-based. So if you call another method directly within the same class, it bypasses the proxy, and AOP annotations won’t be applied. For example, suppose you have method A annotated with @Transactional, and it’s called from method B in the same class. If method B calls A directly, Spring won’t manage the transaction properly because the call doesn’t go through the proxy. ...

May 27, 2024

Simplified Transaction

Example Code https://github.com/jurogrammer/dtrans Situation When you want to separate transactions for different operations, but creating a new service class for this feels cumbersome. Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Service @RequiredArgsConstructor public class SaveServiceVer1 { private final UserRepository userRepository; private final OrderRepository orderRepository; @Transactional public void save() { userRepository.save(new User("홍길동")); // If an error occurs while saving the order, you still want the user to be saved as is. saveOrder(); } public void saveOrder() { orderRepository.save(new Order()); throw new RuntimeException("Error occurred while saving order"); } } Idea Even if an error occurs in saveOrder and the order is rolled back, the User save should still be performed. ...

July 15, 2022

[Spring] Circular Reference Error Caused by Field Injection

References edwith DobiSocks reviewer https://www.mimul.com/blog/di-constructor-injection/ (Why DI is needed) https://madplay.github.io/post/why-constructor-injection-is-better-than-field-injection (Why constructor injection is recommended over field injection) https://d2.naver.com/helloworld/1230 (JVM Internal) Overview In an edwith course, I submitted a project using field injection for DI. However, the reviewer recommended constructor injection and kindly provided related materials. Although I understood most of the points, I didn’t quite get how constructor injection prevents circular reference errors, so I decided to summarize it here. ...

June 1, 2020