图书馆内的弹簧豆

发布于 2025-01-31 16:42:32 字数 915 浏览 2 评论 0原文

我正在开发一些使用春季和春季靴子的公司内部用途的液体。

我的豆定义遇到了问题。我想创建一些不属于我库的对象的豆子,但是

@Configuration
public class LibClass {
@Bean 
public Gson gson() { return new Gson();}} 

,每当我这样做时,它也会影响使用我的库并为它们创建豆子的服务,这意味着他们将被迫使用我的我通过定义spring.main.main.main.main.main-bean-definition-overriding = true,gson或启用了豆类的覆盖。

例如,如果我的服务中的代码与上面的代码相同:

@Configuration
public class ServiceClass {
@Bean 
public Gson gson() { return new Gson();}} 

春季抛出

BeanDefinitionOverRideRideException

  1. 在库中创建了这种豆是一个好习惯吗?
  2. 我该如何告诉Spring仅在我的图书馆课内使用此豆,而不是外部

感谢

编辑: 正如我在评论中提到的那样,我不想强​​迫使用@qualifier,就好像他们还没有使用它,如果没有它,我们就不需要,他们会得到这个例外,并且不会知道为什么要了解一些使用随机豆的库并不小。但是,如果有人忘记创建豆子,他们可以错误地使用lib bean,甚至不知道。这可能会导致一些令人讨厌和意外的行为。 另外,注释@ConditionAlonMissingBean在这里对我无济于事,因为如果需要,我将无法以后配置特定的配置,并且我的内部库将受到用户未知配置的影响,这似乎不是一个好的实践

I'm developing some libs for internal use in my company, which using spring and spring Boot.

I'm encountered a problem with my bean definition. I want to create some beans of objects that don't belong to my library, for example:

@Configuration
public class LibClass {
@Bean 
public Gson gson() { return new Gson();}} 

However, whenever I do this it affect the service that using my library and creating beans for them too, which means they will be forced to use my Gson or enabling overriding of beans, by defining spring.main.allow-bean-definition-overriding=true which feels wrong.

For example, if I have the same code as above in my service:

@Configuration
public class ServiceClass {
@Bean 
public Gson gson() { return new Gson();}} 

Spring throws

BeanDefinitionOverrideException

  1. Is creating those kind of beans inside a library is a good practice?
  2. How can I tell spring to use this bean just inside my libraries classes and not outside

Thanks

Edit:
As I mentioned in the comments, I don't want to force using of @Qualifier, as if they don't using it already, which we don't if there is no need to, they will get this exception and won't know why as it is not trivial to understand that some libraries using random beans. Nonetheless, if someone forget to create a bean, they can use the lib bean, by mistake, without even knowing. This could cause some nasty and unexpected behavior.
Also, the annotation @ConditionalOnMissingBean won't help me here, as I won't be able to configure specific configuration later if needed and my internal library will be affected by an unknown configuration from the user, which isn't seem like a good practice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2025-02-07 16:42:32

更改bean名称并添加预选程序应有效:

@Bean 
@Qualifier("gsonForInternal")
public Gson gsonForInternal() { return new Gson();}

自动启动您应该在哪里:

@Autowired 
@Qualifier("gsonForInternal")
private Gson gsonForInternal;

编辑:
如果您希望您可以使此bean有条件:

@ConditionalOnMissingBean
@Bean 
public Gson gson() { return new Gson();}

但是,如果库用户将定义自己的bean:

@Bean 
public Gson gson() { return new Gson();}

这意味着您的库将使用其定义的gson

,因此我认为您应该使用限定符 - 如果我理解您的要求正确

changing the bean name and adding a qualifier should work:

@Bean 
@Qualifier("gsonForInternal")
public Gson gsonForInternal() { return new Gson();}

where autowiring you should :

@Autowired 
@Qualifier("gsonForInternal")
private Gson gsonForInternal;

Edit:
in case you want you can make this bean conditional:

@ConditionalOnMissingBean
@Bean 
public Gson gson() { return new Gson();}

however if the library user will define its own bean:

@Bean 
public Gson gson() { return new Gson();}

this means your library will use his defined Gson

so i think you should be using qualifier - if i understand your requirement correctly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文