解决方案 1
通常实例化FirstCSVConcrete和FirstXLSConcrete类型的bean,即:
@Component
public class FirstCSVConcrete implements FirstInterface {
@Autowired
private TotalReport totalReport;
}
FirstXLSConcrete 也一样。
现在,如下设置您的MainFactory:
public class MainFactory {
@Autowired
private FirstCSVConcrete firstCSVConcrete;
@Autowired
private FirstXLSConcrete firstXLSConcrete;
private String type;
public static MainFactory newInstance(String type){
return new MainFactory(type);
}
private MainFactory(String type) {
this.type = type;
}
private FirstInterface selectedFirstInterface = null;
public FirstInterface getFirstInterface() {
if(selectedFirstInterface == null) {
selectedFirstInterface = selectFirstInterfaceForType(type);
}
return selectedFirstInterface;
}
private FirstInterface selectFirstInterfaceForType(String type) {
if("CSV".equalsIgnoreCase(type)) {
return firstCSVConcrete;
}
return firstXLSConcrete;
}
}
Spring 无法将依赖项注入到您自己实例化的对象中。因此,您必须采用这种方法,或者其中一种变体。
解决方案 2:更整洁;在 Spring 中使用 aspectj
@Component
public class TotalReport {
...
}
@Configurable
public class FirstXLSConcrete implements FirstInterface {
@Autowired
private TotalReport totalReport;
...
}
@Configurable
public class FirstCSVConcrete implements FirstInterface {
@Autowired
private TotalReport totalReport;
}
public class MainFactory {
private FirstInterface firstInterface;
private MainFactory(String type) {
if (type.equalsIgnoreCase("CSV")) {
firstInterface = new FirstCSVConcrete();
} else {
firstInterface = new FirstXLSConcrete();
}
System.out.println(firstInterface.getTotalReport());
}
public static MainFactory newInstance(String type) {
return new MainFactory(type);
}
}
在您的应用程序上下文配置文件中声明以下内容:
<context:load-time-weaver />
<context:spring-configured />
在应用程序的META-INF 目录中创建一个context.xml 并将以下内容放入其中。
<Context path="/youWebAppName">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
useSystemClassLoaderAsParent="false"/>
</Context>
将 Spring 的 spring-tomcat-weaver.jar(可能命名为 org.springframework.instrument.tomcat-<version>.jar)放在你的 tomcat 安装的 lib 目录中,瞧,aspectj 魔法开始起作用了。对于带有@Configurable注解的类,@Autowired的依赖会自动解析;即使实例是在 spring-container 之外创建的。
我想答案现在已经变得很长了。如果您想了解更多详细信息,这里是using aspectj with Spring 的链接。还有几个配置选项。把自己打晕。