Java 如何自动连接相互依赖的接口的不同实现?
我对Spring非常陌生,刚刚被分配到一个Spring/Java项目上 SftpAccessPoint有一个作为字段的接口(MCertificatePository)。该字段连接到静态内部类(DependencyProvider)中的方法中,如下所示Java 如何自动连接相互依赖的接口的不同实现?,java,spring,Java,Spring,我对Spring非常陌生,刚刚被分配到一个Spring/Java项目上 SftpAccessPoint有一个作为字段的接口(MCertificatePository)。该字段连接到静态内部类(DependencyProvider)中的方法中,如下所示 public class SftpAccessPoint { private static CertificateRepository mCertificateRepository; public static
public class SftpAccessPoint {
private static CertificateRepository mCertificateRepository;
public static class DependencyProvider {
@Autowired
public void setCertificateRepository(final CertificateRepository pCertificateRepository) {
SftpAccessPoint.mCertificateRepository = pCertificateRepository;
}
}
}
相关bean的配置如下所示:
<bean id="certificateRepositoryImpl" class="com.full.appservices.impl.CertificateRepositoryImpl" />
<bean class="com.connector.sftp.accesspoint.SftpAccessPoint$DependencyProvider" />
我假设我需要在代码中添加一些注释,并使用Bean config为测试上下文创建一个xml。如何实现以下目标:
*我非常喜欢不改变SftpAccessPoint,除非没有其他好的选择Spring有概要文件的概念。这意味着您可以指定根据应用程序设置将不同的bean集加载到应用程序上下文中。完整文档位于此处:。但本质上,每个环境可以有一组单独的BER定义。在您的情况下,类似这样的情况:
<beans profile="test">
<bean id="certificateRepositoryImpl" class="...MockCertificateRepository" />
</beans>
<beans profile="production">
<bean id="certificateRepositoryImpl" class="...CertificateRepositoryImpl" />
</beans>
<beans profile="common"/>
spring.profiles.active=dev,common
或
在测试应用程序上下文配置中尝试类似的操作
<bean class="com.connector.sftp.accesspoint.SftpAccessPoint$DependencyProvider" />
<bean id="mockCertificateRepository" class="com.full.appservices.impl.MockCertificateRepository">
<constructor-arg>
<bean id="certificateRepositoryImpl" class="com.full.appservices.impl.CertificateRepositoryImpl" />
</constructor-arg>
</bean>
spring.profiles.active=production,common
<bean class="com.connector.sftp.accesspoint.SftpAccessPoint$DependencyProvider" />
<bean id="mockCertificateRepository" class="com.full.appservices.impl.MockCertificateRepository">
<constructor-arg>
<bean id="certificateRepositoryImpl" class="com.full.appservices.impl.CertificateRepositoryImpl" />
</constructor-arg>
</bean>