SpringBoot2学习笔记——Maven配置、HelloWorld、依赖管理、自动配置

1、Maven 配置

  1. 首先检查 java 版本和 Maven 版本,springboot2 要求 java 8+、Maven 3.3+。
    在这里插入图片描述
  2. 在 Maven 的 setting.xml 文件中配置阿里镜像和 jdk 版本,使 Maven 每次下载依赖时都在阿里仓库下载,并且使用配置的 jdk。
<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-14</id>
		<activation>
			<activeByDefault>true</activeByDefault>
			<jdk>14</jdk>
		</activation>
        <properties>
          <maven.compiler.source>14</maven.compiler.source>
          <maven.compiler.target>14</maven.compiler.target>
          <maven.compiler.compilerVersion>14</maven.compiler.compilerVersion>
        </properties>
   </profile>
</profiles>

2、HelloWorld

  1. 创建 Maven 工程,这里暂时创建 Maven java 工程。
    在这里插入图片描述
  2. 在 pom.xml 中引入依赖。
<!--继承springboo2父工程后,才能使用springboot2-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.3</version>
</parent>

<dependencies>
    <!--引入web项目所需要的全部依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
  1. 创建主程序,主程序是项目的入口。
    使用注解@SpringBootApplication声明主程序类。
    ConfigurableApplicationContext:IOC容器类,可以查询和获取容器内的所有对象

MainApplication.java

package com.mcc.springboot;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;

/**
 * 主程序类,主配置类
 * 使用注解:@SpringBootApplication,声明主类
 */
@SpringBootApplication//声明该类为主类
public class MainApplication {
    public static void main(String[] args) {
    	//ConfigurableApplicationContext:IOC容器类,可以查询和获取容器内的所有对象
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
    }
}
  1. 编写业务逻辑。
    springboot 中,对业务逻辑的编写与 SSM 框架中编写的方式相同,不需要做任何修改。

HelloController.java

package com.mcc.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

// @ResponseBody
// @Controller
@RestController//该注解是@Controller加@ResponseBody
public class HelloControllor {
    @RequestMapping("/hello")
    public String hello(){
        return "hello, springboot2!";
    }
}

注意:
(1)响应字符串给浏览器,而不是跳转到视图页面,此时需要添加注解@ResponseBody,该注解加载某个方法上,表示该方法返回字符串,加到类上,表示该类内所有方法都返回字符串。
(2)@RestController = @Controller + @ResponseBody

  1. 测试。
    在 springboot 中,直接运行 main 方法,即可启动项目,进行测试。

在这里插入图片描述

  1. 简化配置。
    在资源文件夹中,创建application.properties文件,在该文件中进行各种属性的配置。
    注意:(1)文件名固定,不能改变。(2)可以配置的属性点击链接查看springboot 2.4.3 Application properties

在这里插入图片描述
application.properties

server.servlet.context-path=/helloworld
server.port=8088
  • server.servlet.context-path:配置工程的访问路径。假设 @RequestMapping("/hello"),如果不配置该属性,则访问路径为:http://localhost:8080/hello,若配置该属性,则访问路径为:http://localhost:8080/配置的路径/hello
    springboot 内置的 tomcat,默认情况该属性是没有被配置的,因此在访问 /hello 时,不需要添加工程路径,而我们在编写 springMVC 项目时,由于在 idea 中整合了 tomcat,因此需要添加配置的路径。

在这里插入图片描述

  1. 简化部署。
    通过在 pom.xml 中配置插件,将工程不打 war 包,而是打成一个可执行的 jar 包,之后可以直接在目标服务器上运行。
<build>
    <plugins>
        <!--简化部署的插件:将工程打成可执行的jar包-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

在这里插入图片描述
打包完成之后,在文件夹中找到打包好的 jar 包。
在这里插入图片描述

在这里插入图片描述
之后在命令行中,进入到 jar 包所在目录,执行命令:java -jar helloworld-1.0-SNAPSHOT.jar
在这里插入图片描述
项目就会运行起来,在浏览器中就可以访问了。

3、依赖管理

  1. 父工程
    springboot2 工程要引入父工程:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
    </parent>
    

    父工程的父工程:

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.4.3</version>
    </parent>
    

    在 spring-boot-dependencies 中,几乎声明了所有与开发相关的 jar 包,因此,在我们自己的项目中,使用 dependencies 中的依赖时,不需要加版本号。但要是需要使用 dependencies 中没有的依赖时,要自己声明版本号。

    修改默认版本号:
    当我们需要修改依赖的默认版本号时,
    (1)查看 spring-boot-dependencies 中使用的${keyName}
    (2)在工程的 pom.xml 中的 properties 标签内,声明一个标签,标签名为 keyName,标签值为新的版本号即可。
    在这里插入图片描述

  2. 场景启动器

    以 spring-boot-starter-xxx 命名的依赖是 xxx场景启动器。引入某个场景启动器后,由于 Maven 的依赖传递特性,会自动将与该场景有关的全部 jar 包,都引入到项目中。

    如开发 web 应用时,导入 web 场景启动器:

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

    所有的场景启动器都引入依赖:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.4.3</version>
      <scope>compile</scope>
    </dependency>
    

4、自动配置

springboot 已经自动配置好了 tomcat、spring、springMVC 相关内容。

  1. 默认扫描的包结构:
    springboot 默认扫描主程序所在包,以及主程序所在包的所有子包中的所有组件。

  2. 改变扫描的包:
    (1)当需要改变扫描包的范围时,可以通过@SpringBootApplication(scanBasePackages = "包名")实现。

@SpringBootApplication(scanBasePackages = "com.mcc.springboot")//声明该类为主类
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

(2)@SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan("包名"),因此,也可以这样实现:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.mcc.springboot")
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值