本文主要是介绍Proguard混淆代码导致Spring自动装配失败,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近尝试用Proguard来混淆代码,以增加发布出去的代码安全性。今天运行混淆后的jar包发生如下异常
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 26 more
程序很简单,接口
- public interface Service {
- int getAge();
- }
实现类使用@Component标签实例化到spring容器中
- @Component
- public class ServiceImpl implements Service{
- public int getAge() {
- return new Random().nextInt(100);
- }
- }
在Controller中使用@Autowired标签自动装配
- @Autowired
- public Service service;
原程序可以正常运行,但在混淆了代码之后,发生以上所述的异常,原因是spring找不到能够匹配的Service实例。
忙活了一整天各种测试各种查资料,终于找到解决方案,很简单
1.在proguard配置中加上“-keepdirectories”
2.保证proguard版本不低于4.4
另外附上解决前和解决后的jar包解压时候的控制台输出
1.这是没加“-keepdirectories”的输出
inflated: abc/service/Service.class
inflated: test/rest/controller/Controller.class
inflated: test/rest/controller/ServiceImpl.class
inflated: META-INF/maven/test/rest/pom.xml
inflated: META-INF/maven/test/rest/pom.properties
2.加上“-keepdirectories”之后解压
inflated: META-INF/MANIFEST.MF
created: abc/
created: abc/service/
created: test/
created: test/rest/
created: test/rest/controller/
inflated: abc/service/Service.class
inflated: test/rest/controller/Controller.class
inflated: test/rest/controller/ServiceImpl.class
created: META-INF/maven/
created: META-INF/maven/test/
created: META-INF/maven/test/rest/
inflated: META-INF/maven/test/rest/pom.xml
inflated: META-INF/maven/test/rest/pom.properties
转自:http://kane-xie.iteye.com/blog/2240340
这篇关于Proguard混淆代码导致Spring自动装配失败的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!