Lar*_*erg 7 spring inject overwrite javabeans autowired
鉴于我将Spring bean配置为
@Service("myService")
public class DefaultService extends MyService {
}
Run Code Online (Sandbox Code Playgroud)
和一个使用这个bean的类
public class Consumer {
@Autowired
@Qualifier("myService")
private MyService service;
...
}
Run Code Online (Sandbox Code Playgroud)
我现在希望我的项目(包括前面的类)有Consumer另一个MyService被注入的实现.因此我想覆盖beanmyService
@Service("myService")
public class SpecializedService implements MyService {
}
Run Code Online (Sandbox Code Playgroud)
导致Consumer现在携带一个SpecializedService而不是DefaultService.根据定义,我不能在Spring容器中有两个具有相同名称的bean.我怎么能告诉spring,新服务的定义是否会覆盖旧服务?我不想修改这个Consumer类.
小智 3
显式定义服务 bean
<bean id="myService" class="x.y.z.SpecializedService" />
Run Code Online (Sandbox Code Playgroud)
或对其进行组件扫描。
无论哪种情况,在您的应用程序上下文中,都应避免显式定义 DefaultService 并避免对其进行组件扫描。
| 归档时间: |
|
| 查看次数: |
32914 次 |
| 最近记录: |