Java 如何用动态值实例化构造函数并自动连接它?
我在这里实例化了一个参数化构造函数,称为带有动态值的请求操作。如何将此@Autowire连接到Requestclass?随后,在Request类中,我创建了一个新的评级响应如何@Autowire这个呢 类初始值设定项Java 如何用动态值实例化构造函数并自动连接它?,java,spring-boot,constructor,autowired,spring-annotations,Java,Spring Boot,Constructor,Autowired,Spring Annotations,我在这里实例化了一个参数化构造函数,称为带有动态值的请求操作。如何将此@Autowire连接到Requestclass?随后,在Request类中,我创建了一个新的评级响应如何@Autowire这个呢 类初始值设定项 public class Intializer { NewClass newclass = new NewClass(); String testName = Number + "_" + "Test"; -->getting the String number dynam
public class Intializer
{
NewClass newclass = new NewClass();
String testName = Number + "_" + "Test"; -->getting the String number dynamically
Test test = new Test(testName); -> this is a different class
Operation operation = new RequestOperation(test, newclass ,
sxxx, yyy, zzz); - argumented constructor
opertaion.perform();
}
RequestClass
public class RequestOperation implements Operation {
// the constructor
public RequestOperation(Test test, Sheet reportSheet, XElement element, TestDataManager testDataManager, Report report)
{
this.test = test;
this.newclass = newclass ;
this.sxxx= sxxx;
this.yyy= yyy;
this.zzz= zzz;
}
@Override
public boolean perform(String CompanyName, String Province) {
Response response = new RatingResponse(this.test, this.reportSheet,
callService(this.buildRequest(CompanyName, Province)), this, this.report);-> create a new paramterizedconstructor
}
private String buildRequest(String CompanyName, String Province) {
return pm.getAppProperties().getProperty(constructedValue); }
}
**响应类**
public class RatingResponse implements Response {
public RatingResponse(Test test, Sheet reportSheet, Object obj, RequestOperation requestOperation, Report report) {
this.test = test;
if (obj instanceof Document) {
this.document = (Document) obj;
}
this.operation = requestOperation;
this.reportSheet = reportSheet;
this.report = report;
}
**接口**
@Component
public interface Operation {
public boolean perform(String Name, String Province);
}
@Component
public interface Response {
void construct();
}
在SpringBoot中,您只能自动连接用@Bean标记的类型或用@Component标记的类或其派生类,如@Service、@Controller 这些注释的特点是,只有一个类的实例保存在内存中 因此,如果您的需求需要为每一组新的动态值创建新的类,那么自动关联它们不是正确的方法 但是,如果您的类可以拥有的可能的动态值数量有限,那么您可以像这样为每个动态值创建bean
@Configuration
class MyBeans{
@Bean
public RatingResponse ratingResponse(){
Response response = new RatingResponse(this.test, this.reportSheet,
callService(this.buildRequest(CompanyName, Province)), this, this.report);
return response
}
}
然后在课堂上你需要使用它,你可以
@Autowired
RatingResponse ratingResponse
我应该如何在MyBeans中获得test、reportsheet等的值