栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot

1 springboot的引言

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的 初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不 再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应 用开发领域(rapid application development)成为领导者。>
springboot(微框架) = springmvc(控制器) + spring(项目管理)

2 springboot的特点
  1. 创建独立的Spring应用程序。
  2. 嵌入的Tomcat,无需部署WAR文件。
  3. 简化Maven配置。
  4. 自动配置Spring。
  5. 没有XML配置。
    总结,如图所示:
3 第一个SpringBoot程序

springboot项目约定,如图所示:
1.打开IDEA如图所示的界面,点击Create New Project。
2.选择Empty Java,点击Next。如图所示:
3.填写项目名称,点击Finish。如图所示:
4.选择项目右键点击File–>New–Module,如图所示:
5.选择Maven,点击Next。如图所示:
6.填写项目名称,点击Next。如图所示:
7.点击Finish即可,如图所示:
8.引入相关依赖的代码如下:



  4.0.0
  com.txw
  springboot_01
  1.0-SNAPSHOT
  war
  
    UTF-8
    1.8
    1.8
  
  
  
    org.springframework.boot
    spring-boot-starter-parent
    2.2.5.RELEASE
  
  
    
    
      org.springframework.boot
      spring-boot-starter-web
    
  
  
    springboot_01
  

如图所示:
9.在resources目录创建application.yml的代码如下:

server:
  port: 8080

如图所示:

10.编写SpringBoot01Application的代码如下:

package com.txw;

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

@SuppressWarnings("all")   // 注解警告信息
@SpringBootApplication   // 标识这是一个springboot全局入口类
public class SpringBoot01Application {
    public static void main(String[] args) {
        
        SpringApplication.run(SpringBoot01Application.class,args);
    }
}

如图所示:
运行如图所示:说明启动成功!
11.编写HelloController的代码如下:

package com.txw.controller;

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

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        System.out.println("======hello world=======");
        return "hello";
    }
}

如图所示:
重新运行主启动类,如图所示:

通过浏览器访问:http://localhost:8080/hello/hello,如图所示:
控制台打印结果如图所示:
总结,如图所示:
springboot入口类注解&自定义banner&配置文件拆分,如图所示:

4 springboot中管理对象创建 4.1 管理单个对象

在springboot中可以管理自定义的简单组件对象的创建可以直接使用注解形式创建。

使用 @Repository @Service @Controller 以及@Component管理不同简单对象,比如: 比如要通过工厂创建自定义User对象。
1.编写User的代码如下:

package com.txw.entity;

import org.springframework.stereotype.Component;

@Component("user")
@SuppressWarnings("all")   // 注解警告信息
public class User {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

如图所示:
通过工厂创建之后可以在使用处任意注入该对象,比如:在控制器中使用自定义简单对象创建。
2.编写HelloController的代码如下:

package com.txw.controller;

import com.txw.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SuppressWarnings("all")   // 注解警告信息
@Controller
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private User user;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        System.out.println("======hello world=======");
        System.out.println(user);
        return "hello";
    }
}

如图所示:

4.2 管理多个对象

在springboot中如果要管理复杂对象必须使用@Configuration + @Bean注解进行管理。

1.管理复杂对象的创建的代码如下:

package com.txw.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Calendar;

@Configuration   // (推荐)|@Component(不推荐)
public class Beans {
    @Bean
    public Calendar getCalendar(){
        return Calendar.getInstance();
    }
}

如图所示:
2.使用复杂对象的代码如下:

package com.txw.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Calendar;

@SuppressWarnings("all")   // 注解警告信息
@Controller
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private Calendar calendar;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        System.out.println("======hello world=======");
        System.out.println(calendar);
        return "hello";
    }
}

如图所示:
总结,如图所示:

5. springboot中集成jsp展示
  1. 引入jsp的集成jar包的代码如下:
 
    
      org.apache.tomcat.embed
      tomcat-embed-jasper
    

如图所示:
3. 配置视图解析器的代码如下:

server:
  port: 8080
spring:
  mvc:
    view:
      prefix: / # 视图解析器的前缀
      suffix: .jsp  # 视图解析器的后置

如图所示:
4.编写HelloController的代码如下:

package com.txw.controller;

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

@Controller
@RequestMapping("/hello")
public class HelloController {


    @RequestMapping("index")
    @ResponseBody
    public String index(){
        System.out.println("======index controller=======");
        return "index";
    }
}

如图所示:
5. 引入jsp运行插件的代码如下:

 
    
      
        org.springframework.boot
        spring-boot-maven-plugin
        
          -Dfile.encoding=UTF-8
        
      
    

如图所示:
运行流程,如图所示:
通过浏览器访问:http://localhost:8080/hello/index,如图所示:
控制台打印结果,如图所示:
如果报错误,解决办法,把如图所示的删除之后,重新加载即可!

总结,如图所示:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号