Spring Cloud Data Flow @EnableBinding 注释中断构造函数注入

Spring Cloud Data Flow @EnableBinding annotation breaks constructor injection

我正在尝试使用构造函数注入创建一个 bean,并使用 Spring Cloud @EnableBinding 注释对其进行注释。

我正在使用 Spring 引导 1.3.3 和 Spring 云 Brixton.M5

我在应用程序启动期间收到以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'locationReceiver' defined in file [<path>/projects/location/location-microservice/target/classes/<xxx>/location/microservice/stream/LocationReceiver.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [xxx.location.microservice.stream.LocationReceiver$$EnhancerBySpringCGLIB$$b2012a14]: No default constructor found; nested exception is java.lang.NoSuchMethodException: xxx.location.microservice.stream.LocationReceiver$$EnhancerBySpringCGLIB$$b2012a14.<init>()
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at xxx.location.microservice.LocationMicroservice.main(LocationMicroservice.java:13) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_72]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.3.3.RELEASE.jar:1.3.3.RELEASE]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [xxx.location.microservice.stream.LocationReceiver$$EnhancerBySpringCGLIB$$b2012a14]: No default constructor found; nested exception is java.lang.NoSuchMethodException: xxx.location.microservice.stream.LocationReceiver$$EnhancerBySpringCGLIB$$b2012a14.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 22 common frames omitted
Caused by: java.lang.NoSuchMethodException: xxx.location.microservice.stream.LocationReceiver$$EnhancerBySpringCGLIB$$b2012a14.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_72]
    at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_72]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 23 common frames omitted

如果

,则此异常消失
  1. @EnableBinding 已删除
  2. 引入了无参数构造函数(显然,尽管 @Autowired 在包含 args 的构造函数上注释,但它仍然被使用)
  3. 改为使用字段注入

下面的代码片段:

@EnableBinding(Sink.class)
public class LocationReceiver {
//    @Autowired
    private final LocationModelRepository repository;

//    LocationReceiver() {
//        repository = null;
//    }

    @Autowired
    public LocationReceiver(LocationModelRepository repository) {
        this.repository = repository;
    }
}

我猜它与 CGLIB 代理有某种关系,但它应该支持从 Spring 4.

开始的构造函数注入

有人知道哪里出了问题吗?是我的错还是 Spring 云数据流中的错误?

刚刚在 github 上得到 Dave Syer 的回答:

That's because @EnableBinding is a @Configuration (and until Spring 4.3 constructor injection doesn't work with @Configuration).