嘿嘿,我的SpringBoot入门教程。随便写写。
pom.xml文件
1 | 一个简单的项目所使用的这个依赖 |
默认配置文件 application.properties
1 | 随便写两个配置 |
默认的Resouces目录
存放着 一些项目的配置文件application.properties、静态文件夹static, 模板文件夹templates
1
2
3
4
5
6
7
8
9 可能有童鞋很好奇这些文件夹都是springboot在哪儿默认配置的?
双击Shift 全局搜索ResourceProperties.class文件
对! springboot的默认配置(autoconfig)就是在这个类中配置的
# org/springframework/boot/autoconfigure/web/ResourceProperties.class
在这里指定了路径文件夹
private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
默认的启动类SpringApplication.class
1 | 该类中主要的注解 |
helloworld
1 | 定义一个controller |
模板的访问
这里使用的模板引擎是 freemarker 当然了也可以使用其他的
springboot默认的是thymeleaf模板引擎
1 | 1. 添加依赖 |
全局异常的捕获
项目如果没有对异常处理的话, 服务器就会宕机。这是万万不允许的。
所以 需要一个全局异常的捕获。才能保证项目的正常运行。
1 | 如: 我们进行 1/0 操作, 服务器就会挂了。 |
集成JPA
个人看法: JPA主张NoSql 确实方便。 但是初学者 会有很多坑。
必须确保所有的命名都正确。程序才能运行。(我刚用JPA时, 也踩了好多坑)
反正我是不喜欢用 JPA, 我还是喜欢Mybatis这种半自动化的框架(自己定制sql语句)
1 | 1. 添加数据库和jpa的依赖 |
过滤器的使用
方式一 注解式1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package com.dottie.boot.Filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* 使用注解的方式来使用过滤器
* 注意这种方式要在启动类中加上 @ServletComponentScan
*
*
* 这里直接用@WebFilter就可以进行配置,同样,可以设置url匹配模式,过滤器名称等。
* 这里需要注意一点的是@WebFilter这个注解是Servlet3.0的规范,并不是Spring boot提供的。
* 除了这个注解以外,我们还需在配置类中加另外一个注解:@ServletComponetScan,指定扫描的包。
*/
"AnnoFilter", urlPatterns = {"/hello", "/index"}) (filterName =
public class AnnoFilter implements Filter{
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("注解方式使用过滤器。。。");
}
public void destroy() {
}
}
方式二:@Configuration手动配置(推荐使用)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
621. 先写一个过滤器Filter
package com.dottie.boot.Filter;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 测试 filter
* 请求操作耗时例子
* 过滤器,主要的用途是过滤字符编码、做一些业务逻辑判断等。
*/
4j
public class LogCostFitler implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
long start = System.currentTimeMillis();
filterChain.doFilter(httpServletRequest, httpServletResponse);
log.info("操作耗时: " + (System.currentTimeMillis() - start) + " 毫秒");
}
public void destroy() {
}
}
2. 新建一个配置类
package com.dottie.boot.Configuration;
import com.dottie.boot.Filter.LogCostFitler;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
public class FilterConfig {
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
LogCostFitler logCostFitler = new LogCostFitler();
filterRegistrationBean.setFilter(logCostFitler);
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.setName("LogCostFilter");
filterRegistrationBean.setOrder(1);
return filterRegistrationBean;
}
}
即可
拦截器的使用
1 | 1. 新建一个拦截器 |
监听器的使用
1 | package com.dottie.boot.Listener; |
区别和用途
1 | 区别: |
生成jar后台运行
java -jar app.jar(一般测试用这种方式) 缺点当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,此时程序也会停止运行。
java -jar app.jar &
&代表在后台运行。
特定:当前ssh窗口不被锁定,但是当窗口关闭时,程序中止运行nohup命令 nohup java -jar app.jar &
nohup 意思是不挂断运行命令,当账户退出或终端关闭时,程序仍然运行。nohup java -jar app.jar >output 2>&1 &
默认输出到nohup.out文件
jobs命令 查看后台运行任务