小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring boot 多模塊入門

 huowufenghuang 2019-01-22

一、創(chuàng)建項目

此時不需要關注這是個spring boot項目,就按照java項目創(chuàng)建。

1、外層創(chuàng)建一個空項目,不用勾選;

  • 簡單起見,項目名就叫做boot

2、刪除src目錄;

3、創(chuàng)建四個模塊,都選擇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、創(chuàng)建WebApplication類,設置main啟動

@SpringBootApplication
public class BootWebApplication {

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

4、添加Controller,測試demo

@RestController
public 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 =====================================================

@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上入手

加上兩個注解搞定,實現(xiàn)沒問題,具體細節(jié)呢,詳見文末。

@Configuration
@ComponentScan("cn.amos")

@Configuration
@ComponentScan("cn.amos")
@SpringBootApplication
public 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類入手,有以下三種實現(xiàn)方式:

我們需要掃描 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);
    }
}

 

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

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多