ProGuard will make Spring Bean name conflict

Startup error

2020-11-06 18:07:31.452  WARN 13120 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.maple.MapleApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'a' for bean class [com.maple.a.d.a.a] conflicts with existing, non-compatible bean definition of same name and class [com.maple.a.b.a]
2020-11-06 18:07:31.463 ERROR 13120 --- [           main] o.s.boot.SpringApplication               : Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.maple.MapleApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'a' for bean class [com.maple.a.d.a.a] conflicts with existing, non-compatible bean definition of same name and class [com.maple.a.b.a]

I tried to use Spring definition.getBeanClassName() naming rules, but it didn’t work. Is it too much trouble if you want to manually set the name of each bean on the annotation? Can ProGuard provide the function of not repeating the class name? I haven’t found it yet.

Hello XiaoyuZhann,

The problem is that if you don’t specify the name with the components, then the name is taken from the class name. And if two components are in two different packages, they can both end up being named, for example “a”. This creates a naming conflict.

The fastest and easiest solution here, is to keep all the Component classes:

keep 'public @org.springframework.stereotype.Component class **'

This will prevent naming conflicts.

Alternatively, it is possible to specify bean names in @Component and other stereotypes (e.g. @Repository, @Service and @Controller). For example,

@Component("myBeanName")

public class MyBean {

}

Please let us know if anything is not clear.

Kind Regards,

Jack

I have same issue and try to add keep '… ’ of your suggestion. It gives error. “Unknown option ‘keep’ in argument number 298”
I add the line in my pom.xml for build. What is correct format for it? Thanks.

Hi @Jinghong_Li,

The correct syntax is like so:
-keep class com.example.someclass {*;}

Whereby there is a required dash (“-”) at the start.

You can read more about constructing -keep rules and the required syntax here:

Additionally, your -keep rules should go in a file called ‘proguard-rules.pro’ which is your configuration file. They do not belong in your pom.xml file.

After adjusting your syntax and putting the -keep rules inside your ProGuard configuration file, you can clean your build cache and rebuild (gradle clean assembleRelease). The error will go away.

Please let me know if anything is not clear.
Kind Regards,
Jack

Thanks for the suggestion. After it, I still have issue with duplicated class name. What is your suggestion? Thanks.

'“Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name ‘j’ for bean class [xxx.j] conflicts with existing, non-compatible bean definition of same name and class [xxxx.req.j]”

for me that issue is resolved by adding
-keep class com.xx.xxxservice.** {*;}

1 Like