打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring boot 多模块入门

一、创建项目

此时不需要关注这是个spring boot项目,就按照java项目创建。

1、外层创建一个空项目,不用勾选;

  • 简单起见,项目名就叫做boot

2、删除src目录;

3、创建四个模块,都选择quickstart,包括web模块也是;

  • boot-common
  • boot-core
  • boot-dao
  • boot-web

4、打开右侧Maven Project,如果有某层是灰度显示的话

右键Model,选择Add FrameWork Support...

勾选上Maven即可

5、配置依赖关系

common ---> dao ---> core  ---> web

web依赖core层,core层依赖dao层,dao依赖common层。

二、基础配置

1、首先是外层pom.xml

    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.9.RELEASE</version>        <relativePath/>    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>

2、其次是boot-web层配置,这里边要加上构建的配置

    <build>        <finalName>boot</finalName>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <configuration>                    <mainClass>cn.amos.web.BootWebApplication</mainClass>                    <layout>ZIP</layout>                </configuration>                <executions>                    <execution>                        <goals>                            <goal>repackage</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build>

3、创建WebApplication类,设置main启动

@SpringBootApplicationpublic class BootWebApplication {    public static void main(String[] args) {        SpringApplication.run(BootWebApplication.class, args);    }}

4、添加Controller,测试demo

@RestControllerpublic class HelloController {    @RequestMapping("hello")    public String sayHello() {        return "Hello Spring boot!";    }}

5、web层resources里添加application.yml

server:    port: 8085    context-path: /boot

最后测下吧 http://127.0.0.1:8085/boot/hello

三、简单逻辑

需求:在web层的controller里边打印一句core层返回的话

========== HelloBusiness.java ======================================================public interface HelloBusiness {    /**     * say hello     *     * @return String     */    String sayHello();}=========== HelloBusinessImpl.java ==================================================@Component("helloBusiness")public class HelloBusinessImpl implements HelloBusiness {    @Override    public String sayHello() {        return this.getClass().getSimpleName() + " Say Hello!";    }}========== HelloController.java =====================================================@RestControllerpublic class HelloController {    @Resource    private HelloBusiness helloBusiness;    @RequestMapping("hello")    public String sayHello() {        return helloBusiness.sayHello();    }}===================================================================================

按照以前的思维,这样写是对的

但是:运行的时候报错

A component required a bean of type ''cn.amos.core.business.HelloBusiness'' that could not be found.

怎么办呢?

经过上时间的调试,准备在程序的入口WebApplication上入手

加上两个注解搞定,实现没问题,具体细节呢,详见文末。

@Configuration
@ComponentScan("cn.amos")

@Configuration@ComponentScan("cn.amos")@SpringBootApplicationpublic class BootWebApplication {    public static void main(String[] args) {        SpringApplication.run(BootWebApplication.class, args);    }}

四、测试运行

http://127.0.0.1:8085/boot/hello

五、注解备注:

1、@Configuration

声明当前类是一个配置类

2、@ComponentScan

使用方式如下:

@ComponentScan("cn.amos")

3、@ComponentScans

使用方式如下:

@ComponentScans({@ComponentScan("cn.amos.core"), @ComponentScan("cn.amos.dao")})

4、@SpringBootApplication

@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan

局限性:@SpringBootApplication里的@ComponentScan只能自动扫描当前包下所有使用@Service、@Component、@Repository和@Controller的类,并注册为bean。

所以为了扫描到其他模块下声明的bean,我们需要从WebApplication类入手,有以下三种实现方式:

我们需要扫描 boot-dao | boot-core | boot-web三层,拿到使用@Component(@Service/@Repository/@Controller)注解的类.====================================================================1.自我推荐方式,类不会被重复加载,没有用到默认的 @SpringBootApplication  这是一种捷径吧,包名一般都是cn.amos.*.*的,所以cn.amos能扫描所有类.@Configuration@ComponentScan("cn.amos")@EnableAutoConfigurationpublic class BootWebApplication {    public static void main(String[] args) {        SpringApplication.run(BootWebApplication.class, args);    }}====================================================================2.这种方式配置@ComponentScans,类也不会被重复加载,相对配置多了点@Configuration@ComponentScans({@ComponentScan("cn.amos.core"), @ComponentScan("cn.amos.dao")})@SpringBootApplicationpublic class BootWebApplication {    public static void main(String[] args) {        SpringApplication.run(BootWebApplication.class, args);    }}====================================================================3.这种方式看起来简单,但是web层的bean被加载了两次,这就不好了@Configuration@ComponentScan("cn.amos")@SpringBootApplicationpublic class BootWebApplication {    public static void main(String[] args) {        SpringApplication.run(BootWebApplication.class, args);    }}

 

感谢阅读,不当之处,望多多指教

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring Boot + MyBatis 多模块搭建教程
深度解析 Spring Boot 以及手写一个 starter
SpringBoot初体验及原理解析
小白搞 Spring Boot单元测试
Spring Boot 整合 MyBatis
spring-boot(六)缓存配置
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服