【java项目】Service注入或Bean注入两种实现方法

背景: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注入,会导致注入失败。








评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值