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.
HogeProcessor.java
public interface HogeProcessor extends Processor<Object, Object> {
Optional<Object> process(HogeEntity hogeEntity);
}
HogeProcessorImpl.java
public class HogeProcessorImpl implements HogeProcessor {
@Override
@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.
Java's @Transactional annotation is realized using Spring AOP, but this AOP library seems to be the cause of this time.
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.
aop.xml
<!--Enable AOP-->
<aop:aspectj-autoproxy/>
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.
- Spring creates a proxy object for the aspect-targeted class at runtime.
http://hamasyou.com/blog/2004/11/09/spring-framework-jue-shu-ki-aop/
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).
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.
http://kamatama41.hatenablog.com/entry/20140327/1395928048
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