[JAVA] Difference between Spring AOP and library proxy target class


Implement transaction processing for the method. When I tried to realize it using @Transactional annotation, BeanCreationException came to occur when annotation was added.


Create an interface for the class that includes the target method of transaction processing, and add @Transactional annotation to the method of implementation class. Now there are no errors and you can compile successfully.


public interface HogeProcessor extends Processor<Object, Object> {
    Optional<Object> process(HogeEntity hogeEntity);


public class HogeProcessorImpl implements HogeProcessor {

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Optional<Object> process(HogeEntity hogeEntity) {


I did some research myself, but I didn't understand, so I asked him to tell me.

If the interface and implementation class are separated and healed, this is the cause. http://stackoverflow.com/questions/15688689/unsatisfieddependencyexception-in-spring-illegal-arguments-for-constructor-a

Due to the specifications of the library used for AOP, details can be found here. http://d.hatena.ne.jp/minokuba/20110321/1300705068

I didn't understand even after reading the page I was taught, so I looked it up. Below is a summary of the researched content.

Differences between Spring AOP and libraries

Java's @Transactional annotation is realized using Spring AOP, but this AOP library seems to be the cause of this time.

What is AOP

Aspect-oriented programming. Aspect is a behavior and represents a unit of common processing. A mechanism that allows you to go out for common systematic processing that is not essential for business.

This time, AOP is used to insert the process to start the transaction at the timing when the specified method is called.

It seems that AOP can be enabled by writing the following in the configuration file.


<!--Enable AOP-->

Spring AOP library types

In Spring AOP -JDK Dynamic Proxy ・ CGLib Two types can be used. Only the JDK Dynamic Proxy was used in the project that had the problem this time.


The class that is the aspect target of AOP must be JavaBean.

  1. Spring creates a proxy object for the aspect-targeted class at runtime.
  1. Then, inside the proxy object, perform the processing of the aspect target class.
  2. When the aspect target class is specified as the join point, the advice method of the intercept class is called in the proxy.


In other words, when the AOP library creates a bean of the aspect target class, it creates a ProxyObject wrapped in a proxy (proxy conversion).

Difference in Proxy target class by AOP library

It seems that it is not possible to Autowired an instance of a bean that is proxied with JDK dynamic proxy by directly specifying the implementation class.


When using JDK DynamicProxy, the class that implements the interface is the target of Proxy conversion. In other words, it seems that the implementation class cannot be DI directly. This time, I tried to use @Transactional's AOP while the implementation class was directly DI, so it was not the target of Proxyization of JDK DynamicProxy, and BeanCreationException occurred.

So, I created a new interface for the existing class, changed it to do DI for that interface, and solved it by making the existing class an implementation class of the interface. Also, it seems that the implementation class can be proxyed by using CGLib, which is another AOP library, so I have not tried it this time, but there seems to be a way to use this.

Recommended Posts

Difference between Spring AOP and library proxy target class
Difference between class and instance
Difference between instance method and class method
Difference between instance variable and class variable
Difference between vh and%
Difference between i ++ and ++ i
What is the difference between a class and a struct? ?? ??
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between bundle and bundle install
Relationship between package and class
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Ruby] Difference between get and post
Difference between render method and redirect_to
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
[Ruby] Maybe you don't really understand? [Difference between class and module]
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
Differences between Applet class and JApplet class
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Easy to understand the difference between Ruby instance method and class method.
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Spring with Kotorin --2 RestController and Data Class
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
[Java] Differences between instance variables and class variables
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between addPanel and presentModally of FloatingPanel
[Ruby] Difference between print, puts and p