gpt4 book ai didi

spring-boot - 在 Spring Boot 中 Autowiring 参数化构造函数

转载 作者:行者123 更新时间:2023-12-04 00:02:30 25 4
gpt4 key购买 nike

在参数化构造函数中传递值时,我无法 Autowiring bean。

如何使用 SpringBoot 调用参数化构造函数?

@Component
public class MainClass {

public void someTask() {
AnotherClass obj = new AnotherClass(1, 2);
}

}
//Replace the new AnotherClass(1, 2) using Autowire?

@Component
public class AnotherClass {
private int number,age;

public AnotherClass(int number, int age) {
super();
this.number = number;
this.age = age;
}
}

我想 Autowiring “AnotherClass”bean。
如何删除 new AnotherClass(1, 2);我该如何放置 @Autowire这里?

最佳答案

您需要在构造函数中指定此 bean:

@Component
public class MainClass {

private final AnotherClass anotherClass;

// this annotation is NOT required if there is only 1 constructor, shown for clarity.
@Autowired
MainClass(AnotherClass anotherClass) {
this.anotherClass = anotherClass;
}
public void someTask() {
// anotherClass is already instantiated by the time you get here.
}

}

选项 1:直接允许 AnotherClass使用组件扫描创建。

现在,为了让 Spring 能够构建 AnotherClass作为 bean,您需要以“Spring 方式”告诉它从何处获取值:

@Component
public class AnotherClass {
private final int number,age;

// also not needed if this is the only constructor.
@Autowired
public AnotherClass(
// @Value is a spring annotation to provide spring the value it needs for this parameter.
@Value("${property.number:0}") int number,
@Value("${property.age:0}") int age) {
this.number = number;
this.age = age;
}
}

这样做的目的是拉取 2 个属性, property.numberproperty.age来自 application.properties | application.yml对于这些整数的值。

您需要确保这两个类都在组件扫描路径上,否则 spring boot 将不会尝试制作这些类的 bean。

选项 2:使用配置类使 AnotherClass bean 角,扁 bean

@Configuration
public class MyConfigurationClass {

@Bean
public AnotherClass anotherClass {
return new AnotherClass(1,2)
}
}

在这个例子中,你 不会 批注 AnotherClass@Component .

选项 3:使用自定义工厂方法 as found in this blog .

同样,使用此策略, 不要批注 AnotherClass@Component
@Configuration
public class MyConfigurationClass {

@Bean
public BiFunction<Integer, Integer, MyPrototype> myPrototypeFactory() {
return start, age -> anotherClass(start, age);
}

@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public AnotherClass anotherClass(Integer start, Integer age) {
if (start == null || age == null) {
throw new IllegalArgumentException("start was: " + start + ", age was: " + age + ". Neither can be null!");
}
return new AnotherClass(start,age);
}
}

用法:

@Component
public class MainClass {

private final BiFunction<Integer, Integer, AnotherClass> anotherClassFactory;

// this annotation is NOT required if there is only 1 constructor, shown for clarity.
@Autowired
MainClass(BiFunction<Integer, Integer, AnotherClass> anotherClassFactory) {
this.anotherClassFactory = anotherClassFactory;
}
public void someTask() {
AnotherClass ac = anotherClassFactory.apply(1,2);
// do something with your new AnotherClass
}

}

选项 4:使用 ObjectProvider (自 Spring 4.3 起) as found in this blog post .

同样,使用此策略, 不要批注 AnotherClass@Component
@Configuration
public class MyConfiguration {
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public AnotherClass createAnotherClass(Integer start, Integer age) {
return new AnotherClass(start, age);
}
}

用法:

@Component
public class MainClass {

private final ObjectProvider<AnotherClass> anotherClassProvider;

// this annotation is NOT required if there is only 1 constructor, shown for clarity.
@Autowired
MainClass(ObjectProvider<AnotherClass> anotherClassProvider) {
this.anotherClassProvider = anotherClassProvider;
}
public void someTask() {
// may need to cast the result of '.getObject()'
AnotherClass ac = anotherClassProvider.getObject(/*start*/ 1, /*age*/ 2);
// do something with your new AnotherClass
}

}

关于spring-boot - 在 Spring Boot 中 Autowiring 参数化构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57926579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com