zoukankan      html  css  js  c++  java
  • 03.父工程pom、整合测试、SpringBootApplication注解

    父工程

    idea点击spring-boot-starter-parent找到父工程spring-boot-dependencies模仿配置

    • 父工程
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.fly</groupId>
        <artifactId>SpringDemoRoot</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>1.5.6.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    • 子工程
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.fly</groupId>
        <artifactId>SpringDemo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>com.fly</groupId>
            <artifactId>SpringDemoRoot</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    </project>
    

    SpringBoot整合测试

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    
    • 测试
    import com.fly.IndexController;
    import junit.framework.TestCase;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.web.WebAppConfiguration;
    
    @SpringBootTest(classes = IndexController.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    public class TestIndexController {
        @Autowired
        private IndexController indexController;
    
        @Test
        public void test1(){
            //断言indexController的first()返回"hello:fly"
            TestCase.assertEquals(this.indexController.first(),"hello:fly");
        }
    }
    

    SpringBootApplication注解

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    
    @EnableAutoConfiguration
    @ComponentScan("com.fly")
    public class SpringDemoApp {
        public static void main(String[] args){
            SpringApplication.run(SpringDemoApp.class,args);
        }
    }
    

    使用组合注解SpringBootApplication注解:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    //@EnableAutoConfiguration
    //@ComponentScan("com.fly")
    @SpringBootApplication(scanBasePackages = {"com.fly"})//默认是本包及子报
    public class SpringDemoApp {
        public static void main(String[] args){
            SpringApplication.run(SpringDemoApp.class,args);
        }
    }
    
  • 相关阅读:
    字节流与字符流的区别?
    启动一个线程是用run()还是start()?
    Java中的异常处理机制的简单原理和应用?
    java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
    Java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用?
    运行时异常与一般异常有何异同?
    常用清除浮动的方法,如不清除浮动会怎样?
    多线程有几种实现方法?同步有几种实现方法?
    你所知道的集合类都有哪些?主要方法?
    至少两种方式实现自适应搜索?
  • 原文地址:https://www.cnblogs.com/fly-book/p/11563021.html
Copyright © 2011-2022 走看看