
本文共 41389 字,大约阅读时间需要 137 分钟。
SpringBoot基础
核心思想---自动装配---约定大于配置
开发环境:jdk1.8、maven、springboot、idea
一、快速构建一个springboot项目
1.1、进入springboot官网
1.2、选择配置并下载
1.3、项目的导入
二、自动装配原理
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.2 com.example springboot 0.0.1-SNAPSHOT springboot Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter 2.4.2 javax.validation validation-api 2.0.1.Final org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-configuration-processor true org.apache.maven.plugins maven-surefire-plugin 2.22.1 true org.springframework.boot spring-boot-maven-plugin 2.2.6.RELEASE repackage
2.1、怎么进行自动装配?
spring boot的自动装配:
1、spring boot启动时会加载大量的自动配置类
2、在开发中看需要实现的功能是否存在springboot写好的配置类,如果没有就需要手动配置
3、在yml中修改springboot属性值时(也就是配置),当它提示时,也就相当于你在修改springboot配置好的配置类中的属性
4、springboot配置好的配置类可以在spring.factories中进行查找出来,它会有一个xxxAutoConfiguration(自动配置类:给容器中添加组件),它上面的xxxProperties配置的类属性就是yml中可以进行修改的属性,以此来达到想要配置的功能!!!
结论:主启动器@SpringBootApplication通过扫描依赖中导入的Spring-boot-autoconfigure下的jar包下的META-INF下的spring.factories中的配置进行装配,但是不一定生效,如果没有相应的启动器,就不会生效,需要导入相应的启动器才能自动装配,springboot的自动装配的东西(以前需要自己写的包或者配置文件xml。。。)现在只需要在sprint-boot-autoconfigure-xxx.xx.RELEASE.jar包下都存在,不需要自己配置,只需要调用即可。。
三、yml写法
yml对空格的要求严格
#对象存储Dog: name: "lucky" age: "7"Person: name: "阿辉" age : 22 happy: true birth: 2021/2/23 maps: {k1: v1, k2: v2} lists: [code,basketball,girl] dog: name: Lucky age: 22
通过@Component和@ConfigurationProperties(prefix = "dog")将实体类的属性与yml中的配置进行绑定
ConfigurationProperties(prefix = "dog")必须导入spring-boot-configuration-processor依赖还需要使用prefix进行绑定!
package com.example.springboot.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Data@NoArgsConstructor@AllArgsConstructor@Component@ConfigurationProperties(prefix = "dog")public class Dog { private String name; private Integer age; @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; }}
package com.example.springboot.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;import java.util.Date;import java.util.List;import java.util.Map;@Data@NoArgsConstructor@AllArgsConstructor@Component@ConfigurationProperties(prefix = "person")@Validated //数据校验public class Person { private String name; private Integer age; private Boolean happy; private Date birth; private Mapmaps; private List
通过@Autowired的自动装配原理将实体类中的数据进行输出
package com.example.springboot;import com.example.springboot.pojo.Dog;import com.example.springboot.pojo.Person;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestclass SpringbootApplicationTests { @Autowired private Dog dog; @Autowired private Person person; @Test void contextLoads() { System.out.println(dog); System.out.println("--------------"); System.out.println(person); }}
四、多环境切换测试
application.properties 环境切换(需要三个环境,application.properties、application-test.properties、application-dev.properties)
server.port=8080# SpringBoot的多环境配置:自主选择激活配合文件(test/dev)spring.profiles.active=dev
application.yml方式实现多环境的切换
# 选择激活哪个环境(test/dev) 各个配置之间拿---分隔spring: profiles: active: test---spring: profiles: testserver: port: 8082---spring: profiles: devserver: port: 8089
五、静态资源
1、在springboot中,可以使用以下方式处理静态资源:
webjars:
localhost:8080/webjars/
public、static、/**、resources 可以使用:``localhost:8080/文件名.后缀名
2、优先级(如果文件相同则)
resources > static(默认)>public
六、扩展mvc
package com.example.springboot.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.View;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.util.Locale;////@EnableWebMvc就是添加了@Import({DelegatingWebMvcConfiguration.class})这个类,意思就是从容器中获取所有的webmvcconfig// 分析:WebMvcAutoConfig类有个@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})// 如果有这个类,那么MyConfig就不会自动装配,// 又因为上面DelegatingWebMvcConfiguration这个类继承了WebMvcConfigurationSupport// 所以不能加@EnableWebMvc这个注解,不然就会失去自动装配的功能!// 扩展SpringMVC@Configurationpublic class MyConfig implements WebMvcConfigurer { // ViewResolver 实现了视图解析器接口的类 就相当于是个视图解析器 // 如果想要自定义的功能,只需要把它注入到bean中,交由Springboot,Springboot会帮我们自动装配! @Bean // 自定义功能!!! public ViewResolver myViewResolver(){ return new MyViewResolver(); } public static class MyViewResolver implements ViewResolver{ @Override public View resolveViewName(String s, Locale locale) throws Exception { return null; } } // 视图跳转 @Override public void addViewControllers(ViewControllerRegistry registry) { // addViewController("/springboot")相当于用localhost:8080/springboot这个跳转到localhost:8080/test // 相当于更名!! registry.addViewController("/springboot").setViewName("test"); }}
七、thymeleaf(模板引擎)的应用
7.1、 作用域的导入
7.2、thymeleaf的th的应用
(格式:th: href/ img / src="@{xxx}**")
Miver
八、整合Druid(阿里巴巴的数据库连接池)
8.1、更改yml中数据库连接type为Druid
spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/curry?usrUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource thymeleaf: cache: false #关闭缓存 mode: HTML5 #设置模板类型 encoding: utf-8 #设置编码# 打印自动生成的sql语句mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
8.2、实现Druid绑定到yml中
并实现一个后台监控功能
package com.example.springboot_data.config;import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;import java.util.HashMap;@Configurationpublic class DruidConfig { //绑定到yml中 @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource(){ return new DruidDataSource(); } @Bean //后台监控功能 :spring可以在增加web功能下的web.xml中配置 但是springboot就通过ServletRegistrationBean配置 public ServletRegistrationBean statViewServlet(){ ServletRegistrationBeanbean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*"); //后台登录 账户密码配置 HashMap initParameters = new HashMap<>(); //账户密码设置 initParameters.put("loginUsername","admin"); initParameters.put("loginPassword","admin"); // 禁止谁访问! initParameters.put("ahui","192.168.11.12"); bean.setInitParameters(initParameters); System.out.println(bean.getInitParameters()+"this is getInit"); return bean; }}
8.3、实现filter过滤器
import org.springframework.context.annotation.Configuration;import javax.servlet.Filter;import javax.sql.DataSource;import java.util.HashMap;@Configurationpublic class DruidConfig {@Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBeanbean = new FilterRegistrationBean<>(); bean.setFilter(new WebStatFilter()); // 过滤请求 HashMap initParameters = new HashMap<>(); //过滤这些东西 initParameters.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParameters); return bean; }}
九、springboot整合mybatis
9.1、UserController实现跳转
package com.example.springboot_mybatis.controller;import com.example.springboot_mybatis.mapper.UserMapper;import com.example.springboot_mybatis.pojo.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestControllerpublic class UserController { @Autowired(required = false) private UserMapper userMapper; @GetMapping("/queryUserList") public ListqueryUserList(){ List users = userMapper.queryUserList(); users.forEach(System.out::println); return users; } // 使用restful风格取值id 并查询 @GetMapping("/queryById/{id}") public User queryById( @PathVariable Integer id){ return userMapper.queryById(id); }}
9.2、UserMapper接口实现查询功能
package com.example.springboot_mybatis.mapper;import com.example.springboot_mybatis.pojo.User;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.springframework.stereotype.Repository;import java.util.List;@Mapper // @Mapper表示这是一个mybatis的mapper类// 也可以在主程序入口使用@MapperScan("com.example.springboot_mybatis.mapper"))@Repository//@Componentpublic interface UserMapper { @Select("select * from user") ListqueryUserList(); @Select("select * from user where id = #{id}") User queryById(@Param("id") int id); int addUser(@Param("User")User user); int updateUser(@Param("User")User user); int deleteUserById(@Param("id")int id);}
9.3、User实体类
package com.example.springboot_mybatis.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublic class User { private Integer id; private String name; private Integer age; private String email; private Integer version; private String gmt_create; private String gmt_modified;}
9.4、yml配置连接数据库及绑定mybatis
spring: datasource: type: org.springframework.jdbc.datasource.DriverManagerDataSource username: root password: root url: jdbc:mysql://localhost:3306/curry?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver#整合mybatismybatis: type-aliases-package: com.example.springboot_mybaits.pojo# mapper-locations: classpath:mybatis/mapper/*.xml 用注解就直接省去xml配置
十、SpringSecurity安全机制
安全框架:shiro、springsecurity
安全框架的作用:认证、授权
- 功能权限
- 访问权限
- 菜单权限
重要security类:
webSecurityConfiguration
: 自定义 security 策略AuthenticationManagerBuilder
: 自定义认真策略@EnableWebSecurity
: 开启 WebSecurity 模式
配置类:SecurityConfig.java
package com.example.spring_security.config;import com.example.spring_security.controller.RouterController;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.builders.WebSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;// Aop式编程@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 首页所有人可以访问,功能页只有对应有权限的人可以访问 //它是链式编程 // 授权 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3"); //认证请求 // 没有权限,进入就需要登录 http.formLogin(); //开启注销功能 并跳转到首页 http.logout().logoutSuccessUrl("/"); // springSecurity为了防止网站攻击 默认开启了csrf功能// http.csrf().disable(); } // 认证 springboot 2.1.x 可以直接使用 // 密码编码: PasswordEncoder 没有编码的错误~! // 如果没有密码编码服务器会报500错误 :.withUser("guest").password("guest").roles("vip1"); // 对他进行加密之后:new BCryptPasswordEncoder().encode("curry") @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("ahui").password(new BCryptPasswordEncoder().encode("curry")).roles("vip2","vip3") .and() .withUser("admin").password(new BCryptPasswordEncoder().encode("admin")).roles("vip2","vip3","vip1") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("guest")).roles("vip1"); }}
controller类:RouterController.java
package com.example.spring_security.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.*;import org.thymeleaf.util.StringUtils;import javax.servlet.http.HttpSession;@Controllerpublic class RouterController { @RequestMapping("/index") public String toIndex(){ return "index"; } @RequestMapping("/login") public String toLogin(){ return "/views/login"; } @PostMapping("/user/login") public String redirectLogin(Model model, HttpSession httpSession, @RequestParam("username") String userName, @RequestParam("password") String passWord){ if (!StringUtils.isEmpty(userName) && "admin".equals(passWord)) {// httpSession.setAttribute("loginUser",userName); return "redirect:/index.html"; } else { model.addAttribute("msg","用户名或密码错误"); return "views/login"; } } @RequestMapping("/level1/{id}") public String toLevel1(@PathVariable("id") int id){ return "views/level1/"+id; } @RequestMapping("/level2/{id}") public String toLevel2(@PathVariable("id") int id){ return "views/level2/"+id; } @RequestMapping("/level3/{id}") public String toLevel3(@PathVariable("id") int id){ return "views/level3/"+id; }}
十一、shiro安全机制
shiro需要一个config类来实现过滤 和一个realm对象来实现认证与授权
application.yml
#连接数据库的配置,以及使用druid数据源进行连接spring: thymeleaf: cache: false datasource: url: jdbc:mysql://localhost:3306/curry?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root type: com.alibaba.druid.pool.DruidDataSourcemybatis:# mapper-locations: classpath:mapper/*.xml //使用mybatis注解实现,不需要使用xml方式 type-aliases-package: com.example.shiro_springboot.pojo
shiroconfig.java
package com.example.shiro_springboot.config;import org.apache.shiro.spring.web.ShiroFilterFactoryBean;import org.apache.shiro.web.mgt.DefaultWebSecurityManager;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.LinkedHashMap;import java.util.Map;@Configurationpublic class ShiroConfig { // shiroFilterFactoryBean 过滤器 @Bean // 通过@Qualifier("defaultWebSecurityManager")与下面的@Bean(name = "defaultWebSecurityManager")的方法绑定 public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){ ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); // 设置安全管理器 bean.setSecurityManager(defaultWebSecurityManager); //添加shiro的内置过滤器! /* * anon : 无需认证就可以访问 * authc : 必须认证了才能访问 * user : 必须拥有记住我功能才能访问 * perms : 拥有对某个资源的权限才能访问 * role : 拥有某个角色权限才能访问 * */ MapfilterChainDefinitionMap = new LinkedHashMap<>(); bean.setFilterChainDefinitionMap(filterChainDefinitionMap);// filterChainDefinitionMap.put("/user/add","authc");// filterChainDefinitionMap.put("/user/update","authc"); //权限设置 没有add权限 filterChainDefinitionMap.put("/user/update","perms[user:update]"); filterChainDefinitionMap.put("/user/add","perms[user:add]"); filterChainDefinitionMap.put("/user/*","authc"); // 授权跳转 bean.setUnauthorizedUrl("/noauth"); bean.setLoginUrl("/toLogin"); // 设置拦截器 bean.setFilterChainDefinitionMap(filterChainDefinitionMap); return bean; } // DefaultWebSecurityManager @Bean(name = "defaultWebSecurityManager") public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){ // 通过@Qualifier("userRealm")与下面的UserRealm的方法绑定 DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); // 关联UserRealm securityManager.setRealm(userRealm); return securityManager; } //创建realm 对象 @Bean public UserRealm userRealm(){ return new UserRealm(); }}
Userrealm.java
package com.example.shiro_springboot.config;import com.example.shiro_springboot.pojo.User;import com.example.shiro_springboot.service.UserService;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.*;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.authz.SimpleAuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;import org.apache.shiro.subject.Subject;import org.springframework.beans.factory.annotation.Autowired;public class UserRealm extends AuthorizingRealm { @Autowired UserService userService; // 授权 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { System.out.println("执行了授权方法"); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addStringPermission("user:add"); //拿到当前登录的用户 的对象 Subject subject = SecurityUtils.getSubject(); User currentUser = (User) subject.getPrincipal(); //拿到User对象 // info.addStringPermission(currentUser.getPerms()); return info; } // 认证 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { System.out.println("执行了"+authenticationToken+"方法!!!"); //获取当前的用户 Subject subject = SecurityUtils.getSubject(); //伪造的数据库信息// String name = "admin";// String password = "admin"; UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken; //连接真实数据库 User user = userService.queryUserByName(userToken.getUsername()); if(user == null) // 如果等于null 表示没有这个人 return null; //Shiro 做密码加密方式,Md5 md5盐支加密 // shiro做的密码认证 直接交给shiro return new SimpleAuthenticationInfo("",user.getPwd(),""); }}
跳转视图
ShiroController.java
package com.example.shiro_springboot.controller;import com.example.shiro_springboot.mapper.UserMapper;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.IncorrectCredentialsException;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.subject.Subject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class ShiroController { @Autowired(required = false) private UserMapper userMapper; @GetMapping({"/","/index"}) public String sayShiro(Model model){ model.addAttribute("msg","hello shiro!!!"); return "index"; }// @GetMapping("/userList")// public String userList(Model model){// Listusers = userMapper.queryUserByName();// model.addAttribute("msg",users);//// users.forEach(System.out::println);// return "user/showUsers";// } @GetMapping("/user/add") public String add(){ return "user/add"; } @GetMapping("/user/update") public String update(){ return "user/update"; } @GetMapping("/toLogin") public String toLogin(){ return "user/login"; } @RequestMapping("/noauth") @ResponseBody public String noAuth(){ return "未经授权无法登陆!"; } @RequestMapping("/login") public String login(String username, String password, Model model){ // 获取当前用户 Subject subject = SecurityUtils.getSubject(); // 封装用户的登录数据 UsernamePasswordToken token = new UsernamePasswordToken(username, password); try { subject.login(token); // 执行登录的方法,如果没有异常,就登录成功! return "index"; }catch (UnknownAccountException e){ model.addAttribute("msg","用户名错误!"); return "user/login"; } catch (IncorrectCredentialsException e){ model.addAttribute("msg","密码错误!"); return "user/login"; } }}
UserMapper.java 查询数据库
package com.example.shiro_springboot.mapper;import com.example.shiro_springboot.pojo.User;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import org.springframework.stereotype.Repository;@Repository@Mapper//@Componentpublic interface UserMapper { @Select("select * from user where name = #{name}") public User queryUserByName(String name);}
实体类pojo/User.java
package com.example.shiro_springboot.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.util.Date;@Data@NoArgsConstructor@AllArgsConstructorpublic class User { private Integer id; private String perms; private String name; private String pwd; private Integer age; private String email; private Integer version; private Date gmt_create; private Date gmt_modified;}
十二、Swagger
前后端分离——技术栈 (Vue + Springboot)
- Swagger版本3.0.x 就不支持 访问localhost:8080/swagger-ui.html接口访问到
- 而Swagger的2.9.x则支持
swagger依赖
io.springfox springfox-swagger2 2.9.1 io.springfox springfox-swagger-ui 2.9.1 SwaggerConfig配置
package com.example.springboot_swagger.config;import io.swagger.annotations.Api;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.service.Contact;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;@Configuration@EnableSwagger2 //开启Swaggerpublic class SwaggerConfig { //配置了Swagger的Bean实例 @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // .enable(true) true表示启动swagger false表示不启动swagger .select() // basePackage 表示指定扫描哪个包 .paths(PathSelectors.ant("/example")) // 过滤路径! .build(); // build工厂模式 } // 配置Swagger信息 ApiInfo类 private ApiInfo apiInfo(){ return new ApiInfo("阿辉的Swagger", "Api Documentation", "v1.0", "urn:tos", new Contact("阿辉", "https://www.baidu.com", "1917523457@qq.com"), "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); }}
swagger 实现顶部分组功能!
- 不同分组实现不同环境的接口
@Bean public Docket docket(){ // 显示swagger环境 Profiles profiles = Profiles.of("dev","test"); System.out.println(profiles+"dasdas"); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // 如何做到多个分组 多个docket实例就可以做到多个分组 及多个docket方法!方法名不同即可 .groupName("阿辉") .select() // basePackage 表示指定扫描哪个包 .paths(PathSelectors.ant("/example")) // 过滤路径! .build(); // build工厂模式 } @Bean public Docket docket2(){ return new Docket(DocumentationType.SWAGGER_2).groupName("Stephen"); } @Bean public Docket docket3(){ return new Docket(DocumentationType.SWAGGER_2).groupName("curry"); }
十三、异步任务实现
service层实现多线程模拟
package com.example.demo.service;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Servicepublic class TaskService { @Async // 表示为异步锁! public void ok() throws InterruptedException { Thread.sleep(3000); System.out.println("数据正在处理中……"); }}
controller层实现
- Controller调用service层
package com.example.demo.controller;import com.example.demo.service.TaskService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class TaskController { @Autowired TaskService taskService; @RequestMapping("/hello") @ResponseBody public String sayHello() throws InterruptedException { taskService.ok(); return "OK"; }}
实现异步任务调度 还需要在主类上开启异步
@EnableAsync
定时任务的执行
corn表达式的执行
需要在主类上加入定时任务执行的注解
@EnableScheduling //开启定时任务
需要在被执行的方法的上面加上
@Scheduled(cron = "0/5 * * * * ?")
package com.example.demo.service;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic class TaskService { //corn表达式 corn表达式依次顺序 秒 分 时 日 月 星期(可以设置0-7或者? 表示每天) // corn = "30 0/5 10,18 * ?" 表示每天的十点和十八点 每隔五分钟执行一次 //corn = "0 0 12 ? * 1-6" 每个月的周一到周六12点00分执行一次 @Scheduled(cron = "0/5 * * * * ?") //每天每时每刻五秒执行一次 public void hello(){ System.out.println("hello 被执行!"); }}
十四、序列化的实现
springboot集成redis ,redis存储对象需要使用到序列化方式去传递!
package com.example.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configurationpublic class RedisConfig { @Bean @SuppressWarnings("all") // 消除所有的警告问题 public RedisTemplateredisTemplate(RedisConnectionFactory redisConnectionFactory) { // 公司序列化的模板!!!! // 为了开发方便,一般使用 RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); //Json序列化配置 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); //String的序列化 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //Key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); //hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); //value序列化方式采用jackson template.setValueSerializer(jackson2JsonRedisSerializer); //hash的value序列化方式采用Jackson template.setHashKeySerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; }}
把redis常用操作集中一起 定义成一个组件
- RedisUtil
package com.example.utils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import org.springframework.util.CollectionUtils;import java.util.Collection;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.TimeUnit;@Componentpublic class RedisUtil { @Autowired //自动装配我们上面所配置的序列化方式 private RedisTemplateredisTemplate; // =============================common============================ /** * 指定缓存失效时间 * @param key 键 * @param time 时间(秒) */ public boolean expire(String key, long time) { try { if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 根据key 获取过期时间 * @param key 键 不能为null * @return 时间(秒) 返回0代表为永久有效 */ public long getExpire(String key) { return redisTemplate.getExpire(key, TimeUnit.SECONDS); } /** * 判断key是否存在 * @param key 键 * @return true 存在 false不存在 */ public boolean hasKey(String key) { try { return redisTemplate.hasKey(key); } catch (Exception e) { e.printStackTrace(); return false; } } /** * 删除缓存 * @param key 可以传一个值 或多个 */ @SuppressWarnings("unchecked") public void del(String... key) { if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); } else { redisTemplate.delete((Collection ) CollectionUtils.arrayToList(key)); } } } // ============================String============================= /** * 普通缓存获取 * @param key 键 * @return 值 */ public Object get(String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } /** * 普通缓存放入 * @param key 键 * @param value 值 * @return true成功 false失败 */ public boolean set(String key, Object value) { try { redisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 普通缓存放入并设置时间 * @param key 键 * @param value 值 * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 * @return true成功 false 失败 */ public boolean set(String key, Object value, long time) { try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 递增 * @param key 键 * @param delta 要增加几(大于0) */ public long incr(String key, long delta) { if (delta < 0) { throw new RuntimeException("递增因子必须大于0"); } return redisTemplate.opsForValue().increment(key, delta); } /** * 递减 * @param key 键 * @param delta 要减少几(小于0) */ public long decr(String key, long delta) { if (delta < 0) { throw new RuntimeException("递减因子必须大于0"); } return redisTemplate.opsForValue().increment(key, -delta); } // ================================Map================================= /** * HashGet * @param key 键 不能为null * @param item 项 不能为null */ public Object hget(String key, String item) { return redisTemplate.opsForHash().get(key, item); } /** * 获取hashKey对应的所有键值 * @param key 键 * @return 对应的多个键值 */ public Map
发表评论
最新留言
关于作者
