Java Spring 启动配置标准
Java Spring Boot Config Standard
我有这样的界面层次结构:
public interface Shape {
//code
}
@Component
public class Circle implements Shape {
//code
}
@Component
public class Square implements Shape {
//code
}
我想知道使用 Spring 引导 bean 约定连接这些的最佳方法。
解决方案一:
@Component(value = "Circle")
public class Circle implements Shape {
//code
}
@Component(value = "Square")
public class Square implements Shape {
//code
}
@Configuration
public class ShapeConfig {
@Bean
Foo circleFoo(@Qualifiers("Circle") Shape shape) {
return new Foo(shape);
}
@Bean
Foo squareFoo(@Qualifiers("Square") Shape shape) {
return new Foo(shape);
}
}
方案二:
@Component
public class Circle implements Shape {
//code
}
@Component
public class Square implements Shape {
//code
}
@Configuration
public class ShapeConfig {
@Bean
Foo circleFoo(Circle shape) {
return new Foo(shape);
}
@Bean
Foo squareFoo(Square shape) {
return new Foo(shape);
}
}
在这种情况下,最好的 java/spring 做法是什么?我发现 value 和 @Qualifier 的东西有点冗长,但我想知道具体实现中的接线是否不受欢迎
这取决于您的应用实施
在自动装配的情况下,spring 首先尝试按名称自动装配,如果没有找到,则按类型查找,然后按构造函数查找(如果按类型未找到任何 bean)。
除非我们没有多个具有相同类型和不同名称的 bean,否则我们可以使用解决方案 2(我们也可以使用 autowire byName 而不是 by constructor ),但如果我们有 2 个或更多,则2 个相同类型的 bean,然后我们使用解决方案 1(限定符)
例如:
@Configuration
public class Config {
@Bean(name = "circle1")
public Circle getCircle1(){
Circle c = new Circle();
c.setRadius(1.5);
return c;
}
@Bean(name = "circle2")
public Circle getCircle2(){
Circle c = new Circle();
c.setRadius(10);
return c;
}
}
假设我有一个服务
@Component
CirculeService {
@Autowire @Qualifier("circle1") Circle circle1
@Autowire @Qualifier("circle2") Circle circle2
}
在上面的例子中,我在限定符的帮助下进行了自动装配(与构造函数的自动装配相同)
我有这样的界面层次结构:
public interface Shape {
//code
}
@Component
public class Circle implements Shape {
//code
}
@Component
public class Square implements Shape {
//code
}
我想知道使用 Spring 引导 bean 约定连接这些的最佳方法。
解决方案一:
@Component(value = "Circle")
public class Circle implements Shape {
//code
}
@Component(value = "Square")
public class Square implements Shape {
//code
}
@Configuration
public class ShapeConfig {
@Bean
Foo circleFoo(@Qualifiers("Circle") Shape shape) {
return new Foo(shape);
}
@Bean
Foo squareFoo(@Qualifiers("Square") Shape shape) {
return new Foo(shape);
}
}
方案二:
@Component
public class Circle implements Shape {
//code
}
@Component
public class Square implements Shape {
//code
}
@Configuration
public class ShapeConfig {
@Bean
Foo circleFoo(Circle shape) {
return new Foo(shape);
}
@Bean
Foo squareFoo(Square shape) {
return new Foo(shape);
}
}
在这种情况下,最好的 java/spring 做法是什么?我发现 value 和 @Qualifier 的东西有点冗长,但我想知道具体实现中的接线是否不受欢迎
这取决于您的应用实施
在自动装配的情况下,spring 首先尝试按名称自动装配,如果没有找到,则按类型查找,然后按构造函数查找(如果按类型未找到任何 bean)。
除非我们没有多个具有相同类型和不同名称的 bean,否则我们可以使用解决方案 2(我们也可以使用 autowire byName 而不是 by constructor ),但如果我们有 2 个或更多,则2 个相同类型的 bean,然后我们使用解决方案 1(限定符) 例如:
@Configuration
public class Config {
@Bean(name = "circle1")
public Circle getCircle1(){
Circle c = new Circle();
c.setRadius(1.5);
return c;
}
@Bean(name = "circle2")
public Circle getCircle2(){
Circle c = new Circle();
c.setRadius(10);
return c;
}
}
假设我有一个服务
@Component
CirculeService {
@Autowire @Qualifier("circle1") Circle circle1
@Autowire @Qualifier("circle2") Circle circle2
}
在上面的例子中,我在限定符的帮助下进行了自动装配(与构造函数的自动装配相同)