retrofit-spring-boot-starter icon indicating copy to clipboard operation
retrofit-spring-boot-starter copied to clipboard

Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean of type 'com.xx.HttpApi' that could not be found.

Open JakeWoki opened this issue 2 years ago • 1 comments

[ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean of type 'com.xx.HttpApi' that could not be found.

Action:

Consider defining a bean of type 'com.xx.HttpApi' in your configuration.

Process finished with exit code 1

@RetrofitClient(
        baseUrl = "https://api.weixin.qq.com/",
        fallbackFactory = HttpDegradeFallbackFactory.class
)
@Logging(logStrategy = LogStrategy.BODY)
/*默认策略情况下,每5s平均响应时间不得超过500ms,否则启用熔断降级*/
@SentinelDegrade(count = 500)
public interface HttpApi {

    @GET("sns/jscode2session")
    Response<WeChatMiniProgramCode2Session> jsCodeToSession(@Query("js_code") String jsCode, @Query("appid") String appId, @Query("secret") String secret, @Query("grant_type") String grantType);
}

一台电脑这样正常,另一台这样却报上面错误

@Component
public class HttpApiFallback implements HttpApi {
    @Override
    public Response<WeChatMiniProgramCode2Session> jsCodeToSession(String jsCode, String appId, String secret, String grantType) {
        return null;
    }
}

@RetrofitClient(
        baseUrl = "https://api.weixin.qq.com/",
        fallback = HttpApiFallback.class,
        fallbackFactory = HttpDegradeFallbackFactory.class
)
@Logging(logStrategy = LogStrategy.BODY)
/*默认策略情况下,每5s平均响应时间不得超过500ms,否则启用熔断降级*/
@SentinelDegrade(count = 500)
public interface HttpApi {

    @GET("sns/jscode2session")
    Response<WeChatMiniProgramCode2Session> jsCodeToSession(@Query("js_code") String jsCode, @Query("appid") String appId, @Query("secret") String secret, @Query("grant_type") String grantType);
}

那台报错的改成这样正常,改成这样后前一台又出错,不知道是什么问题

JakeWoki avatar Sep 13 '22 01:09 JakeWoki

麻烦提供一下完整的堆栈,应该有HttpApi没找到的具体原因。

chentianming11 avatar Sep 22 '22 02:09 chentianming11

堆栈是指哪些? RetrofitClient加上fallback就用不了

JakeWoki avatar Sep 26 '22 03:09 JakeWoki

完整的错误日志

RetrofitClient加上fallback就用不了,这个我没复现出来。因此得依据你具体的情况才可以分析。

chentianming11 avatar Sep 26 '22 08:09 chentianming11

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by retrofit2.Platform (file:/C:/Users/888/.m2/repository/com/squareup/retrofit2/retrofit/2.9.0/retrofit-2.9.0.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int) WARNING: Please consider reporting this to the maintainers of retrofit2.Platform WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release 2022-09-30 09:40:19.046 WARN 30148 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.test.app.api.wx.HttpApi' available: expected single matching bean but found 2: httpApiFallback,httpApi 2022-09-30 09:40:23.164 INFO 30148 --- [ restartedMain] com.test.app.api.websocket.WSServer : Close cim server success!!! 2022-09-30 09:40:23.164 INFO 30148 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2022-09-30 09:40:23.164 INFO 30148 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closing ... 2022-09-30 09:40:23.179 INFO 30148 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed 2022-09-30 09:40:23.195 INFO 30148 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2022-09-30 09:40:23.211 INFO 30148 --- [ restartedMain] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-09-30 09:40:23.243 ERROR 30148 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a single bean, but 2 were found: - httpApiFallback: defined in file [C:\Users\888\git\Test\app-api\target\classes\com\test\app\api\wx\fallback\HttpApiFallback.class] - httpApi: defined in file [C:\Users\888\git\Test\app-api\target\classes\com\test\app\api\wx\HttpApi.class]

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Process finished with exit code 1

JakeWoki avatar Sep 30 '22 01:09 JakeWoki

可以在注入HttpApi 的地方加上@qualifier("httpApi") 试试。这个报错是因为有两个HttpApi bean实例,注入的时候不知道该选择哪一个。

chentianming11 avatar Oct 07 '22 07:10 chentianming11

也可以使用2.3.8版本试试,这个版本指定了HttpApi 为Primary

chentianming11 avatar Oct 07 '22 09:10 chentianming11

没有qualifier这个注解,2.3.8还是一样的错误

JakeWoki avatar Oct 08 '22 02:10 JakeWoki

没有qualifier这个注解,2.3.8还是一样的错误

  1. @Qualifier,这个注解从Spring2.5就有了,应该是有的。
  2. 请问下,你目前使用的Spring-boot是什么版本?

chentianming11 avatar Oct 09 '22 03:10 chentianming11

Spring是2.7.3,加上@Qualifier可以了

JakeWoki avatar Oct 09 '22 06:10 JakeWoki