如何进行带有多个参数的Spring构造函数注入
假设我们有一个类A和B。在B的构造函数中,不仅需要A,还需要一些其他String/boolean值。例如
@Componenet(value = "B")
@DependsOn(value = "A")
public class B{
...
}
public B(A a_instance, String name1, String name2, boolean b1){
...
}
我知道使用注释。但不确切知道应该如何处理这些字符串/布尔值?
Suppose we have a Class A, and B. inside B's constructor, not only A is needed, but also some other String / boolean values. e.g
@Componenet(value = "B")
@DependsOn(value = "A")
public class B{
...
}
public B(A a_instance, String name1, String name2, boolean b1){
...
}
I know using annotation. but not knowing exactly, what should be done with those String/boolean values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题并不完全清楚。您的 B 构造函数似乎位于类 B 之外。除此之外,假设没有其他构造函数,您所拥有的将不起作用,因为 Spring 将查找默认构造函数。我认为您要问的是,如果您想使用 @Autowired 构造函数来获取其中的字符串和布尔值,该怎么办。如果是这样,您需要这样的东西:
在这种情况下,如果您有多个 A 类型的 bean,
@Value
的行为有点像@Qualifier
。Your question isn't entirely clear. Your B constructor appears to be outside of class B. Aside from that, assuming there are no other constructors, what you have won't work because Spring will look for a default constructor. I think what you're asking is what to do if you want to
@Autowired
that constructor to get String and boolean values in it. If so, you want something like this:In this situation,
@Value
acts somewhat like@Qualifier
would if you had multiple beans of type A.