注解
@Component :标准一个普通的spring Bean类。
@Repository:标注一个DAO组件类。
@Service:标注一个业务逻辑组件类。
@Controller:标注一个控制器组件类。
这些注解功能相同,用来标注不同的组件
@RequestMaping(“hello”) 写类上边
@GetMaping(“hello1”) 写类的方法上边
访问时用localhost:8080/hello/hello1访问
@Restcontroller只相当于返回return后的字符串
@Controller是页面跳转
实例
@RestController //@Restcontroller只相当于返回return后的字符串,@Controller是页面跳转
@RequestMapping("hello") //上边的大hello
public class hello {
public String world(){
return "Hello Word";
}
@GetMapping("hello1") //hello下的hello1
public String world1(){
return "Hello Word1";
}
}
@ConfigurationProperties(prefix = “student”)注解来绑定yml里对象的值
@Component
@ConfigurationProperties(prefix = "student")
public class Person {
private String name;
private int age;
......
}
@Autowired
@Validated //数据校验
@Validated //数据校验
public class Person {
@Email() //name属性必须为email格式
private String name;
......
}
图片来源: bilibili狂神说Java
配置文件
yml和properties语法
springboot官方推荐使用yml语法
yml和yaml区别: yml的处理器是双核双线程,跑分是两万,而yaml的处理器是四核四线程,跑分是两万五,区别在于处理器运算速度不一样
无论是yml和properties配置名必须得是application
application.properties语法 key = value
application.yml语法 key:<空格>value
yml实例
#yaml
#对空格的要求非常严格
#设置默认端口
server:
port: 8080
#键值对
name: lizheng
#对象1
student1:
name: lizheng
age: 18
gender: boy
#对象2 (花括号代表对象)
student2: {name: lizheng,age: 18,gender: boy}
#数组
list:
- li
- zhang
- wang
- liu
#数组2 (中括号代表数组)
list2: [li,zhang,wang,liu]
# ---------------------------------------------------------
#给类赋值
#通过@ConfigurationProperties(prefix = "student")注解来绑定
student:
name: liz${random.int} #Spring EL 随机数
age: 18 #随机用户名${random.uuid}
happy: true
list:
- wangweicheng
- ranyulin
- love
birth: 2002/01/01
maps: {k1: v1,k2: v2}
dog:
name: ahuang
age: 3
# ---------------------------------------------------------
# 2022/05/31 11:38
# 明天见!
配置文件优先级
1.file: ./config/
2.file: ./
3.classpath:/config/
4.classpath:/ (默认路径)
同一路径下的优先级
properties>ymal>yml
生产环境
properties
application.properties(默认环境)
properties-dev.properties(开发环境)
properties-test.properties(测试环境)
要使用哪个环境只需要在默认环境中写
spring.profiles.active = test //使用测试环境
yml
中间的分割线- - -就相当于分隔单个文件。只需要在一个文件中就能完成
---
server:
port: 8080
spring:
profiles:
active: dev #声明使用dev生产环境
---
#配置dev生产环境
server:
port: 8081
spring:
config:
activate:
on-profile: dev #声明dev生产环境
---
#配置test生产环境
erver:
port: 8082
spring:
config:
activate:
on-profile: test #声明test生产环境
debug=true
debug: true #通过debug=true查看自动配置类哪些生效了哪些没生效
Web
访问优先级resources>static>public
创建index.html放到上边的任意文件中,就可以实现定制首页
thymeleaf模板
<html lang="en" xmlns:th="http://www.thymeleaf.org">
在pom中引入thymeleaf依赖
<!--引入thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
将html页面a.html放在templates中
@Controller
public class HelloController {
@RequestMapping("/hello")
public String abc(){
return "a";
}
}
通过thymeleaf实现首页
@Controller
public class HelloController {
@RequestMapping({"/", "/index"}) //进入默认页面或者访问index都是首页内容
public String abc(){
//model.addAttribute("msg","liz"); //通过thymeleaf传值 html页写<div th:text="${msg}"></div>
return "index";
}
}
国际化
在resources目录下创建i18n目录,在其中创建
login.properties(默认)
login_zh_CN.properties(中文)
login_en_US.properties(英文)
…
在IDEA设置中添加Resource Bundle Editor 插件,选择资源包,然后就可以在一个页面中配置三个文件
index.html
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
MyMvcConfig类中
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//实现首页
@Override
public void addViewControllers(ViewControllerRegistry registry) {
WebMvcConfigurer.super.addViewControllers(registry);
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
//自定义国际化组件
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
MyLocaleReslove类中
public class MyLocaleResolver implements LocaleResolver {
//请求解析
@Override
//获取语言中的请求参数
public Locale resolveLocale(HttpServletRequest request) {
String lang = request.getParameter("l");
Locale locale = Locale.getDefault(); //如果选择没有就是用默认的
if(!StringUtils.isEmpty(lang)){
//zh_CN
String split[] = lang.split("_");
//国家,地区
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}