简体   繁体   中英

How to enable Autowired for FeignClient in JHipster?

I have a microservice application and i want to enable it to call API.

FeignClientConfiguration.java

@Configuration
@Profile("!test")
@EnableFeignClients(basePackages = "blabla")
public class FeignClientConfiguration {
}

And then spring boot App :

@ComponentScan
@EnableAutoConfiguration(exclude ={MetricFilterAutoConfiguration.class,MetricRepositoryAutoConfiguration.class})
@EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
@EnableDiscoveryClient
public class MyApp { }

The feign client

@FeignClient()
public interface ExtClient { ... }

I then tried to autowired the client

Mytransaction.java

public class MyTransaction {
@Autowired
ExtClient txnClient;
 ....
}

But it fails with a NPE. How to autowired FEIGN in JHipster?

MyTransaction must be a Spring bean. Simplest way is to annotate it with @Service , this way it'll get instantiated by Spring and txnClient will get injected. By the way, you should consider using constructor injection rather than field injection, many examples in JHipster generated code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM