SPRINGBOOT01_概述、入门案例、父项目依赖管理特性、@Configuration、@Import、@Conditional、@ConfigurationProperties配置绑定(一)

简介: ①. SpringBoot的概述②. HelloWorld项目

①. SpringBoot的概述


①. Spring Boot是一个便捷搭建基于spring工程的脚手架,是整合Spring技术栈的一站式框架


②. 作用


是帮助开发人员快速搭建大型的spring 项目


简化工程的配置,依赖管理


实现开发人员把时间都集中在业务开发上


③. 缺点:


人称版本帝,迭代快,需要时刻关注变化


封装太深,内部原理复杂,不容易精通


④.SrpingBoot官网文档


微信图片_20220107151029.png


微信图片_20220107151105.png


⑤. 查看版本新特性


微信图片_20220107151119.png


②. HelloWorld项目


  • ①. Maven配置文件新添内容:


<mirrors>
  <mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>central</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  </mirror>
</mirrors>
<profiles>
  <profile>
    <id>jdk-1.8</id>
    <activation>
      <activeByDefault>true</activeByDefault>
      <jdk>1.8</jdk>
    </activation>
    <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>
  </profile>
</profiles>


  • ②. 需求:浏览发送/hello请求,响应"Hello,Spring Boot 2”


如何快速搭建helloWorld项目官方文档


  • ③. 引入依赖


 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>
    <dependencies>
        <!--web场景-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!--这个插件,可以将应用打包成一个可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


④. 创建主程序


将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器


想要改变扫描路径


@SpringBootApplication(scanBasePackages=“com.lun”)


@ComponentScan 指定扫描路径,这个不能和@SpringBootApplication一起使用


//@SpringBootApplication 等同于如下三个注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com")//可以扫描com及com下的所有包
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}


⑤. 编写业务

更多配置信息官方文档


@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }
}
server:
  port: 8888


⑥. 运行&测试

运行MainApplication类

浏览器输入http://localhost:8888/hello,将会输出Hello,Spring Boot 2!


⑦. 运用springboot提供的插件进行打包处理,进行maven的打包

用cmd运行java -jar boot-01-helloworld-1.0-SNAPSHOT.jar,既可以运行helloworld工程项目


    <!--这个插件,可以将应用打包成一个可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


微信图片_20220107151259.png



相关文章
|
12月前
|
Java 微服务 Spring
springBoot:@Enable&@import&事件监听 (六)
本文介绍了如何在Spring Boot项目中实现跨项目获取Bean类的方法,包括创建两个项目、在pom文件中导入依赖、定义Bean和配置类、封装注解@EnableStudent以简化导入过程。同时,详细讲解了@Import的四种用法,并展示了如何通过实现ApplicationRunner、CommandLineRunner接口及配置spring.factories文件来执行程序启动时的监听逻辑。
139 2
|
Java Spring 容器
SpringBoot自动装配原理之@Import注解解析
SpringBoot自动装配原理之@Import注解解析
321 0
|
Java Spring 容器
Springboot中的@Import注解~
Springboot中的@Import注解~
220 0
|
Java Spring 容器
Spring Boot - 自动装配中的不可忽视的@Import
Spring Boot - 自动装配中的不可忽视的@Import
217 0
|
Java 容器 Spring
【SpringBoot2 从0开始】底层注解 - @Import
【SpringBoot2 从0开始】底层注解 - @Import
【SpringBoot2 从0开始】底层注解 - @Import
【SpringBoot】@Enable*注解和@Import
【SpringBoot】@Enable*注解和@Import

热门文章

最新文章