Spring AOP Bean 注入错误?
Spring AOP Bean Injection Bug?
我在这里签入了测试项目:https://github.com/loesak/spring-aop-injection-bug
鉴于以下 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.loesoft</groupId>
<artifactId>spring-aop-injection-bug</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.framework.bom.version>4.2.0.RELEASE</spring.framework.bom.version>
<spring.retry.version>1.1.2.RELEASE</spring.retry.version>
<aspectj.aspectjweaver>1.8.7</aspectj.aspectjweaver>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.framework.bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>${spring.retry.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.aspectjweaver}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
</project>
和Spring配置:
package com.loesoft.spring.aop.injection.bug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Retryable;
@Configuration
@EnableRetry
public class Proof {
public static void main(String... args) {
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Proof.class);
context.registerShutdownHook();
}
@Bean
public BeanThing beanThing() {
return new BeanThing();
}
@Bean
@Autowired
public BeanNeedy beanNeedy(BeanThing beanThing) {
return new BeanNeedy(beanThing);
}
public static interface BeanInterface {
public void doSomething();
}
public static class BeanThing implements BeanInterface {
@Retryable
public void doSomething() {
System.out.println("BeanNeedingDependencies doing something");
}
}
public static class BeanNeedy {
private final BeanThing beanThing;
public BeanNeedy(BeanThing beanThing) {
this.beanThing = beanThing;
}
}
}
Spring抛出以下错误:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beanNeedy' defined in com.loesoft.spring.aop.injection.bug.Proof: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.loesoft.spring.aop.injection.bug.Proof$BeanThing]: : No qualifying bean of type [com.loesoft.spring.aop.injection.bug.Proof$BeanThing] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.loesoft.spring.aop.injection.bug.Proof$BeanThing] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.loesoft.spring.aop.injection.bug.Proof.main(Proof.java:15)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.loesoft.spring.aop.injection.bug.Proof$BeanThing] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 14 more
我已经追踪到 bean "beanThing" 最终成为 JdkDynamicAopProxy 的事实,因为它有接口,并且在创建 bean [=32= 时试图找到一个匹配的 bean 作为自动装配的候选者时],代码以 ResolvableType#isInstance 结束,它不检查 JDK 代理类型的基础类型,也没有找到类型为 "BeanThing".[=15= 的已创建 bean "beanThing" ]
我可以通过两种方式解决这个问题。第一种方法是将 @EnableRetry 注释字段 "proxyTargetClass" 设置为 "true" 或删除 class BeanThing 上的接口。
这是一个错误还是我遗漏了一些我不知道的关于 Spring AOP 的信息?总的来说,这对我来说似乎不对。我觉得 Spring 应该能够确定 Proxy bean 的底层类型,除非有一些技术原因导致它不能。如果出于技术原因无法确定 JDK 代理的基础类型,那么也许 Spring 应该进行一些额外的检查,以帮助开发人员弄清楚发生了什么。
请记住,这与 Spring 重试无关(因此未标记),因为我已经能够使用其他需要使用 AOP 代理包装底层 bean 的注释重现此问题.
来自 JDK 代理 javadocs:
A dynamic proxy class is a class that implements a list of interfaces
specified at runtime when the class is created
这意味着代理只能实现bean的接口,而不是它的实例。您不能用代理替换真正的 class,因为您没有 class 的方法和变量,只有代理的方法。
I can get around this in two ways. The first by setting the
@EnableRetry annotation field "proxyTargetClass" to "true" or by
removing the interface on the class BeanThing.
如果您将 proxyTargetClass
设置为 true
,而不是 JDK 代理,Spring 将创建一个 CGLIB class(当class 没有接口,因为您无法创建没有接口的 JDK 代理)。
使用 CGLIB,动态创建 bean 的 subclass 以拦截方法调用。
CGLIB 代理是 BeanThing
,因为它继承自它。 JDK 代理不是 BeanThing
,因为它只是实现了它的接口。
有第三种方法可以解决您的问题:不是注入 BeanThing
class,而是注入它实现的接口 (BeanInterface
).
@Bean
public BeanInterface beanThing() {
return new BeanThing();
}
// .....
public static class BeanNeedy {
private final BeanInterface beanThing;
public BeanNeedy(BeanInterface beanThing) {
this.beanThing = beanThing;
}
}
配置
为了启用 CGLIB,您可以在您的配置 class 中使用 @EnableAspectJAutoProxy
注释并将 proxyTargetClass
设置为 true
。
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class OneOfYourGlobalConfigConfigs {
}
我在这里签入了测试项目:https://github.com/loesak/spring-aop-injection-bug
鉴于以下 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.loesoft</groupId>
<artifactId>spring-aop-injection-bug</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.framework.bom.version>4.2.0.RELEASE</spring.framework.bom.version>
<spring.retry.version>1.1.2.RELEASE</spring.retry.version>
<aspectj.aspectjweaver>1.8.7</aspectj.aspectjweaver>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.framework.bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>${spring.retry.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.aspectjweaver}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
</project>
和Spring配置:
package com.loesoft.spring.aop.injection.bug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Retryable;
@Configuration
@EnableRetry
public class Proof {
public static void main(String... args) {
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Proof.class);
context.registerShutdownHook();
}
@Bean
public BeanThing beanThing() {
return new BeanThing();
}
@Bean
@Autowired
public BeanNeedy beanNeedy(BeanThing beanThing) {
return new BeanNeedy(beanThing);
}
public static interface BeanInterface {
public void doSomething();
}
public static class BeanThing implements BeanInterface {
@Retryable
public void doSomething() {
System.out.println("BeanNeedingDependencies doing something");
}
}
public static class BeanNeedy {
private final BeanThing beanThing;
public BeanNeedy(BeanThing beanThing) {
this.beanThing = beanThing;
}
}
}
Spring抛出以下错误:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beanNeedy' defined in com.loesoft.spring.aop.injection.bug.Proof: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.loesoft.spring.aop.injection.bug.Proof$BeanThing]: : No qualifying bean of type [com.loesoft.spring.aop.injection.bug.Proof$BeanThing] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.loesoft.spring.aop.injection.bug.Proof$BeanThing] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.loesoft.spring.aop.injection.bug.Proof.main(Proof.java:15)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.loesoft.spring.aop.injection.bug.Proof$BeanThing] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 14 more
我已经追踪到 bean "beanThing" 最终成为 JdkDynamicAopProxy 的事实,因为它有接口,并且在创建 bean [=32= 时试图找到一个匹配的 bean 作为自动装配的候选者时],代码以 ResolvableType#isInstance 结束,它不检查 JDK 代理类型的基础类型,也没有找到类型为 "BeanThing".[=15= 的已创建 bean "beanThing" ]
我可以通过两种方式解决这个问题。第一种方法是将 @EnableRetry 注释字段 "proxyTargetClass" 设置为 "true" 或删除 class BeanThing 上的接口。
这是一个错误还是我遗漏了一些我不知道的关于 Spring AOP 的信息?总的来说,这对我来说似乎不对。我觉得 Spring 应该能够确定 Proxy bean 的底层类型,除非有一些技术原因导致它不能。如果出于技术原因无法确定 JDK 代理的基础类型,那么也许 Spring 应该进行一些额外的检查,以帮助开发人员弄清楚发生了什么。
请记住,这与 Spring 重试无关(因此未标记),因为我已经能够使用其他需要使用 AOP 代理包装底层 bean 的注释重现此问题.
来自 JDK 代理 javadocs:
A dynamic proxy class is a class that implements a list of interfaces specified at runtime when the class is created
这意味着代理只能实现bean的接口,而不是它的实例。您不能用代理替换真正的 class,因为您没有 class 的方法和变量,只有代理的方法。
I can get around this in two ways. The first by setting the @EnableRetry annotation field "proxyTargetClass" to "true" or by removing the interface on the class BeanThing.
如果您将 proxyTargetClass
设置为 true
,而不是 JDK 代理,Spring 将创建一个 CGLIB class(当class 没有接口,因为您无法创建没有接口的 JDK 代理)。
使用 CGLIB,动态创建 bean 的 subclass 以拦截方法调用。
CGLIB 代理是 BeanThing
,因为它继承自它。 JDK 代理不是 BeanThing
,因为它只是实现了它的接口。
有第三种方法可以解决您的问题:不是注入 BeanThing
class,而是注入它实现的接口 (BeanInterface
).
@Bean
public BeanInterface beanThing() {
return new BeanThing();
}
// .....
public static class BeanNeedy {
private final BeanInterface beanThing;
public BeanNeedy(BeanInterface beanThing) {
this.beanThing = beanThing;
}
}
配置
为了启用 CGLIB,您可以在您的配置 class 中使用 @EnableAspectJAutoProxy
注释并将 proxyTargetClass
设置为 true
。
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class OneOfYourGlobalConfigConfigs {
}