configs;
16 |
17 | @Data
18 | public static class MpConfig {
19 | /**
20 | * 设置微信公众号的appid
21 | */
22 | private String appId;
23 |
24 | /**
25 | * 设置微信公众号的app secret
26 | */
27 | private String secret;
28 |
29 | /**
30 | * 设置微信公众号的token
31 | */
32 | private String token;
33 |
34 | /**
35 | * 设置微信公众号的EncodingAESKey
36 | */
37 | private String aesKey;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/spring-boot-read-yml/src/main/java/com/example/springbootreadyml/controller/TestReadYmlController.java:
--------------------------------------------------------------------------------
1 | package com.example.springbootreadyml.controller;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.example.springbootreadyml.config.WxMpProperties;
5 | import lombok.extern.slf4j.Slf4j;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.beans.factory.annotation.Value;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | /**
12 | * 读取yml文件的controller
13 | *
14 | * @Authro Java碎碎念
15 | */
16 | @Slf4j
17 | @RestController
18 | public class TestReadYmlController {
19 |
20 | @Value("${server.port}")
21 | private Integer port;
22 |
23 | @Autowired
24 | private WxMpProperties wxMpProperties;
25 |
26 | @RequestMapping("/readYml")
27 | public void readYml() {
28 | log.info("server.port=" + port);
29 | log.info("wxMpProperties=" + JSON.toJSONString(wxMpProperties));
30 | }
31 | }
32 |
33 |
34 |
--------------------------------------------------------------------------------
/spring-boot-read-yml/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | wechat:
2 | mp:
3 | configs:
4 | - appid: appid1
5 | secret: arr1_secret
6 | token: arr1_token
7 | aesKey: arr1_key
8 | msgDataFormat: JSON
9 |
10 | - appid: appid2
11 | secret: arr2_secret
12 | token: arr2_token
13 | aesKey: arr2_key
14 | msgDataFormat: JSON
15 | server:
16 | port: 8888
17 | servlet:
18 | context-path: /
--------------------------------------------------------------------------------
/spring-boot-read-yml/src/test/java/com/example/springbootreadyml/SpringBootReadYmlApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.springbootreadyml;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class SpringBootReadYmlApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-boot-security/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/spring-boot-security/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.2.1.RELEASE
9 |
10 |
11 | com.example
12 | spring-boot-security
13 | 0.0.1-SNAPSHOT
14 | spring-boot-security
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-security
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-test
36 | test
37 |
38 |
39 | org.junit.vintage
40 | junit-vintage-engine
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | org.springframework.boot
51 | spring-boot-maven-plugin
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/spring-boot-security/src/main/java/com/example/springboot/security/SpringBootSecurityApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.security;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringBootSecurityApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringBootSecurityApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-boot-security/src/main/java/com/example/springboot/security/config/MyPasswordEncoder.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.security.config;
2 |
3 | import org.springframework.security.crypto.password.PasswordEncoder;
4 |
5 | public class MyPasswordEncoder implements PasswordEncoder {
6 | @Override
7 | public String encode(CharSequence rawPassword) {
8 | return rawPassword.toString();
9 | }
10 |
11 | @Override
12 | public boolean matches(CharSequence rawPassword, String encodedPassword) {
13 | return encodedPassword.equals(rawPassword);
14 | }
15 | }
--------------------------------------------------------------------------------
/spring-boot-security/src/main/java/com/example/springboot/security/config/WebSecurityConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.security.config;
2 |
3 | import org.springframework.context.annotation.Configuration;
4 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8 |
9 | /**
10 | * 配置类
11 | * @Author java_suisui
12 | *
13 | */
14 | @EnableWebSecurity
15 | @Configuration
16 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
17 |
18 | @Override
19 | protected void configure(AuthenticationManagerBuilder auth) throws Exception {
20 | //配置内存中的 用户名、密码和角色
21 | auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("user").password("123456").roles("USER");
22 | auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("admin").password("123456").roles("ADMIN");
23 | }
24 |
25 | @Override
26 | protected void configure(HttpSecurity http) throws Exception {
27 | http.authorizeRequests()
28 | .antMatchers("/login").permitAll()
29 | .antMatchers("/user").hasRole("USER") //访问 /user这个接口,需要有USER角色
30 | .antMatchers("/admin").hasRole("ADMIN")
31 | .anyRequest().authenticated() //剩余的其他接口,登录之后就能访问
32 | .and()
33 | .formLogin().defaultSuccessUrl("/hello");
34 | }
35 | }
--------------------------------------------------------------------------------
/spring-boot-security/src/main/java/com/example/springboot/security/controller/AdminController.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.security.controller;
2 |
3 | import org.springframework.web.bind.annotation.GetMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 |
6 | /**
7 | * AdminController
8 | */
9 | @RestController
10 | public class AdminController {
11 |
12 | @GetMapping("/admin")
13 | public String hello() {
14 | return "Hello,admin!";
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/spring-boot-security/src/main/java/com/example/springboot/security/controller/AppController.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.security.controller;
2 |
3 | import org.springframework.web.bind.annotation.GetMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 |
6 | /**
7 | * AppController
8 | */
9 | @RestController
10 | public class AppController {
11 |
12 | @GetMapping("/hello")
13 | public String hello() {
14 | return "Hello,spring security!";
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/spring-boot-security/src/main/java/com/example/springboot/security/controller/UserController.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.security.controller;
2 |
3 | import org.springframework.web.bind.annotation.GetMapping;
4 | import org.springframework.web.bind.annotation.RestController;
5 |
6 | /**
7 | * UserController
8 | */
9 | @RestController
10 | public class UserController {
11 |
12 | @GetMapping("/user")
13 | public String hello() {
14 | return "Hello,user!";
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/spring-boot-security/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.security.user.name=java_suisui
2 | spring.security.user.password=123456
--------------------------------------------------------------------------------
/spring-boot-security/src/test/java/com/example/springboot/security/SpringBootSecurityApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.security;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class SpringBootSecurityApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-boot-study-base/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 |
8 | org.springframework.boot
9 | spring-boot-starter-parent
10 | 2.1.6.RELEASE
11 |
12 |
13 |
14 | com.java.suisui.study
15 | spring-boot-study-base
16 | 1.0-SNAPSHOT
17 |
18 |
19 | 1.8
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-test
31 | test
32 |
33 |
34 |
35 |
36 | org.jsoup
37 | jsoup
38 | 1.12.1
39 |
40 |
41 |
42 |
43 | com.alibaba
44 | fastjson
45 | 1.2.58
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | org.springframework.boot
57 | spring-boot-maven-plugin
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/spring-boot-study-base/src/main/java/com/study/day01/Config.java:
--------------------------------------------------------------------------------
1 | package com.study.day01;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Conditional;
5 | import org.springframework.context.annotation.Configuration;
6 |
7 | /**
8 | * @Auther: lifq
9 | * @Description:
10 | */
11 | @Configuration
12 | public class Config {
13 |
14 | @Bean
15 | @Conditional(WindowsCondition.class)
16 | public ListService window() {
17 | return new WindowsService();
18 | }
19 |
20 | @Bean
21 | @Conditional(LinuxCondition.class)
22 | public ListService linux() {
23 | return new LinuxService();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/spring-boot-study-base/src/main/java/com/study/day01/LinuxCondition.java:
--------------------------------------------------------------------------------
1 | package com.study.day01;
2 |
3 | import org.springframework.context.annotation.Condition;
4 | import org.springframework.context.annotation.ConditionContext;
5 | import org.springframework.core.type.AnnotatedTypeMetadata;
6 |
7 | /**
8 | * @Auther: lifq
9 | * @Description:
10 | */
11 | public class LinuxCondition implements Condition {
12 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
13 | return context.getEnvironment().getProperty("os.name").contains("Linux");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/spring-boot-study-base/src/main/java/com/study/day01/LinuxService.java:
--------------------------------------------------------------------------------
1 | package com.study.day01;
2 |
3 | /**
4 | * @Auther: lifq
5 | * @Description:
6 | */
7 | public class LinuxService implements ListService {
8 | public String showListCmd() {
9 | return "ls";
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/spring-boot-study-base/src/main/java/com/study/day01/ListService.java:
--------------------------------------------------------------------------------
1 | package com.study.day01;
2 |
3 | import org.springframework.stereotype.Service;
4 |
5 | /**
6 | * @Auther: lifq
7 | * @Description:
8 | */
9 | public interface ListService {
10 |
11 | public String showListCmd();
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-boot-study-base/src/main/java/com/study/day01/Main01.java:
--------------------------------------------------------------------------------
1 | package com.study.day01;
2 |
3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4 |
5 | /**
6 | * @Auther: lifq
7 | * @Description:
8 | */
9 | public class Main01 {
10 |
11 | public static void main (String []args) {
12 |
13 | AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
14 |
15 | ListService listService = applicationContext.getBean(ListService.class);
16 |
17 | System.out.println(applicationContext.getEnvironment().getProperty("os.name") + "系统下的列表命令为:" + listService.showListCmd());
18 |
19 |
20 | }
21 |
22 |
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/spring-boot-study-base/src/main/java/com/study/day01/WindowsCondition.java:
--------------------------------------------------------------------------------
1 | package com.study.day01;
2 |
3 | import org.springframework.context.annotation.Condition;
4 | import org.springframework.context.annotation.ConditionContext;
5 | import org.springframework.core.type.AnnotatedTypeMetadata;
6 |
7 | /**
8 | * @Auther: lifq
9 | * @Description:
10 | */
11 | public class WindowsCondition implements Condition {
12 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
13 | return context.getEnvironment().getProperty("os.name").contains("Windows");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/spring-boot-study-base/src/main/java/com/study/day01/WindowsService.java:
--------------------------------------------------------------------------------
1 | package com.study.day01;
2 |
3 | /**
4 | * @Auther: lifq
5 | * @Description:
6 | */
7 | public class WindowsService implements ListService {
8 | public String showListCmd() {
9 | return "dir";
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/spring-boot-websocket/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/spring-boot-websocket/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.2.1.RELEASE
9 |
10 |
11 | com.example
12 | spring-boot-websocket
13 | 0.0.1-SNAPSHOT
14 | spring-boot-websocket
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-websocket
30 |
31 |
32 |
33 | org.springframework.boot
34 | spring-boot-starter-thymeleaf
35 |
36 |
37 |
38 | org.projectlombok
39 | lombok
40 | true
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-starter-test
45 | test
46 |
47 |
48 | org.junit.vintage
49 | junit-vintage-engine
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | org.springframework.boot
59 | spring-boot-maven-plugin
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/spring-boot-websocket/src/main/java/com/example/springboot/websocket/SpringBootWebsocketApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.websocket;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringBootWebsocketApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringBootWebsocketApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-boot-websocket/src/main/java/com/example/springboot/websocket/config/WebSocketConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.websocket.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.web.socket.server.standard.ServerEndpointExporter;
6 |
7 |
8 | @Configuration
9 | public class WebSocketConfig {
10 | /**
11 | * 给spring容器注入这个ServerEndpointExporter对象
12 | * 相当于xml:
13 | *
14 | *
15 | *
16 | *
17 | * 检测所有带有@serverEndpoint注解的bean并注册他们。
18 | *
19 | * @return
20 | */
21 | @Bean
22 | public ServerEndpointExporter serverEndpointExporter() {
23 | System.out.println("我被注入了");
24 | return new ServerEndpointExporter();
25 | }
26 | }
--------------------------------------------------------------------------------
/spring-boot-websocket/src/main/java/com/example/springboot/websocket/controller/WebSocketController.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.websocket.controller;
2 |
3 | import com.example.springboot.websocket.util.WebSocketServer;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.RequestMethod;
6 | import org.springframework.web.bind.annotation.RequestParam;
7 | import org.springframework.web.bind.annotation.RestController;
8 |
9 | import java.io.IOException;
10 |
11 | /**
12 | */
13 | @RestController
14 | @RequestMapping("/api/ws")
15 | public class WebSocketController {
16 |
17 |
18 | /**
19 | * 群发消息内容
20 | *
21 | * @param message
22 | * @return
23 | */
24 | @RequestMapping(value = "/sendAll", method = RequestMethod.GET)
25 | public String sendAllMessage(@RequestParam(required = true) String message) {
26 | try {
27 | WebSocketServer.BroadCastInfo(message);
28 | } catch (IOException e) {
29 | e.printStackTrace();
30 | }
31 | return "ok";
32 | }
33 |
34 | /**
35 | * 指定会话ID发消息
36 | *
37 | * @param message 消息内容
38 | * @param id 连接会话ID
39 | * @return
40 | */
41 | @RequestMapping(value = "/sendOne", method = RequestMethod.GET)
42 | public String sendOneMessage(@RequestParam(required = true) String message, @RequestParam(required = true) String id) {
43 | try {
44 | WebSocketServer.SendMessage(message, id);
45 | } catch (IOException e) {
46 | e.printStackTrace();
47 | }
48 | return "ok";
49 | }
50 | }
--------------------------------------------------------------------------------
/spring-boot-websocket/src/main/java/com/example/springboot/websocket/dto/SocketMessage.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.websocket.dto;
2 |
3 | public class SocketMessage {
4 |
5 | public String message;
6 |
7 | public String date;
8 |
9 | }
--------------------------------------------------------------------------------
/spring-boot-websocket/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/spring-boot-websocket/src/main/resources/static/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | websocket测试
6 |
7 |
12 |
13 |
14 |
15 | WebSocket测试,客户端接收到的消息如下:
16 |
17 |
20 |
21 |
22 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/spring-boot-websocket/src/test/java/com/example/springboot/websocket/SpringBootWebsocketApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.springboot.websocket;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class SpringBootWebsocketApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/spring-cache-demo/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/spring-cache-demo/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/suisui2019/springboot-study/13083fe6fa2e748097a0b98c78bc76cb6f2ab98f/spring-cache-demo/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/spring-cache-demo/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip
2 |
--------------------------------------------------------------------------------
/spring-cache-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.7.RELEASE
9 |
10 |
11 | com.example
12 | demo
13 | 0.0.1-SNAPSHOT
14 | demo
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-data-jpa
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-cache
34 |
35 |
36 |
37 | org.springframework.boot
38 | spring-boot-starter-data-redis
39 |
40 |
41 |
42 | mysql
43 | mysql-connector-java
44 | runtime
45 |
46 |
47 | org.projectlombok
48 | lombok
49 | true
50 |
51 |
52 | org.springframework.boot
53 | spring-boot-starter-test
54 | test
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | org.springframework.boot
63 | spring-boot-maven-plugin
64 |
65 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/spring-cache-demo/src/main/java/com/example/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cache.annotation.EnableCaching;
6 | /**
7 | * 整合缓存demo
8 | *
9 | * @Date: 2019/8/22 19:33
10 | *
11 | */
12 | @SpringBootApplication
13 | @EnableCaching
14 | public class DemoApplication {
15 | public static void main(String[] args) {
16 | SpringApplication.run(DemoApplication.class, args);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/spring-cache-demo/src/main/java/com/example/demo/aop/LogRecordAspect.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.aop;
2 |
3 | import org.aspectj.lang.ProceedingJoinPoint;
4 | import org.aspectj.lang.annotation.Around;
5 | import org.aspectj.lang.annotation.Aspect;
6 | import org.aspectj.lang.annotation.Pointcut;
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.web.context.request.RequestAttributes;
11 | import org.springframework.web.context.request.RequestContextHolder;
12 | import org.springframework.web.context.request.ServletRequestAttributes;
13 |
14 | import javax.servlet.http.HttpServletRequest;
15 |
16 |
17 | /**
18 | * 切面 打印请求、返回参数信息
19 | *
20 | * @author lifq
21 | *
22 | * 2018年10月18日 上午10:56:49
23 | */
24 | @Aspect // 定义一个切面
25 | @Configuration
26 | public class LogRecordAspect {
27 | private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class);
28 |
29 | // 定义切点Pointcut
30 | @Pointcut("execution(* com.example.demo.controller.*Controller.*(..))")
31 | public void excudeService() {
32 | }
33 |
34 | @Around("excudeService()")
35 | public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
36 | RequestAttributes ra = RequestContextHolder.getRequestAttributes();
37 | ServletRequestAttributes sra = (ServletRequestAttributes) ra;
38 | HttpServletRequest request = sra.getRequest();
39 |
40 | String method = request.getMethod();
41 | String uri = request.getRequestURI();
42 | logger.info("***************************************************");
43 | logger.info("请求开始, URI: {}, method: {}", uri, method);
44 | long start = System.currentTimeMillis();
45 |
46 | // result的值就是被拦截方法的返回值
47 | Object result = pjp.proceed();
48 | logger.info("请求结束, URI: {},耗时={}", uri, System.currentTimeMillis() - start);
49 | return result;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/spring-cache-demo/src/main/java/com/example/demo/config/WebConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.config;
2 |
3 | import org.springframework.cache.CacheManager;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.context.annotation.Primary;
7 | import org.springframework.data.redis.cache.RedisCacheConfiguration;
8 | import org.springframework.data.redis.cache.RedisCacheManager;
9 | import org.springframework.data.redis.cache.RedisCacheWriter;
10 | import org.springframework.data.redis.connection.RedisConnectionFactory;
11 | import org.springframework.data.redis.core.RedisTemplate;
12 | import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
13 | import org.springframework.data.redis.serializer.RedisSerializationContext;
14 | import org.springframework.data.redis.serializer.RedisSerializer;
15 | import org.springframework.data.redis.serializer.StringRedisSerializer;
16 |
17 | import java.time.Duration;
18 |
19 | @Configuration
20 | public class WebConfig {
21 |
22 | @Bean(name = "redisTemplate")
23 | public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
24 |
25 | RedisTemplate redisTemplate = new RedisTemplate<>();
26 |
27 | redisTemplate.setConnectionFactory(redisConnectionFactory);
28 | redisTemplate.setKeySerializer(keySerializer());
29 | redisTemplate.setHashKeySerializer(keySerializer());
30 | redisTemplate.setValueSerializer(valueSerializer());
31 | redisTemplate.setHashValueSerializer(valueSerializer());
32 | return redisTemplate;
33 | }
34 |
35 | @Primary
36 | @Bean
37 | public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
38 | //缓存配置对象
39 | RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
40 |
41 | redisCacheConfiguration = redisCacheConfiguration.entryTtl(Duration.ofMinutes(30L)) //设置缓存的默认超时时间:30分钟
42 | .disableCachingNullValues() //如果是空值,不缓存
43 | .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer())) //设置key序列化器
44 | .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer((valueSerializer()))); //设置value序列化器
45 |
46 | return RedisCacheManager
47 | .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
48 | .cacheDefaults(redisCacheConfiguration).build();
49 | }
50 |
51 | private RedisSerializer keySerializer() {
52 | return new StringRedisSerializer();
53 | }
54 |
55 | private RedisSerializer