带你走进Spring-Boot-项目实战:Maven-多模块项目搭建

com.example.demo demo-biz ${demo.version} com.example.demo demo-common ${demo.version} com.example.demo demo-dao ${demo.version} com.example.demo demo-web ${demo.version}

注:${demo.version} 定义在 properties 标签中

② 其次在 demo-biz 层中的 pom 文件中添加 demo-dao 及 demo-common 依赖

com.example.demo demo-common com.example.demo demo-dao

③ 之后在 demo-web 层中的 pom 文件中添加 demo-biz 依赖

com.example.demo demo-biz
4.6 web 层调用 biz 层接口测试

模块依赖关系配置完成之后,通过 web 层 测试下 biz 层的接口是否可以正常调用。

① 首先在 demo-biz 层创建 com.example.demo.biz 包,添加 service 目录并在其中创建 DemoService 接口类及 impl 目录(用于存放接口实现类)。

package com.example.demo.biz.service;

/**

  • @author linjian
  • @date 2019/1/15
    */
    public interface DemoService {

String test();
}

package com.example.demo.biz.service.impl;

import com.example.demo.biz.service.DemoService;
import org.springframework.stereotype.Service;

/**

  • @author linjian
  • @date 2019/1/15
    */
    @Service
    public class DemoServiceImpl implements DemoService {

@Override
public String test() {
return “interface test”;
}
}

② DemoController 通过 @Autowired 注解注入 DemoService ,修改 DemoController 的 test 方法使之调用 DemoService 的 test 方法

@Autowired
private DemoService demoService;

@GetMapping(“test”)
public String test() {
return demoService.test();
}

③ 再次运行 DemoWebApplication 类中的 main 方法启动项目,发现如下报错


APPLICATION FAILED TO START


Description:

Field demoService in com.example.demo.web.controller.DemoController required a bean of type ‘com.example.demo.biz.service.DemoService’ that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type ‘com.example.dem

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值