SpringBoot配置BeanNameGenerator以使用限定符名称。

huangapple go评论70阅读模式
英文:

SpringBoot configuring BeanNameGenerator to use Qualifier names

问题

我正在使用ProGuard来混淆我的代码,并且为了解决重复的bean定义名称问题,我正在使用自定义的bean名称生成器来注册带有完全限定名称的bean。

public static class CustomGenerator implements BeanNameGenerator {

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        return definition.getBeanClassName();
    }
}

然后,我使用自定义名称生成器创建应用程序上下文。

ApplicationContext ctx = new SpringApplicationBuilder(DataQualityApplication.class)
            .beanNameGenerator(new CustomGenerator())
            .run(args);

然而,问题在于现在这些bean是通过它们对应的类类型进行注册的,而不是通过@Qualifier@Component中提供的限定符名称进行注册。
在我的项目中,我该如何实现这一点?

英文:

I am using proguard to obfuscate my code and to tackle duplicate bean definition names, I am using custom bean name generator to register beans with fully qualified names.

public static class CustomGenerator implements BeanNameGenerator {

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
        return definition.getBeanClassName();

    }
}

Then I am creating application context using the custom name generator

ApplicationContext ctx = new SpringApplicationBuilder(DataQualityApplication.class)
			.beanNameGenerator(new CustomGenerator())
			.run(args);

The issue however is that the beans are now being registered using their corresponding class types and not by the qualifier names provided in the @Qualifier or @Component.
How can I achieve this in my project?

答案1

得分: 2

通过扩展AnnotationBeanNameGenerator来解决此问题,而不是实现BeanNameGenerator接口。

public static class CustomGenerator extends AnnotationBeanNameGenerator {

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {

        UnaryOperator<String> fun = pkgName -> {
            int lastIndex = pkgName.lastIndexOf('.');
            if (lastIndex != -1) {  
                pkgName = pkgName.substring(0, lastIndex);
            }
            return pkgName;
        };
        
        String className = super.generateBeanName(definition, registry);
        String packageName = definition.getBeanClassName();
        return (fun.apply(packageName) + "." + className);
    }
}

这将根据是否带注解的情况返回带有限定名/组件名称的Bean定义,否则返回类名。解决了Proguard JAR中的重复Bean定义错误。

英文:

Solve it by extending AnnotationBeanNameGenerator rather than implementing the BeanNameGenerator interface

public static class CustomGenerator extends AnnotationBeanNameGenerator {

    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {

    	UnaryOperator&lt;String&gt; fun=pkgName-&gt;{
    		 int lastIndex = pkgName.lastIndexOf (&#39;.&#39;);
 		    if (lastIndex!=-1){  
 		    	pkgName=pkgName.substring (0, lastIndex);
 		    }
 		    return pkgName;
    	};
        String className = super.generateBeanName(definition, registry);
        String packagename = definition.getBeanClassName();
        return (fun.apply(packagename) + &quot;.&quot; + className);
    }
}

This returns fully qualified names with qualifier/component name if annotated otherwise class name.
Solves the duplicate bean definition error in Proguard jar.

答案2

得分: 0

如果您将类名用作bean名称,这意味着您每种类型只能有一个bean。在这种情况下,只能通过类型进行注入,只需删除@Qualifer并且不要为您的bean命名,这应该可以正常工作。如果您有多个相同类的bean,则无法使用您的生成器。

英文:

If you use the class name as bean name, that means you can have only one bean per type. In this situation, the injection can be done by type only, just remove the @Qualifer and don't name your beans, this should work fine. If you have several beans for the same class, you can't use your generator.

huangapple
  • 本文由 发表于 2020年9月3日 19:29:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63722709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定