SpringBoot开发流程

一、 maven创建SpringBoot项目 

 

注意:  jdk必须1.8

 

Finish 完成创建

 

二、项目应用

1.springboot的项目结构

 

2.springboot的常规开发

 

由于springboot先天性集成了spring/springmvc,所以我们不需要编写相关配置文件,直接开发即可。

三、springboot的核心配置文件

 

Springboot的核心配置文件主要是来修改一些基本配置(服务器端口号、项目访问名字)以及集成配置(数据源、mybatisredis等等)

 

 

 

Springboot的核心配置文件分为两种形式:(注意:名字都必须为application

1.application.properties   

2.application.yml

 

3.自定义springboot配置

 

Springbootapplication.properties/application.yml 文件还可以配置一些自定义属性用来给对象属性赋值方便代码维护以及解耦合

应用场景springMvc文件上传,定义全局文件夹路径。

 

1、在application.yml中添加自定义属性配置

 

方式一:通过@Value(“${upload.imagePath}”)

 

方式二:定义通用映射解析

2、在application.properties中添加自定义属性配置

1.application.properties配置文件中定义自定义属性
com.sam.name=sam
com.sam.age=11
com.sam.desc=magical sam
2、编写Bean类,加载属性

Sam类需要添加@Component注解,让spring在启动的时候扫描到该类,并添加到spring容器中。

第一种:使用spring支持的@Value()加载
package com.sam.demo.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author sam
 * @since 2017/7/15
 */
@Component
public class Sam {

    //获取application.properties的属性
    @Value("${com.sam.name}")
    private String name;

    @Value("${com.sam.age}")
    private int age;

    @Value("${com.sam.desc}")
    private String desc;
    
    //getter & setter
}
第二种:使用@ConfigurationProperties(prefix="") 设置前缀,属性上不需要添加注解。
package com.sam.demo.conf;

import org.springframework.stereotype.Component;

/**
 * @author sam
 * @since 2017/7/15
 */
@Component
@ConfigurationProperties(prefix = "com.sam")
public class Sam {

    private String name;

    private int age;

    private String desc;

    //getter & setter
}

 

3、在controller中注入并使用Sam这个Bean。
package com.sam.demo.controller;

import com.sam.demo.conf.Sam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sam
 * @since 2017/7/14
 */
@RestController
public class IndexController {

    @Autowired
    private Sam sam;

    @RequestMapping("/index")
    public String index() {
        System.out.println(sam.getName() + " " + sam.getAge() + " " + sam.getDesc());
        return "index";
    }

}

 

四、springboot的环境配置

 

在实际开发过程中,不同的开发环境,我们使用的配置信息也是不一样的,这关系到连接池配置、工厂配置等等

例如:

生产环境(线上环境)

开发环境(线下环境)

测试环境(单元测试)

不同的环境我们要指定不同的配置文件,相对springboot给我们提供了这样的便利。

注意:也可以在application.properties总配置文件中引入application-dev.properties(开发)或application-prod.properties(生产)或application-test.properties(测试) 达到动态切换环境的目的

application.properties:

 

五、springboot整合Mybatis

1.导入相关依赖jar

<!--mybatis 依赖-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.10</version>
    </dependency>
    <!-- 分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
    </dependency>

<!--连接池配置-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.31</version>
</dependency>
<!--log4j-->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

 

2.在配置文件中加入连接池以及mybatis的相关配置

3.Demo测试

六、springboot整合Redis

1.导入依赖

2.在application.yml中添加redis配置

3.自定义rediskey/value 序列化配置否则redis中存放的key/value是二进制格式,根据key删除时,找不到指定key这里使用的是RedisTemplate

4.Redis的开发使用

七、springboot集成shiro

 

1.引入相关依赖

 

2.准备shiro的配置文件

1.手动封装拦截策略

2.直接引入xml配置文件

在启动时 在启动类上通过@ImportResource(“classpath:shiro-config.xml”),初始化shiro配置

 

八、springboot整合JSP

 

1.引入相关依赖

2.手动创建webapp 静态资源文件夹

3.在application.yml 中引入视图解析配置

4.启动测试

注意: 如果在访问jsp的过程中 发生下载现象  

  1. 排除jar的依赖问题
  2. jar<scope> 设置为 compile

SpringBoot不推荐使用JSP作为View,而是推荐我们使用模板(如:thymeleaf、freemarker等模板引擎),原因如下:

1. JSP性能较差

2. 绝对的前后端分离思想,JSP并不利于页面调试(运行依赖于web容器)

3. SpringBoot对内嵌web容器的支持默认也是用tomcat。但tomcat对web资源的处理上写死了要使用文件目录,对于打包成jar包的SpringBoot

应用来说,显然不行,也有的人打包成war,然后还是部署到tomcat上,这样违反了SpringBoot的初衷,这样一来,等于否定了嵌入式容

器,而且程序员还要处理嵌入式环境和外部tomcat环境的不同带来的问题。  

 

posted @ 2018-11-13 17:06  我风依旧  阅读(14808)  评论(0编辑  收藏  举报