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);
    }
}

 

感謝閱讀,不當之處,望多多指教

相關文章
相關標籤/搜索