以Spring 3.x为例,代码如下:
// IProcessor.java
package example;
public interface IProcessor {
void execute();
}
// JavatangProcessor.java
package example.impl;
import example.IProcessor;
@Component
@Transactional
public class JavatangProcessor implements IProcessor {
@Override
public void execute() {
}
}
xml配置文件内容如下:
...
如果在其他地方使用@Autowired注入JavatangProcessor的话,在启动工程的时候会出现类似下面的错误信息:
java.lang.IllegalArgumentException: Can not xxx.javatangProcessor field JavatangProcessor to $Proxy41
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
at java.lang.reflect.Field.set(Field.java:657)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
这是因为tx:annotation-driven默认情况下Spring使用的是基于接口的代理模式,在这种情况下子类无法直接进行初始化并进行引用,解决方法是在tx:annotation-driven中将proxy-target-class属性设置为true,即创建子类来代理业务类,xml配置文件修改成:
如果是Spring 2.x的话需要设置成:
参考资料:
Spring3事务管理——使用@Transactional 注解
Spring @Transactional doesn't work with other annotations?