Skip to content

Commit 069175b

Browse files
author
ZuRun's MacBook Pro
committed
redis模块
1 parent 6ed7ace commit 069175b

23 files changed

Lines changed: 843 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
target/
22
!.mvn/wrapper/maven-wrapper.jar
33

4+
### MAC ###
5+
.DS_Store
6+
47
### STS ###
58
.apt_generated
69
.classpath
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.zuhr.demo.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.cache.annotation.EnableCaching;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.PropertySource;
8+
9+
/**
10+
*
11+
* spring-boot更新到1.5.2版本后locations属性无法使用
12+
* @PropertySource 注解只可以加载proprties文件,无法加载yaml文件
13+
* 故现在把数据放到application.yml文件中,spring-boot启动时会加载
14+
*
15+
* @author zurun
16+
* @date 2018/2/11 10:37:16
17+
*/
18+
@Configuration
19+
@PropertySource(value = "classpath:/config/myTestConf.yml")
20+
@ConfigurationProperties(prefix = "test")
21+
@EnableCaching
22+
public class ConfigBean {
23+
@Value("${name}")
24+
private String name;
25+
@Value("${age}")
26+
private int age;
27+
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public int getAge() {
38+
return age;
39+
}
40+
41+
public void setAge(int age) {
42+
this.age = age;
43+
}
44+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package me.zuhr.demo.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.http.converter.HttpMessageConverter;
6+
import org.springframework.http.converter.StringHttpMessageConverter;
7+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
10+
import java.nio.charset.Charset;
11+
import java.util.List;
12+
13+
/**
14+
* 解决SpringBoot的中文乱码问题
15+
*
16+
* @author zurun
17+
* @date 2018/2/11 11:28:49
18+
*/
19+
//@Configuration
20+
public class CustomMVCConfiguration extends WebMvcConfigurerAdapter {
21+
// @Bean
22+
// public HttpMessageConverter<String> responseBodyConverter() {
23+
// StringHttpMessageConverter converter = new StringHttpMessageConverter(
24+
// Charset.forName("UTF-8"));
25+
// return converter;
26+
// }
27+
//
28+
// @Override
29+
// public void configureMessageConverters(
30+
// List<HttpMessageConverter<?>> converters) {
31+
// super.configureMessageConverters(converters);
32+
// converters.add(responseBodyConverter());
33+
// }
34+
//
35+
// @Override
36+
// public void configureContentNegotiation(
37+
// ContentNegotiationConfigurer configurer) {
38+
// configurer.favorPathExtension(false);
39+
// }
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package me.zuhr.demo.interceptor;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.servlet.HandlerInterceptor;
6+
import org.springframework.web.servlet.ModelAndView;
7+
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
/**
12+
* @author zurun
13+
* @date 2017/12/27 16:59:59
14+
*/
15+
public class MyInterceptor implements HandlerInterceptor {
16+
/** logger */
17+
private static final Logger LOGGER = LoggerFactory.getLogger(MyInterceptor.class);
18+
19+
@Override
20+
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
21+
// LOGGER.debug("拦截器拦截!");
22+
// LOGGER.info("拦截器拦截!");
23+
// LOGGER.error("拦截器拦截!");
24+
// LOGGER.info(httpServletRequest.getRequestURI());
25+
// if("/".equals(httpServletRequest.getRequestURI())){
26+
// httpServletResponse.sendRedirect("404.html");
27+
//
28+
// return false;
29+
// }
30+
31+
return true;
32+
}
33+
34+
@Override
35+
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
36+
37+
}
38+
39+
@Override
40+
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
41+
42+
}
43+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.zuhr.demo.interceptor.config;
2+
3+
import me.zuhr.demo.interceptor.MyInterceptor;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
/**
9+
* 重写WebMvcConfigurerAdapter中的addInterceptors方法把自定义的拦截器类添加进来即可
10+
*
11+
* @author zurun
12+
* @date 2017/12/27 17:05:57
13+
*/
14+
@Configuration
15+
public class AddInterceptor extends WebMvcConfigurerAdapter {
16+
@Override
17+
public void addInterceptors(InterceptorRegistry registry) {
18+
// 多个拦截器组成一个拦截器链
19+
// addPathPatterns 用于添加拦截规则
20+
// excludePathPatterns 用户排除拦截
21+
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").
22+
excludePathPatterns("/index.html","/getLogo");
23+
super.addInterceptors(registry);
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package me.zuhr.demo.utils;
2+
3+
/**
4+
* 注解工具类
5+
*
6+
* @author zurun
7+
* @date 2018/2/13 01:27:16
8+
*/
9+
public class AnnotationUtils {
10+
11+
/**
12+
* 类型转换,主要用途为去掉烦人的 unchecked cast warning 提示
13+
*
14+
* @param obj
15+
* @param <T>
16+
* @return
17+
*/
18+
@SuppressWarnings("unchecked")
19+
public static <T> T cast(Object obj) {
20+
return (T) obj;
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
t2:
2+
name: aaa99
3+
test:
4+
name: 哈哈😀_aa
5+
age: ${random.int}
6+
#${random.int} # 随机数
7+
8+
# 端口号
9+
server.port: 18888

common/redis/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
<artifactId>basis</artifactId>
1818
<version>1.0-SNAPSHOT</version>
1919
</dependency>
20+
21+
<dependency>
22+
<groupId>org.springframework.data</groupId>
23+
<artifactId>spring-data-redis</artifactId>
24+
<!--<version>2.0.2.RELEASE</version>-->
25+
</dependency>
26+
<dependency>
27+
<groupId>redis.clients</groupId>
28+
<artifactId>jedis</artifactId>
29+
<version>2.9.0</version>
30+
</dependency>
2031
</dependencies>
2132

2233
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.zuhr.demo.redis;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* 这个启动项没啥用,单纯用来做单元测试的
8+
*
9+
* @author zurun
10+
* @date 2018/2/12 16:48:25
11+
*/
12+
@SpringBootApplication
13+
public class RedisApplicationTest {
14+
public static void main(String[] args) {
15+
SpringApplication.run(RedisApplicationTest.class, args);
16+
}
17+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package me.zuhr.demo.redis.config;
2+
3+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
4+
import com.fasterxml.jackson.annotation.PropertyAccessor;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import me.zuhr.demo.redis.utils.RedisUtils;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.boot.context.properties.ConfigurationProperties;
10+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.context.annotation.PropertySource;
14+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
15+
import org.springframework.data.redis.core.RedisTemplate;
16+
import org.springframework.data.redis.core.StringRedisTemplate;
17+
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
18+
import org.springframework.data.redis.serializer.StringRedisSerializer;
19+
20+
/**
21+
* @author zurun
22+
* @date 2018/2/9 17:51:33
23+
*/
24+
//@Configuration
25+
//@ConfigurationProperties(prefix = "spring.redis")
26+
//@PropertySource("classpath:redis.properties")
27+
//@EnableCaching
28+
@EnableConfigurationProperties
29+
@Configuration
30+
@PropertySource(value = "classpath:/config/redis.properties")
31+
//@ConfigurationProperties(prefix = "spring.redis")
32+
public class RedisConfig {
33+
@Value("${spring.redis.host}")
34+
private String host;
35+
@Value("${spring.redis.port}")
36+
private int port;
37+
@Value("${spring.redis.password}")
38+
private String password;
39+
40+
41+
/**
42+
* 连接redis的工厂类
43+
*
44+
* @return
45+
*/
46+
@Bean
47+
public JedisConnectionFactory jedisConnectionFactory() {
48+
JedisConnectionFactory factory = new JedisConnectionFactory();
49+
factory.setHostName(host);
50+
factory.setPort(port);
51+
// factory.setTimeout(timeout);
52+
factory.setPassword(password);
53+
// factory.setDatabase(database);
54+
return factory;
55+
}
56+
57+
/**
58+
* 配置RedisTemplate
59+
* 设置添加序列化器
60+
* key 使用string序列化器
61+
* value 使用Json序列化器
62+
* //TODO-zurun 还有一种简答的设置方式,改变defaultSerializer对象的实现。
63+
*
64+
* @return
65+
*/
66+
// @Bean
67+
// public RedisTemplate<String, String> redisTemplate() {
68+
// //StringRedisTemplate的构造方法中默认设置了stringSerializer
69+
// RedisTemplate<String, String> template = new RedisTemplate<>();
70+
// //set key serializer
71+
// StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
72+
// template.setKeySerializer(stringRedisSerializer);
73+
// template.setHashKeySerializer(stringRedisSerializer);
74+
//
75+
// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
76+
// ObjectMapper objectMapper = new ObjectMapper();
77+
// objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
78+
// objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
79+
//
80+
// jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
81+
// //set value serializer
82+
// template.setDefaultSerializer(jackson2JsonRedisSerializer);
83+
//
84+
// template.setConnectionFactory(jedisConnectionFactory());
85+
// template.afterPropertiesSet();
86+
// return template;
87+
// }
88+
@Bean
89+
RedisTemplate<String, Object> objRedisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory connectionFactory) {
90+
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
91+
redisTemplate.setConnectionFactory(connectionFactory);
92+
redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer(Object.class));
93+
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
94+
redisTemplate.setKeySerializer(stringRedisSerializer);
95+
// redisTemplate.setValueSerializer(stringRedisSerializer);
96+
redisTemplate.setHashKeySerializer(stringRedisSerializer);
97+
return redisTemplate;
98+
}
99+
100+
101+
/**
102+
* 注:Qualifier注解用来指定bean,消除重复bean提示
103+
*
104+
* @param connectionFactory
105+
* @return
106+
*/
107+
@Bean
108+
public RedisUtils redisUtils(@Qualifier("jedisConnectionFactory") JedisConnectionFactory connectionFactory) {
109+
return new RedisUtils(connectionFactory);
110+
}
111+
112+
113+
public String getHost() {
114+
return host;
115+
}
116+
117+
public void setHost(String host) {
118+
this.host = host;
119+
}
120+
121+
public int getPort() {
122+
return port;
123+
}
124+
125+
public void setPort(int port) {
126+
this.port = port;
127+
}
128+
129+
public String getPassword() {
130+
return password;
131+
}
132+
133+
public void setPassword(String password) {
134+
this.password = password;
135+
}
136+
137+
}

0 commit comments

Comments
 (0)