我们希望有两个实现生产和开发模式的接口:
考虑一个界面:
public interface AccountList {
public List<Account> getAllAccounts(String userID) ;
}
Run Code Online (Sandbox Code Playgroud)
有两个实现:
基础实现
@Service
public AccountListImp1 interface AccountList { ... }
Run Code Online (Sandbox Code Playgroud)
和一些开发实施
@Service
@Profile("Dev")
public AccountListImp2 interface AccountList { ... }
Run Code Online (Sandbox Code Playgroud)
当我尝试使用bean时:
public class TransferToAccount{
@Autowired
private AccountServices accountServices;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
No qualifying bean of type [AccountList] is defined: expected single matching bean but found 2: coreSabaAccountList,dummyAccountList
Run Code Online (Sandbox Code Playgroud)
在开发过程中,我们设置了spring.profiles.active以dev如下:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>Dev</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
我假设设置配置文件名称,将使spring分类具有不同配置文件的bean,并根据配置文件名称使用它们.
能告诉我怎样才能解决这个问题?我可以使用@Primary,或者更改applicationContext.xml,但我认为@profile应该可以解决我的问题.
Ale*_*exR 10
我认为您的问题是您的基类AccountListImp1没有标记为任何配置文件.我认为如果没有定义活动配置文件,那么将运行没有配置文件规范的bean,但是当您定义配置文件时,具有此类规范的bean将覆盖实现相同接口且没有配置文件定义的bean.这不起作用.
使用活动配置文件时,Xspring会启动所有未针对任何配置文件的bean 以及针对当前配置文件的bean.在您的情况下,这会导致您的两个实现之间发生冲突.
我认为,如果你想使用配置文件,你应该至少定义2:Dev和Prod(这些名称仅作为例子.)
现在标记AccountListImp1的Prod和 AccountListImp2为Dev:
@Service
@Profile("Prod")
public AccountListImp1 interface AccountList { ... }
and some development implementation
@Service
@Profile("Dev")
public AccountListImp2 interface AccountList { ... }
Run Code Online (Sandbox Code Playgroud)
我相信这个配置会起作用.祝好运.我很高兴知道这是否有用.
| 归档时间: |
|
| 查看次数: |
3368 次 |
| 最近记录: |