一、spring boot从官网生成后需要增加以下pom才能启动:
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-jetty org.springframework.boot spring-boot-configuration-processor true
二、spring 与redis集成:
1.pom中增加:
redis.clients jedis 2.9.0
2.增加配置类RedisConfig
package com.zdnst.platform.system.config;import com.zdnst.platform.system.config.dubbo.DubboProperties;import com.zdnst.platform.system.face.RedisFace;import com.zdnst.platform.system.impl.RedisFaceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.data.redis.RedisProperties;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.cache.CacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.RedisSentinelConfiguration;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.repository.cdi.RedisKeyValueTemplateBean;import redis.clients.jedis.JedisPoolConfig;@Configuration@EnableAutoConfiguration@EnableConfigurationProperties(RedisProperties.class)public class RedisConfig { @Autowired RedisProperties redisProperties; @Bean @ConfigurationProperties(prefix="spring.redis") public JedisPoolConfig getRedisConfig(){ JedisPoolConfig config = new JedisPoolConfig(); return config; } @Bean @ConfigurationProperties(prefix="spring.redis") public JedisConnectionFactory getConnectionFactory(){ JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(redisProperties.getHost()); factory.setPort(redisProperties.getPort()); factory.setPassword(redisProperties.getPassword()); factory.setUsePool(true); JedisPoolConfig config = getRedisConfig(); factory.setPoolConfig(config); return factory; } @Bean public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; }//// @Bean// public RedisTemplate getRedisTemplate(){// RedisTemplate template = new RedisKeyValueTemplateBean(getConnectionFactory());// return template;// }}
3、编写redisFace
/** * Copyright(C) 2012-2014 GuangZhou zdnst Co., Ltd. All Rights Reserved. * 版权所有(C)2012-2014 * 公司名称:广州支点科技有限公司 * 公司地址:广州市天河区天源路401号之三E2栋 * 网址:http://www.100100system.com/ *标 题:
*文 件 名:com.zdnst.maps.core.services.impl.RedisServiceImpl.java
*部 门:研发一部 *
版 本: 1.0
*Compiler: JDK1.6.0_21
*创 建 者:yongqin.zhong
*创建时间:May 21, 20154:11:41 PM
*修 改 者:
*修改时间:
*/package com.zdnst.platform.system.impl;import com.google.common.collect.Lists;import com.google.common.collect.Maps;import com.zdnst.common.constant.BaseCode;import com.zdnst.common.util.Constants;import com.zdnst.common.util.DateUtils;import com.zdnst.common.util.StringUtils;import com.zdnst.common.util.ThreadPoolUtils;import com.zdnst.platform.system.exception.PlatformSystemException;import com.zdnst.platform.system.face.RedisFace;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Service;import javax.servlet.http.HttpSession;import java.io.Serializable;import java.util.*;import java.util.concurrent.ExecutorService;import java.util.concurrent.TimeUnit;@Service("redisFace")public class RedisFaceImpl implements RedisFace { /** * 日志打印,使用org.slf4j.Logger */ protected Logger logger = LoggerFactory.getLogger(getClass()); private ExecutorService pool = ThreadPoolUtils.getPool(); /** * redis spring template added by pengzh 2015-4-15 */ @Autowired protected RedisTemplateredisTemplate; @Override public void setMinTimeValue(final Serializable key, final Serializable value, final Integer mins) throws PlatformSystemException { pool.submit(new Runnable(){ @Override public void run() { try { redisTemplate.opsForValue().set(key, value); if(mins!=null&&mins>0){ redisTemplate.expire(key,mins,TimeUnit.MINUTES); } }catch (PlatformSystemException e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //必须抛出通知调用者 throw new PlatformSystemException(e.getCode(),e.getMessage()); } catch (Exception e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //封装为服务层异常110网络超时后再抛出,必须抛出通知调用者 throw new PlatformSystemException(BaseCode.ERROR_CODE602.code,e.getMessage()); } } }); } @Override public void setHourTimeValue(final Serializable key, final Serializable value, final Integer hours) throws PlatformSystemException{ pool.submit(new Runnable(){ @Override public void run() { try { redisTemplate.opsForValue().set(key, value); if(hours!=null&&hours>0){ redisTemplate.expire(key,hours,TimeUnit.HOURS); } }catch (PlatformSystemException e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //必须抛出通知调用者 throw new PlatformSystemException(e.getCode(),e.getMessage()); } catch (Exception e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //封装为服务层异常110网络超时后再抛出,必须抛出通知调用者 throw new PlatformSystemException(BaseCode.ERROR_CODE602.code,e.getMessage()); } } }); } @Override public void setDaysTimeValue(final Serializable key, final Serializable value, final Integer days) throws PlatformSystemException{ pool.submit(new Runnable(){ @Override public void run() { try { redisTemplate.opsForValue().set(key, value); if(days!=null&&days>0){ redisTemplate.expire(key,days,TimeUnit.DAYS); } } catch (Exception e) { e.printStackTrace(); } } }); } @Override public void expire(final Serializable key, final Integer days, TimeUnit timeUnit) throws PlatformSystemException { if(days!=null&&days>0){ redisTemplate.expire(key,days,timeUnit); } } @Override public Serializable get(Object key) throws PlatformSystemException{ try { if(StringUtils.isNotEmpty((String) key)){ return redisTemplate.opsForValue().get(key); } else{ return null; } }catch (PlatformSystemException e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //必须抛出通知调用者 throw new PlatformSystemException(e.getCode(),e.getMessage()); } catch (Exception e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //封装为服务层异常110网络超时后再抛出,必须抛出通知调用者 throw new PlatformSystemException(BaseCode.ERROR_CODE602.code,e.getMessage()); } } @Override public void remove(final Object KeyLike)throws PlatformSystemException{ pool.submit(new Runnable(){ @Override public void run() { try { Set setKeys= redisTemplate.keys(KeyLike+"*"); if(setKeys!=null&&setKeys.size()>0){ Iterator it = setKeys.iterator(); while (it.hasNext()) { Serializable oneKey = it.next(); redisTemplate.delete(oneKey); } } }catch (PlatformSystemException e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //必须抛出通知调用者 throw new PlatformSystemException(e.getCode(),e.getMessage()); } catch (Exception e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //封装为服务层异常110网络超时后再抛出,必须抛出通知调用者 throw new PlatformSystemException(BaseCode.ERROR_CODE602.code,e.getMessage()); } } }); } @Override public void delete(final Serializable key)throws PlatformSystemException { try { if(StringUtils.isNotEmpty((String) key)){ redisTemplate.delete(key); } }catch (PlatformSystemException e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //必须抛出通知调用者 throw new PlatformSystemException(e.getCode(),e.getMessage()); } catch (Exception e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //封装为服务层异常110网络超时后再抛出,必须抛出通知调用者 throw new PlatformSystemException(BaseCode.ERROR_CODE602.code,e.getMessage()); } } /** * 获取流水号 * @param seqKey * @return * @throws PlatformSystemException */ @Override public String getSeq(String seqKey) throws PlatformSystemException { try { Integer seq = (Integer) this.get(seqKey); if(seq == null) { seq = 1; }else{ //重新插入序列号 if(seq == 9999){ seq = 1; }else{ seq ++; } } this.setMinTimeValue(seqKey, seq, null); String fSeq=this.gengercode(4, seq+"",DateUtils.DATE_FORMAT_YYYYMMDD); return fSeq; }catch (PlatformSystemException e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //必须抛出通知调用者 throw new PlatformSystemException(e.getCode(),e.getMessage()); } catch (Exception e) { //打印服务层异常详情,注意第二个参数必须传打印异常堆栈 logger.error(Constants.EX_SERVICE_EXCEPTION + BaseCode.getMsg(e), e); //封装为服务层异常110网络超时后再抛出,必须抛出通知调用者 throw new PlatformSystemException(BaseCode.ERROR_CODE602.code,e.getMessage()); } } /** * 获取流水号 * @param maxLen 流水号长度 * @param code 流水号 * @return */ public String gengercode(int maxLen,String code,String format){ String genCode = code; int len = maxLen - code.length(); for(int i=0;i
三、