SpringBoot使用笔记

本文介绍如何使用SpringBoot搭建简单的Web应用程序,包括项目搭建、配置、启动及多环境配置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                        SpringBoot使用笔记

其实也是参考官方的:http://spring.io/guides/gs/rest-service/ ,在官方代码基础上加入了很多实用的东西,比如运行环境启动命令等等。

官方文档:http://docs.spring.io/spring-boot/docs/current/reference/html/


SpringBoot并不神秘,其最大的好处是可以帮你省略引用一堆jar包,需要神秘jar它自动帮你引用,集成tomcat,集成配置等待好处太多,总之就是更方便开发而已

还是自己体验下比较好。


1.建立java应用程序

起一个Maven的java应用程序,注意不要再起Web应用程序了:



2.maven配置文件

pom.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.xxx</groupId>  
  5.     <artifactId>spring-boot-hello</artifactId>  
  6.     <version>1.0.0</version>  
  7.     <packaging>jar</packaging>  
  8.     <parent>  
  9.         <groupId>org.springframework.boot</groupId>  
  10.         <artifactId>spring-boot-starter-parent</artifactId>  
  11.         <version>1.4.3.RELEASE</version>  
  12.     </parent>  
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.         <maven.compiler.source>1.8</maven.compiler.source>  
  16.         <maven.compiler.target>1.8</maven.compiler.target>  
  17.     </properties>  
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>org.springframework.boot</groupId>  
  21.             <artifactId>spring-boot-starter-web</artifactId>  
  22.         </dependency>  
  23.         <dependency>  
  24.             <groupId>org.springframework.boot</groupId>  
  25.             <artifactId>spring-boot-starter-test</artifactId>  
  26.             <scope>test</scope>  
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>com.jayway.jsonpath</groupId>  
  30.             <artifactId>json-path</artifactId>  
  31.             <scope>test</scope>  
  32.         </dependency>  
  33.     </dependencies>  
  34.     <build>  
  35.         <plugins>  
  36.             <plugin>  
  37.                 <groupId>org.springframework.boot</groupId>  
  38.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  39.             </plugin>  
  40.         </plugins>  
  41.     </build>  
  42. </project>  

3.新建类文件

[java]  view plain  copy
  1. import org.springframework.boot.SpringApplication;  
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  3.   
  4. @SpringBootApplication  
  5. public class Application {  
  6.   
  7.     public static void main(String[] args) {  
  8.         SpringApplication.run(Application.class, args);  
  9.     }  
  10. }  

[html]  view plain  copy
  1. public class Greeting {  
  2.   
  3.     private final long id;  
  4.     private final String content;  
  5.   
  6.     public Greeting(long id, String content) {  
  7.         this.id = id;  
  8.         this.content = content;  
  9.     }  
  10.   
  11.     public long getId() {  
  12.         return id;  
  13.     }  
  14.   
  15.     public String getContent() {  
  16.         return content;  
  17.     }  
  18. }  

[html]  view plain  copy
  1. import java.util.concurrent.atomic.AtomicLong;  
  2. import org.springframework.web.bind.annotation.RequestMapping;  
  3. import org.springframework.web.bind.annotation.RequestParam;  
  4. import org.springframework.web.bind.annotation.RestController;  
  5.   
  6. @RestController  
  7. public class GreetingController {  
  8.   
  9.     private static final String template = "Hello, %s!";  
  10.     private final AtomicLong counter = new AtomicLong();  
  11.   
  12.     @RequestMapping("/greeting")  
  13.     public Greeting greeting(@RequestParam(value="name"defaultValue="World") String name) {  
  14.         return new Greeting(counter.incrementAndGet(),  
  15.                             String.format(template, name));  
  16.     }  
  17. }  

4.运行项目

注意:SpringBoot已经集成了tomcat所以无需再安装了,默认是8080不要和现有的tomcat冲突


成功启动会提示:FrameworkServlet 'dispatcherServlet': initialization completed in 10 ms



5.验证

http://localhost:8080/greeting


6.集成依赖文件

我们去看下系统依赖文件,发现springmvc都自动被引入,如果不适用springboot就需要一个个去单独引用。



7.命令行下启动

实际项目中肯定是到命令行下启动

Windows下

# java -jar spring-boot-hello-1.0.0.jar



Linux下


实际是需要再后台运行的加个&符号,比如这样

nohup java -jar spring-boot-hello-1.0.0.jar >/dev/null 2>&1 &


定制化JVM启动参数

nohup java -Xms1024m -Xmx1024m -Xss1024K -XX:PermSize=64m -XX:MaxPermSize=128m -jar spring-boot-hello-1.0.0.jar >/dev/null 2>&1 &


8.Tomcat下运行

其实完全没必要再到tomcat下再去运行了,如果真的还需要这样可以打war包即可。

[html]  view plain  copy
  1. <artifactId>spring-boot-hello</artifactId>  
  2.     <version>1.0.0</version>  
  3.     <packaging>jar</packaging>  

改为

[html]  view plain  copy
  1. <artifactId>spring-boot-hello</artifactId>  
  2.     <version>1.0.0</version>  
  3.     <packaging>war</packaging>  

需要注意的是jdk需要1.8以上版本,Tomcat需要8.0以上版本


9.Spring Boot配置

如果你需要修改配置,可以在resources文件夹下创建一个application.properties或者application.yml文件,这个文件会被发布到classpath。


比如修改默认的tomcat端口为80

application.properties

[html]  view plain  copy
  1. server.port: 80  
  2. server.tomcat.uri-encoding: UTF-8  


application.yml
[html]  view plain  copy
  1. server:  
  2.   port: 80  
  3.   tomcat:  
  4.     uri-encoding: UTF-8  


再重新运行tomcat就是80端口了:


更多配置参考:http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html


另外:推荐一本书《深入实践Spring Boot》


10.spring boot多环境配置

一般会有开发环境,测试环境,正式环境,甚至于预发布环境。

配置文件不管理好,如果这个靠手工去做非常不靠谱,很容易出错。

Spring boot可以配置多个配置文件,自动切换。


application-online.properties

[html]  view plain  copy
  1. server.port: 80  
  2. server.tomcat.uri-encoding: UTF-8  

application-test.properties

[html]  view plain  copy
  1. server.port: 8080  
  2. server.tomcat.uri-encoding: UTF-8  

application.properties里激活需要的环境即可

[html]  view plain  copy
  1. spring.profiles.active=test  


也可以通过命令行方式启动

将程序打包之后 通过 java -jar xx.jar --spring.profiles.active=dev的方式启动

参考:  SpringBoot学习笔记(2) Spring Boot的一些配置


整理自尚硅谷视频教程springboot高级篇,并增加部分springboot2.x的内容 一、Spring Boot与缓存 一、JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。 • CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可 以在运行 期访问多个CachingProvider。 • CacheManager定义了创建、配置、获取、管理和控制多个唯一命名 的Cache,这些Cache 存在于CacheManager的上下文中。一个CacheManager仅被一个 CachingProvider所拥有。 • Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个 Cache仅被一个 CacheManager所拥有。 • Entry是一个存储在Cache中的key-value对。 • Expiry 每一 个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期 的状态。一旦过期,条 目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。 二、Spring缓存抽象 Spring从3.1开始定义了org.springframework.cache.Cache 和 org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR- 107)注解简化我们开发; • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合; • Cache接 口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache 等; • 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否 已经被调用 过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法 并缓存结果后返回给用户。下 次调用直接从缓存中获取。 • 使用Spring缓存抽象时我们需要关注以下两点; 1、确定方法需要被缓存 以及他们的缓存策略 2、从缓存中读取之前缓存存储的数据 Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、 ConcurrentMapCache等 CacheManager 缓存管理器,管理各种缓存(Cache)组件 @Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @CacheEvict 清空缓存 @CachePut 保证方法被调用,又希望结果被缓存。 @EnableCaching 开启基于注解的缓存 keyGenerator 缓存数据时key生成策略 serialize 缓存数据时value序列化策略 @CacheConfig 抽取缓存的公共配置 三、几个重要概念&缓存注解 1、常用注解 2、常用参数 名字 位置 描述 示例 methodName root object 当前被调用的方法名 #root.methodName method root object 当前被调用的方法 #root.method.name target root object 当前被调用的目标对象 #root.target targetClass root object 当前被调用的目标对象类 #root.targetClass args root object 当前被调用的方法的参数列表 #root.args[0] 3、常用参数SPEL说明 名字 位置 描述 示例 caches root object 当前方法调用使用的缓存列表(如 @Cacheable(value= {"cache1","cache2"}) ), 则有两 个cache #root.caches[0].name argument name evaluation context 方法参数的名字. 可以直接 #参数 名 ,也可以使用 #p0或#a0 的形 式,0代表参数的索引; #iban 、 #a0 、 #p0 result evaluation context 方法执行后的返回值(仅当方法执 行之后的判断有效,如‘unless’ , ’cache put’的表达式 ’cache evict’的表达式 beforeInvocation=false ) #result 四、代码中使用缓存 1、搭建基本环境 1、导入数据库文件 创建出department和employee表 2、创建javaBean封装数据 3、整合MyBatis操作数据库 1.配置数据源信息 2.使用注解版的MyBatis; 1)、@MapperScan指定需要扫描的mapper接口所在的包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值