Spring Boot入门

创建helloworld入门

maven设置

maven阿里云仓库,编译打包使用jdk1.8

<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>
idea创建maven项目

添加父依赖

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

pom添加springboot依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
创建主程序
/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {

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

编写controller类

/**
 * @Controller、@ResponseBody : 可以合并使用一个注解@RestController
 * @ResponseBody:给浏览器页面返回一个字符串注解
 */
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01() {
        return "Hello world";
    }
}
测试

直接运行main方法
如遇一下错误提示

Error:(3, 38) java: 程序包org.springframework.stereotype不存在
Error:(4, 47) java: 程序包org.springframework.web.bind.annotation不存在
Error:(5, 47) java: 程序包org.springframework.web.bind.annotation不存在
Error:(7, 2) java: 找不到符号
 符号: 类 Controller
Error:(10, 6) java: 找不到符号
 符号:  类 ResponseBody
 位置: 类 com.test.controller.HelloController
Error:(11, 6) java: 找不到符号
 符号:  类 RequestMapping
 位置: 类 com.test.controller.HelloController

idea设置
在这里插入图片描述

配置

修改tomcat启动端口,在resource下新建application.properties配置文件

server.port=20361
部署

设置打成jar包需在pom文件添加相关属性
打成jar包设置,如果中间值是war,打包之后就是war包

    <packaging>jar</packaging>

还需添加build模块,其中finalName为打成包之后的名称,可自定义

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>spring</finalName>
    </build>

依赖管理

父项目做依赖管理
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
    </parent>
    <--!他的父项目-->
      <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.4.5</version>
  </parent>
依赖管理    
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
</parent>
他的父项目
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
  </parent>
几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制
开发导入starter场景启动器
1、见到很多 spring-boot-starter-* : *就某种场景
2、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
3、SpringBoot所有支持的场景
https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
4、见到的  *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。
5、所有场景启动器最底层的依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.3.4.RELEASE</version>
  <scope>compile</scope>
</dependency>

无需关注版本号,自动版本仲裁

1、引入依赖默认都可以不写版本
2、引入非版本仲裁的jar,要写版本号。
可以修改默认版本号
1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
2、在当前项目里面重写配置
    <properties>
        <mysql.version>5.1.43</mysql.version>
    </properties>

自动配置

自动配好tomcat
  • 引入tomcat依赖
  • 配置tomcat
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.4.5</version>
      <scope>compile</scope>
    </dependency>
自动配置springMvc
  • 引入springMvc全套组件
  • 自动配好springMvc常用组件
自动配好web常见功能,如:字符编码问题
  • springBoot帮我们配置好了所有web开发的常见场景
默认的包结构
  • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
  • 无需以前的包扫描配置
  • 可改变扫描路径,@SpringBootApplication(scanBasePackages=“需要扫描的路径”),或者@ComponentScan指定扫描路径
@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
各种配置都有默认值
  • 默认配置最终都是映射到某个类上,如:MultipartProperties
  • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
按需要加载所有自动配置项
  • 各种starter
  • 引入了那些场景这个场景的自动配置才会开启
  • SpringBoot所有的自动配置功能都在spring-boot-autoconfigure包里面

容器功能

组件添加

1、@Configuration
基本使用,Full和Lite模式
配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式

 * 1、配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
 * 2、配置类本身也是组件
 * 3、proxyBeanMethods:代理bean的方法,解决组件依赖问题,默认是true
 *      Full(proxyBeanMethods= true)、【保证每个Bean方法被调用多少次返回的组件都是单实例的】
 *      Lite(proxyBeanMethods= false)【每个Bean方法被调用多少次返回的组件都是新创建的】
 * 4、@Import({User.class, DBHelper.class})
 *      给容器中自动创建出这两个类型的组件,默认组件的名字就是全类名
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods= false) // 告诉springboot这是一个配置类 == 配置文件

2、@ComponentScan、@Import

 * 4、@Import({User.class, DBHelper.class})
 *      给容器中自动创建出这两个类型的组件,默认组件的名字就是全类名
@Import({User.class, DBHelper.class})

3、@Conditional
条件装配,满足Conditional指定的条件,则进行组件注入
在这里插入图片描述

原生配置文件

@ImportResource
可把spring组测组件的配置文件改成springboot方式注入

@ImportResource("classpath:bean.xml")
public class MyConfig {

    @Bean // 给容器中添加组件,以方法名作为组件的id,返回类型就是组件类型,返回的值就是组件在容器中的实列
    public User user01(){
        return new User("小小飞",23);
    }
    @Bean("tom")
    public Pet tomcatPet(){
        return new Pet("小小猫");
    }
}
配置绑定

使用java读取到properties文件中的内容,并且把它封装到JavaBean中

public class getProperties {
     public static void main(String[] args) throws FileNotFoundException, IOException {
         Properties pps = new Properties();
         pps.load(new FileInputStream("a.properties"));
         Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
         while(enum1.hasMoreElements()) {
             String strKey = (String) enum1.nextElement();
             String strValue = pps.getProperty(strKey);
             System.out.println(strKey + "=" + strValue);
             //封装到JavaBean。
         }
     }
 }

springBoot中使用@ConfigruationProperties,mycar为配置文件中的前缀

@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {

可使用@EnableConfigurationProperties + @ConfigurationProperties

@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods= false) // 告诉springboot这是一个配置类 == 配置文件
//@ConditionalOnBean(name = "tom")
@ImportResource("classpath:bean.xml")
@EnableConfigurationProperties(Car.class)
    // 1、开启car配置绑定功能
    // 2、把这个car组件自动注册到容器中
public class MyConfig {

    @Bean // 给容器中添加组件,以方法名作为组件的id,返回类型就是组件类型,返回的值就是组件在容器中的实列
    public User user01(){
        return new User("小小飞",23);
    }
    @Bean("tom")
    public Pet tomcatPet(){
        return new Pet("小小猫");
    }
}
===========================================================================================
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;

    public Car() {
    }

或者@Component + @ConfigurationProperties

@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;   

创建项目步骤

  1. 选择Spring Initialize
    在这里插入图片描述
  2. 输入Group和Artifact
    在这里插入图片描述
  3. 勾选Developer Tools、Web、Template Engines
    在这里插入图片描述在这里插入图片描述
    在这里插入图片描述
  4. 删除多余文件
    在这里插入图片描述
  5. thymeleaf的每个html文件都需要引用命令控件 xmlns:th=“http://www.thymeleaf.org”
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>
  1. 简单的controller访问
    在这里插入图片描述
@Controller
public class HelloController {


	// 配置默认访问登录页面
    @GetMapping(value = {"/","/login"})
    public String getLogin(){
        return "login";
    }

	// 登录页面跳转
    @PostMapping("/login")
    public String login(String name ,String password){
        // 登录
        return "redirect:/main.html";
    }
    @GetMapping("/main.html")
    public String mainPage(){
        // 如果直接访问可直接跳过登录,需要设置登录信息或者拦截器
        return "main";
    }

	// 设置返回的信息给前端页面
    @GetMapping("/msg")
    public String getMsg(Model model){
        model.addAttribute("msg","hhhhh");
        return "success";
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值