有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Proguard与spring boot集成测试

我正在使用ProGuard对spring boot应用程序进行模糊处理。它工作正常,结果jar已经构建好,运行良好,没有任何问题

为了实现这一点,我做了一些调整:

  • 定制豆豆发电机

        public static void main(String[] args) {
            new SpringApplicationBuilder(MainApp.class)
              .beanNameGenerator((beanDefinition, beanDefinitionRegistry) -> beanDefinition.getBeanClassName())
              .run(args);
         }
    
  • 保留自动连线、限定符、Bean、值注释成员
  • 主要方法的保存
  • keepattributes开关带有异常、内部类、签名、不推荐、源文件、LineNumberTable、注释、封闭方法
  • dontshrink、dontoptimize、useuniqueclassmembernames、AdapteClassString、dontusemixedcaseclassnames选项

然而,集成测试(@SpringBootTest)出现了错误(在没有proguard的情况下构建时运行良好):

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test.

在包含主入口类(@SpringBootTest(classes=MainApp.class))之后:

2019-09-11 19:00:13,178 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@6b04acb2] to prepare te
st instance [com.xxx.xxx.it.ExternalIT@258274cb]
java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.7.RELEASE.jar:5.1.7.RELEASE]
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xxx.xxx.a.a.b' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

等等

除了创建新的配置文件来执行测试而不混淆之外,还有什么方法可以让它工作吗


共 (1) 个答案

  1. # 1 楼答案

    preservation of Autowired, Qualifier, Bean, Value annotation members,

    你在这里做得对,你需要保留所有属性

    -keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod
    

    然而,这并不是问题的原因,Spring会按类型进行自动连接,根据我在混淆后的经验,这样的问题是因为找不到类型的bean

    你需要调整你的代码,使用你的注释,比如

    @Component("yourComponentName")

    而不是

    @Component

    与bean和服务以及其他注释相同

    无论你在哪里使用@Qualifier("nameofbeanorService")

    由于保留了注释属性,应用程序将运行顺畅

    谢谢