SpringMVC Dao层注入到Service层,Service层注入到controller层及相关XML

1 本人新手,刚开始学习SringMVC框架,需要各个层之间的注入。

举个例子:

最初,在dao层实现service的方式是,new出来一个(每个方法都要new一次)。

IServiceImpl service =new IServiceImpl();

service.fill();//fill是我服务中的方法

 完成注入之后,每个方法使用service如下:

service.fill();//service提前声明了。下面会声明

2 Service层注入到controller层

首先在Service的实现类中添加如下代码:

@Service("Service")//要添加的
public class IServiceImpl implements IService { //这是我的service实现类

然后,在controller类中添加如下代码:

public class Testcontroller {//这是我的controller实现类
     @Autowired//要添加的
     @Qualifier("Service")//要添加的
    private IServiceImpl service;//要添加的,这是上文提及的声明

接下来你在controller的方法中就可以直接使用:

service.fill();//fill是我服务中的方法

3Dao层注入到Service层

和 "Service层注入到controller层"样式差不多

在Dao实现类上面加入:

@Repository(value="userDao")//要添加的

public class IDaoImpl implements IDao {//我的Dao层实现类

然后在Service实现类中添加:

public class IServiceImpl implements IService {//这是我的service实现类
     //@Autowired
     @Resource(name = "userDao")//要添加的

4 XML文件的设置(需要添加的那一行)

Spring-dao.xml

 <context:compoSpring-mvc.xmlnent-scan base-package="com.test.dao.impl"/>//com.test.dao.impl是我的IDaoImpl所在的包

Spring-service.xml

 <context:component-scan base-package="com.test.service.impl"/>//com.test.service.impl是我的IServicempl所在的包

当然,你也在你的Spring-mvc.xml中填入下面的话,也是OK的,目的是为了将你包中的类扫描出来

 <context:component-scan base-package="com.testl"/>//com.test.dao.impl是我的IDaoImpl所在的包
    

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值