我创建了一个类(名为iperfAndroidApp),它实现了一些接口(命名为AppService),并创建了另一个使用此iperfAndroidApp的类(命名为IperfApp).IperfApp应该是其他项目的API。 我做了这一切,以便将来能够在Android实例和IOS之间进行替换。 我希望iperfAndroidApp类将成为xml文件中的bean。
我得到堆栈的意思是IperfApp在构造函数中获取一些参数(来自外部项目),我想将它们绑定到该bean的构造函数,所以我不必使用“new”(行) 8)。
我删除了不相关的部分以便于阅读。
//THE API THAT I AM CREATING
public class IperfApp {
//THE SERVICE THAT SHOULD BE INITIALIZED WITH ANDROID OR IOS
AppService iperf;
//I WANT TO PASS THE ARGUMENTS TO THE BEAN INSTEAD OF USING NEW
public IperfApp(List<String> udids, List<String> commands, int runTime) {
iperf=new IperfAndroidApp(udids, commands, runTime);
}
...
}
//THE BEAN I WANT TO USE
public class IperfAndroidApp extends AndroidApp {
private AndroidServer androidServer;
private List<String> commands;
private int runTime;
public IperfAndroidApp(List<String> udids, List<String> commands, int runTime) {
super(udids);
this.commands = commands;
this.runTime = runTime;
}
....
}
public abstract class AndroidApp extends AppClient {...}
public abstract class AppClient implements AppService {...}
spring配置文件中的bean:
<bean id="iperfInstance"
class="com.airspan.testspan.smarphones.Client.IperfAndroidApp" scope="singleton">
<!-- want to bind args from the code here -->
</bean>
我也尝试过使用spring class配置的不同方法
@Configurable
@ComponentScan("com.airspan.testspan.smarphones")
@PropertySource("classpath:config.properties")
public class AppConfigurations {
//--------- to load the config.property file need to initialize it for spring-----------
@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
//---------------------Beans of Apps------------------------
@Bean
public IperfAndroidApp iperfAppInstance(List<String> udids, List<String> commands, int runTime){
return new IperfAndroidApp(udids, commands, runTime);
}
}
public class IperfAppRunner extends AppRunner {
private AppService iperf;
public IperfAppRunner(List<String> udids, List<String> commands, int runTime) {
//iperf = new IperfAndroidApp(udids, commands, runTime);
iperf=(AppService) context.getBean("iperfAppInstance", udids, commands, runTime);
}
...
}
但它似乎没有传递参数..
我得到了这个例外:
Error creating bean with name 'iperfAppInstance' defined in com.airspan.testspan.smarphones.AppConfigurations: Unsatisfied dependency expressed through method 'iperfAppInstance' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<java.lang.String>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
所以基本上我正在尝试使用“spring factory”创建一个对象,其中包含一些给我的参数。
可以这样做吗?如果是这样,怎么样?