Spring boot笔记(二)

一、核心注解

1、@SpringBootApplication:Spring Boot应用标注在某个类上,说明是主配置类,运行其main方法启动Spring Boot

2、@SpringBootConfiguration:Spring Boot的配置类,标注在某个类上,表示这是一个Spring Boot的配置类

3、@Configuration:配置类上来标注这个注解

4、@EnableAutoConfiguration:开启自动配置功能

5、@EnableScheduling:开启定时(注解在启动类)

6、@Scheduled(cron="0 0/10 * * * ?") :定时方法注解

7、@ComponentScan(basePackages = {""}):springboot 扫描路径

 

二、配置文件

1、全局配置使用application.properties或application.yml

2、自定义属于配置

  person.properties:

person.lastName=hello
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;

@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;prefix = "person":配置文件中哪个下面的所有属性进行一一映射
@Component:将JavaBean加入容器组件
@PropertySource:加载指定的配置文件;
@Validated:注入值数据校验
另外可用@Value("${person.lastName}")单独取值

3、多Profile文件

我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml,默认使用application.properties的配置;
激活指定profile:
a、在配置文件中指定 spring.profiles.active=dev
b、命令行:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
可以直接在测试的时候,配置传入命令行参数
命令行还可以使用外部配置:--spring.config.location=G:/application.properties
c、虚拟机参数;
-Dspring.profiles.active=dev

三、日志框架

1、常用日志组合(SpringBoot选用 SLF4j和logback)

2、SLF4j使用

开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法。给系统里面导入slf4j的jar和 具体的实现jar。SLF4j日志适配图:

3、系统中所有的日志都统一到slf4j

a、将系统中其他日志框架先排除出去;
b、用中间包来替换原有的日志框架;
c、我们导入slf4j其他的实现
 

4、spring boot日志配置

logback.xml:直接就被日志框架识别了;
logback-spring.xml:日志框架就不直接加载日志的配置项,由SpringBoot解析日志配置,可以使用SpringBoot
的高级Profile功能

四、Thymeleaf

1、使用
a.引用依赖spring‐boot‐starter‐thymeleaf,HTML页面放在classpath:/templates/,thymeleaf就能自动渲染。
b.导入thymeleaf的名称空间
c.使用thymeleaf语法

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF‐8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<!‐‐th:text 将div里面的文本内容设置为 ‐‐>
<div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>

2、语法规则

a. th:任意html属性;来替换原生属性的值


 

b.表达式

${...}:获取变量值;OGNL;使用内置的基本对象;内置的一些工具对象
*{...}:选择表达式:和${}在功能上是一样;
#{...}:获取国际化内容
@{...}:定义URL
~{...}:片段引用表达式

3、thymeleaf公共页面元素抽取

1、抽取公共片段
<div th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</div>
2、引入公共片段
<div th:insert="~{footer :: copy}"></div>
~{templatename::selector}:模板名::选择器
~{templatename::fragmentname}:模板名::片段名
3、默认效果:
insert的公共片段在div标签中
如果使用th:insert等属性进行引入,可以不用写~{}:
行内写法可以加上:[[~{}]];[(~{})];

三种引入公共片段的th属性:
th:insert:将公共片段整个插入到声明引入的元素中
th:replace:将声明引入的元素替换为公共片段
th:include:将被引入的片段的内容包含进这个标签中

<footer th:fragment="copy">
&copy; 2011 The Good Thymes Virtual Grocery
</footer>
引入方式
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
效果
<div>
<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>
<div>
&copy; 2011 The Good Thymes Virtual Grocery
</div>

 

五、拦截器

1、编写拦截器,实现HandlerInterceptor

/**
* 登陆检查,
*/
public class LoginHandlerInterceptor implements HandlerInterceptor {
    //目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
    Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");
        if(user == null){
            //未登陆,返回登陆页面
            request.setAttribute("msg","没有权限请先登陆");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else{
            //已登陆,放行请求
            return true;
        }
    } 
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object
handler, ModelAndView modelAndView) throws Exception {
} 
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
}
}

 2、注册拦截器

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
//静态资源; *.css , *.js
//SpringBoot已经做好了静态资源映射
registry.addInterceptor(new
LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login");
}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值