Spring boot 多模块入门

单模块demo:https://github.com/AmosWang0626/boot-singlejava

多模块demo:https://github.com/AmosWang0626/bootgit

1、建立项目

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

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

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

二、删除src目录;

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

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

四、打开右侧Maven Project,若是有某层是灰度显示的话

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

勾选上Maven便可spring

五、配置依赖关系

common ---> dao ---> core  ---> webapp

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

2、基础配置

一、首先是外层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>

二、其次是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>

三、建立WebApplication类,设置main启动

@SpringBootApplication
public class BootWebApplication {

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

四、添加Controller,测试demo

@RestController
public class HelloController {

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

五、web层resources里添加application.yml

server:
    port: 8085
    context-path: /boot

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

3、简单逻辑

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

========== 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 =====================================================

@RestController
public 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")
@SpringBootApplication
public class BootWebApplication {

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

4、测试运行

http://127.0.0.1:8085/boot/hello

5、注解备注:

一、@Configuration

声明当前类是一个配置类

二、@ComponentScan

使用方式以下:

@ComponentScan("cn.amos")

三、@ComponentScans

使用方式以下:

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

四、@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")
@EnableAutoConfiguration
public 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")})
@SpringBootApplication
public class BootWebApplication {

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

====================================================================
3.这种方式看起来简单,可是web层的bean被加载了两次,这就很差了

@Configuration
@ComponentScan("cn.amos")
@SpringBootApplication
public class BootWebApplication {

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

 

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

相关文章
相关标签/搜索