( Spring 版本为4.1.6) 我有一个服务接口(interface)“ContractService”,用于检索某人的契约(Contract)。 2 个服务类 ContratServiceImpl 和 ContratServiceImplWeb 实现此接口(interface)。 ContratServiceImpl调用dao获取数据 ContratServiceImplWeb 调用 Web 服务...
接口(interface)ContractService被另一个服务(UTService)使用:
@org.springframework.stereotype.Service
@Transactional(value="transactionManager")
public class UTServiceImpl implements UTService {
@Autowired @Qualifier(...<variable.propeties>...)
private ContractService contractService;
...
}
这里是实现接口(interface)的服务
@Service("ContratServiceImplWeb") @Transactional(value="transactionManager")
class ContratServiceImplWeb implements ContratService {
...
}
@Service("ContratServiceImpl") @Transactional(value="transactionManager")
class ContratServiceImpl implements ContratService {
...
}
Configuration class
@Configuration
@EnableTransactionManagement
@ImportResource(value={ "classpath:sessionFactory-datasource-spring.xml", "classpath:contrat_factories.xml"} )
@ComponentScan(basePackages = { "com.xxx.core.service.impl"
, "com.xxx.core.dao.impl"}
)
public class ContextCoreServiceConfiguration {
}
当我的应用程序启动时,我想使用 ContratServiceImpl 或 ContratServiceImplWeb “限定”ContractService,具体取决于设置 bean 名称的外部配置文件(.properties 或 XML)。
我该怎么做?
----- 最后评论 -------- ContractServiceImpl 和 ContractServiceImplWeb 设置为 @service 并通过配置进行扫描。
在“UTServiceImple”类中,2 个类中的 1 个需要使用区分参数进行@autowired(可以配置文件吗?)。这并不是唯一受关注的类组。
一对夫妇的个人资料不应相同。 那么,我必须为每组类(class)设置一个配置文件吗?
最佳答案
我想 Spring 配置文件正是您正在寻找的。p>
它们允许您根据参数( Activity 配置文件)动态选择特定的 bean 实现。
详细信息请查看here if you are using boot或here if using raw spring .
关于java - Spring Qualifier,应用程序启动时的服务实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30713244/