Spring-cloud-netflix: Feign Targeter is `@Autowired`, causing NPE because of lifecycle issues.

Created on 29 Sep 2016  路  3Comments  路  Source: spring-cloud/spring-cloud-netflix

When i add dev-tools dependency FeignClientFactoryBean throws NullPointerException.
I don't know very well how dev-tools works but i think Spring doesn't see anymore @Autowired annotation for field targeter.

error.txt

In my gradle build there are:

    dependencyManagement {
        imports {
            mavenBom 'io.spring.platform:platform-bom:Athens-RELEASE'
            mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.RELEASE'
            mavenBom "org.springframework.boot:spring-boot-parent:1.4.1.RELEASE"

        }
    }

    dependencies {
        //Cloud
        compile 'org.springframework.cloud:spring-cloud-starter-config'
        compile 'org.springframework.cloud:spring-cloud-starter-eureka'
        compile 'org.springframework.cloud:spring-cloud-starter-ribbon'
        compile 'org.springframework.cloud:spring-cloud-starter-feign'
        compile 'org.springframework.cloud:spring-cloud-starter-hystrix'


        compile("org.springframework.boot:spring-boot-starter-security")

        compile group: 'org.springframework.boot', name: 'spring-boot-devtools'

        testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
    }

In the Brixton release the code was:

    private static final Targeter targeter;

    static {
        Targeter targeterToUse;
        if (ClassUtils.isPresent("feign.hystrix.HystrixFeign",
                FeignClientFactoryBean.class.getClassLoader())) {
            targeterToUse = new HystrixTargeter();
        }
        else {
            targeterToUse = new DefaultTargeter();
        }
        targeter = targeterToUse;
    }

in Camden there is @Autowired.

In either Brixton or Camden if you put in class FeignClientFactoryBean the following code:

    @Autowired
    private PackageDto packageDto;

where PackageDto isn't a bean the application doesn't throw any Exception. In this case without dev-tools spring throws org.springframework.beans.factory.UnsatisfiedDependencyException: Unsatisfied dependency expressed through field 'packageDto'.

bug

Most helpful comment

A quick workaround for this issue appears for be importing the FeignAutoConfiguration.class, which I assume configures it earlier:

@SpringBootApplication
@ImportAutoConfiguration(FeignAutoConfiguration.class)
@EnableFeignClients
public class Application {

All 3 comments

3991c8c5f78b6c2d781c5c4b0e86c961378e6fc9 reintroduced @Autowired to FeignClientFactoryBean 馃槩 .

A quick workaround for this issue appears for be importing the FeignAutoConfiguration.class, which I assume configures it earlier:

@SpringBootApplication
@ImportAutoConfiguration(FeignAutoConfiguration.class)
@EnableFeignClients
public class Application {

This is a sample project recreating the issue: https://github.com/Turbots/spring-cloud-feign-autowiring-problem
It seems that the error occurs from the moment we add the spring-boot-devtools dependency, which changes the autoconfiguration order I guess

Was this page helpful?
0 / 5 - 0 ratings