zoukankan      html  css  js  c++  java
  • 博客园 图灵学院spring boot学习1

     1、整个代码如下

     parent是工程,base是子工程

     我们来看看父工程的pom依赖

    pom.xml

    <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.tuling.springboot</groupId>
        <artifactId>vip-springboot-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <name>vip-springboot-parent</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-parent</artifactId>
                    <version>1.5.10.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.tuling.springboot.base.SpringBootStart</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <modules>
            <module>vip-springboot-base</module>
        </modules>
    </project>

     首先父亲工程的类型是pom类型进行jar包的管理,所以类型为

    <groupId>com.tuling.springboot</groupId>
        <artifactId>vip-springboot-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>

    其次父亲工程要进行jar的管理,这里如何管理的了,引入了spring boot的依赖,让spring boot的parent去管理真正的jar包

    <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-parent</artifactId>
                    <version>1.5.10.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    <type>pom</type>这里表示parent工程中引入的dependency不是我们平常使用的jar,而是一个pom依赖,真正的jar包管理是spring-boot-parent帮助我们实现

    import 它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置

    接下来我们来看base子项目的依赖

    <?xml version="1.0"?>
    <project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.tuling.springboot</groupId>
            <artifactId>vip-springboot-parent</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>vip-springboot-base</artifactId>
        <name>vip-springboot-base</name>
        <url>http://maven.apache.org</url>
        <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>
    <!--         <dependency> -->
    <!--             <groupId>org.springframework</groupId> -->
    <!--             <artifactId>springloaded</artifactId> -->
    <!--         </dependency> -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
        </dependencies>
    </project>

    这里base要是web模块,引入下面的

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

    要引入单元测试,只需要引入下面的starter

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

    要进行热加载,修改代码之后不用重启,引入下面的模块

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>

    上面就是整个springboot的依赖关系

    接下来,我们来看下springboot的启动分析

    package com.tuling.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @EnableAutoConfiguration
    public class SampleController {
    
        @RequestMapping("/")
        @ResponseBody
        String home() {
            return "Hello World!";
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SampleController.class, args);
        }
    }

     现在有上面的这个类SampleController ,在改类上使用了@Controller和@EnableAutoConfiguration注解,改类我们是可以直接运行的

    @EnableAutoConfiguration类默认会扫描当前@EnableAutoConfiguration所在类的注解添加到应用上下文中,@EnableAutoConfiguration会扫描到SampleController 存在@control注解,这样在浏览器就可以http://localhost:8080/访问到了"Hello World!"

    如果把上面的类写成下面的形式,如果@EnableAutoConfiguration与@control注解不再一个类中,@EnableAutoConfiguration默认只能扫描自己所属类中存在的注解,所以扫描不到其他类中的注解,@EnableAutoConfiguration要扫描到其他类中存在的注解,需要引入@ComponentScan(basePackages={"com.tuling.springboot"}),指定

    @EnableAutoConfiguration能够扫描到那些包下面的注解

    package com.tuling.springboot.base;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @EnableAutoConfiguration
    @ComponentScan(basePackages={"com.tuling.springboot"})
    public class SpringBootStart {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SpringBootStart.class, args);
        }
    }

    接下来讲讲@SpringBootConfiguration:他继承@Configuration,说明这是一个配置类,什么是配置类呢?就相当于我们spring中以前写的xml配置,例如我们我们的bean标签,用来实例化一个bean。那么在这个配置类中就是实现以前我们xml配置的功能,因此我们就可以在@SpringBootConfiguration所在的类中定为定义bean对象添加到spring容器中,代码如下所示

    package com.tuling.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @EnableAutoConfiguration
    @SpringBootConfiguration
    public class SampleController {
    
        @RequestMapping("/")
        @ResponseBody
        String home() {
            return "Hello World!";
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SampleController.class, args);
        }
        
        @Bean
        public AA sb(){
            
            new AA();
        }
    }

    我们就可以将一个AA对象的实例注册到spring的容器中

    接下来我们讲解@SpringBootApplication注解

    package com.tuling.springboot.base;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    
    @SpringBootApplication
    public class SpringBootStart {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SpringBootStart.class, args);
        }
    }

    2.@SpringBootApplication配置详解:
    他是一个组合注解,他内部主要包含三个子注解:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan,因为SpringBootApplication默认包含了ComponentScan注解,默认扫描的是@SpringBootApplication所属类所在的包及其子包的注解,上面的代码就是扫描com.tuling.springboot.base包机器子包,我们也可以手动指定要扫描的包

    package com.tuling.springboot.base;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    
    @SpringBootApplication
    @ComponentScan(basePackages={"com.example.boot"}) 
    public class SpringBootStart {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SpringBootStart.class, args);
        }
    }

    总结如下:

    spring boot 启动注解分析
    1.@EnableAutoConfiguration:开启自动配置功能
    @ComponentScan(basePackages={"com.example.boot"}) 包扫描
    
    
    2.@SpringBootApplication配置详解:
    他是一个组合注解,他内部主要包含三个子注解:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan
    
    @SpringBootConfiguration:他继承@Configuration,说明这是一个配置类,什么是配置类呢?就相当于我们以前写的xml配置,例如我们我们的bean标签,用来实例化一个bean。那么在这个配置类中就是实现以前我们xml配置的功能
    
    @EnableAutoConfiguration:开启自动配置功能,他会扫描带有@Configuration的类,然后初始化这些配置类中的信息并且加入到应用上下文中去,同时完成一些基本的初始化工作
    
    @ComponentScan:组件包扫描,也就是我现在需要扫描哪些包下面的注解,可自动发现和装配一些bean。默认扫描当前启动类所在包下面的类和下面的所有子包
  • 相关阅读:
    flask url_for后没有带端口号
    [Flask]通过render_form快捷渲染表单
    [Flask]使用sqlite数据库
    jmeter参数化读取数据进行多次运行
    eclipse 集成jdk
    创建一个gradle项目
    gradle的安装
    springmvc--jsp页面乱码
    APP专项测试使用到的工具
    robotframework实战三--自定义关键字
  • 原文地址:https://www.cnblogs.com/kebibuluan/p/11693208.html
Copyright © 2011-2022 走看看