在库中使用Proguard有一个应该自动装配的@Service bean

时间:2010-12-10 17:39:37

标签: spring obfuscation proguard autowired

我有一个Common.License库,我和Proguard混淆了:

<plugin>
    <groupId>com.pyx4me</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <obfuscate>true</obfuscate>
        <options>
            <option>-dontoptimize</option>
            <option>-renamesourcefileattribute SourceFile</option>
            <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod</option>
            <option>-keep public class * { public protected *;}</option>
            <option>-keepclassmembernames class * {java.lang.Class class$(java.lang.String); java.lang.Class class$(java.lang.String, boolean);}</option>
            <option>-keepclassmembernames class * {com.common.license.LicenseSessionStore licenseSessionStore; com.common.license.LicenseStore licenseStore;}</option>
            <option>-keepclassmembers enum * {public static **[] values(); public static ** valueOf(java.lang.String);}</option>
            <option>-keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve();}</option>
        </options>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jsse.jar</lib>
        </libs>
        <addMavenDescriptor>false</addMavenDescriptor>
    </configuration>
</plugin>

这个库有一个用@Service注释的Spring bean:

@Service
public class LicenseServiceImpl implements LicenseService {

    @Autowired(required = false)
    LicenseSessionStore licenseSessionStore;

    @Autowired(required = false)
    LicenseStore licenseStore;

...

}

我在Web服务Company.License中使用此库,我希望LicenseService能够自动装配:

@Component
public class BackgroundTasks {

    @Autowired
    ScheduledExecutorService scheduledExecutorService;

    @Autowired
    LicenseService licenseService;

...

}

因此,Company.License依赖于Common.License。如果我对Common.License进行模糊处理,则licenseService将不会在BackgroundTasks中自动装配。我可以解决这个问题的唯一方法是将licenseService显式定义为bean:

@Bean(name = "licenseService", autowire = Autowire.BY_NAME)
public LicenseService getLicenseService() {
    if (licenseService == null) {
        licenseService = new LicenseServiceImpl();
    }
    return licenseService;
}

我不需要显式地将此声明为像这样的bean,因为我已经使用@Service注释了该类,这应该足以在licenseService Spring中自动装配BackgroundTasks - 可行的工作。但事实并非如此!

Proguard专门做了什么使这不起作用?在Proguard的配置中我能做些什么来使它更适合Spring吗?

捐赠

3 个答案:

答案 0 :(得分:6)

非常感谢Eric Lafortune帮助我指出了正确的方向:

http://sourceforge.net/projects/proguard/forums/forum/182456/topic/2547498

这是添加工作pom文件,指定插件和所需选项:

<plugin>
    <groupId>com.pyx4me</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <obfuscate>true</obfuscate>
        <options>
            <option>-dontoptimize</option>
            <option>-keepdirectories</option>
            <option>-renamesourcefileattribute SourceFile</option>
            <option>-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod</option>
            <option>-keep public class * { public protected *;}</option>
            <option>-keepclassmembernames class * {java.lang.Class class$(java.lang.String); java.lang.Class class$(java.lang.String, boolean);}</option>
            <option>-keepclassmembernames class * {com.common.license.LicenseService licenseService; com.common.license.LicenseSessionStore licenseSessionStore;}</option>
            <option>-keepclassmembers enum * {public static **[] values(); public static ** valueOf(java.lang.String);}</option>
            <option>-keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve();}</option>
            <option>-keep @org.springframework.beans.factory.annotation.Service class *</option>
            <option>-keep @org.springframework.stereotype.Controller class *</option>
            <option>-keepclassmembers class * { @org.springframework.beans.factory.annotation.Autowired *; }</option>
        </options>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jsse.jar</lib>
        </libs>
        <addMavenDescriptor>false</addMavenDescriptor>
    </configuration>
</plugin>

请注意!您需要使用ProGuard 4.4,最新版本的ProGuard-Maven-Plugin(2.0.4)使用4.3,因此您需要编辑:

{M2_HOME}\repository\com\pyx4me\proguard-maven-plugin\2.0.4\proguard-maven-plugin-2.0.4.pom 

拥有4.4依赖(如下所示):

<dependency>
  <groupId>net.sf.proguard</groupId>
  <artifactId>proguard</artifactId>
  <version>4.4</version>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>

答案 1 :(得分:1)

这是另一种切换到proguard 4.4版本的方法(可能会更好一点):

<configuration>
  <proguardVersion>4.4</proguardVersion>
  <obfuscate>true</obfuscate>
  <options>
  ... 
  </options>    
  <libs>
    <lib>${java.home}/lib/rt.jar</lib>
    <lib>${java.home}/lib/jsse.jar</lib>
  </libs>
  <addMavenDescriptor>false</addMavenDescriptor>
</configuration>
<dependencies>
  <dependency>
    <groupId>net.sf.proguard</groupId>
    <artifactId>proguard</artifactId>
    <version>4.4</version>
    <scope>runtime</scope>
    <optional>true</optional>
  </dependency>
</dependencies>

答案 2 :(得分:0)

我遇到了同样的问题,-keepdirectories指令帮助我解决了这个问题。