背景:controller及service上级路径均为"com.pcl.los"
一、Service注入
1.在进入项目执行applicationContext.xml时先扫描com.pcl.los
<context:component-scan base-package="com.pcl.los" />
2.在ServiceImpl实现类最上方写入:@Service("iDepartmentsSrv")
@Service("iDepartmentsSrv")
public class DepartmentsSrvImpl implements IDepartmentsSrv {
//方法块
}
3.在Controller中使用
@Autowired
IDepartmentsSrv iDepartmentsSrv;
二、Bean注入
1.在进入项目执行applicationContext.xml时先扫描 配置文件beanConfig.xml
<context:component-scan base-package="com.pcl.los.controller" />
<import resource="beanConfig.xml"/>
2.在beanConfig.xml中注入
<bean id="iDepartmentsSrv" class="com.pcl.los.service.impl.DepartmentsSrvImpl"></bean>
3.在Controller中使用
@Autowired
IDepartmentsSrv iDepartmentsSrv;
或这样:
2.在beanConfig.xml中注入
<!-- 客户服务 -->
<bean id="iDepend_09" class="esbSrv.depend.service.impl.Depend_09_impl"></bean>
<!-- 账户服务依赖客户-->
<bean id="iDeductionService" class="account.service.impl.DeductionServiceImpl">
<property name="iDepend_09" ref="iDepend_09"></property>
</bean>
<!-- 其他服务依赖账户服务-->
<bean id="srv09_001" class="esbSrv.provide.service.impl.EsbSrvImpl09_001">
<property name="iDeductionService" ref="iDeductionService"></property>
</bean>
3.在ServiceImpl中使用
//客户服务
private IDepend_09 iDepend_09;
public void setiDepend_09(IDepend_09 iDepend_09) {
this.iDepend_09 = iDepend_09;
}
注意:不可同时存在Bean注入又Service注入,会导致注入失败。