DruidConfig配置类
package com.xdclass.app.config;import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.web.servlet.FilterRegistrationBean;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;import java.util.Map;@Configurationpublic class DruidConfig {@Value("${druid.login.user_name}")private String userName;@Value("${druid.login.password}")private String password;/** * 必须配置数据源,不然⽆法获取到sql监控,与sql防⽕墙监控 */@Bean(name = "default_databaseSource")@ConfigurationProperties(prefix = "spring.datasource")public DataSource druidDataSource() {return new DruidDataSource();}@Beanpublic ServletRegistrationBean druidServlet() {ServletRegistrationBean servletRegistrationBean = newServletRegistrationBean();servletRegistrationBean.setServlet(new StatViewServlet());servletRegistrationBean.addUrlMappings("/druid/*");Map initParameters = new HashMap();initParameters.put("loginUsername", userName);// ⽤户名initParameters.put("loginPassword", password);// 密码initParameters.put("resetEnable", "false");// 禁⽤HTML⻚⾯上的“Reset All”功能servletRegistrationBean.setInitParameters(initParameters);return servletRegistrationBean;}@Beanpublic FilterRegistrationBean filterRegistrationBean() {FilterRegistrationBean filterRegistrationBean = newFilterRegistrationBean();filterRegistrationBean.setFilter(new WebStatFilter());filterRegistrationBean.addUrlPatterns("/*");filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");return filterRegistrationBean;}}
DubboConfig配置类
package com.xdclass.app.config;import org.apache.dubbo.config.MetadataReportConfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class DubboConfig {@Beanpublic MetadataReportConfig metadataReportConfig() {MetadataReportConfig metadataReportConfig = newMetadataReportConfig();metadataReportConfig.setAddress("zookeeper://1.127.201.102:2181");return metadataReportConfig;}}
CaptchaConfig 验证码配置类
package net.xdclass.redis.config;import com.google.code.kaptcha.Constants;import com.google.code.kaptcha.impl.DefaultKaptcha;import com.google.code.kaptcha.util.Config;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.Properties;@Configurationpublic class CaptchaConfig {/** * 验证码配置 * Kaptcha配置类名 * * @return */@Bean@Qualifier("captchaProducer")public DefaultKaptcha kaptcha() {DefaultKaptcha kaptcha = new DefaultKaptcha();Properties properties = new Properties();//验证码个数properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");//字体间隔properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE,"8");//干扰线颜色//干扰实现类properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");//图片样式properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.WaterRipple");//文字来源properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "123456789abcdefg");Config config = new Config(properties);kaptcha.setConfig(config);return kaptcha;}}
RedisTemplateConfiguration配置类
package net.xdclass.redis.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.annotation.JsonTypeInfo;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.MapperFeature;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.cache.RedisCacheManager;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.RedisSerializationContext;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.util.StringUtils;import java.lang.reflect.Method;import java.time.Duration;@Configurationpublic class RedisTemplateConfiguration {/************自定义序列化和反序列化机制配置************/** * @param redisConnectionFactory * @return */@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate redisTemplate = new RedisTemplate();redisTemplate.setConnectionFactory(redisConnectionFactory);// 使用Jackson2JsonRedisSerialize 替换默认序列化Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);// 设置key和value的序列化规则redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// 设置hashKey和hashValue的序列化规则redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// 设置支持事物//redisTemplate.setEnableTransactionSupport(true);redisTemplate.afterPropertiesSet();return redisTemplate;}/***************修改redis缓存序列化器和配置manager过期时间***************************/@Bean@Primarypublic RedisCacheManager cacheManager1Minutes(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration config = instanceConfig(60L);return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).transactionAware().build();}@Beanpublic RedisCacheManager cacheManager1Hour(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration config = instanceConfig(3600L);return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).transactionAware().build();}@Beanpublic RedisCacheManager cacheManager1Day(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration config = instanceConfig(3600 * 24L);return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).transactionAware().build();}private RedisCacheConfiguration instanceConfig(Long ttl) {Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);objectMapper.registerModule(new JavaTimeModule());// 去掉各种@JsonSerialize注解的解析objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false);// 只针对非空的值进行序列化objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);// 将类型序列化到属性json字符串中objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);return RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(ttl)).disableCachingNullValues().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));}/*********SpringCache框架自定义缓存KeyGenerator************/@Beanpublic KeyGenerator springCacheDefaultKeyGenerator(){return new KeyGenerator() {@Overridepublic Object generate(Object o, Method method, Object... objects) {return o.getClass().getSimpleName() + "_"+ method.getName() + "_"+ StringUtils.arrayToDelimitedString(objects, "_");}};}}
RestTemplateConfig配置类
package test.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.client.ClientHttpRequestFactory;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.web.client.RestTemplate;@Configurationpublic class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory) {return new RestTemplate(factory);}@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory() {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();//单位为msfactory.setReadTimeout(5000);//单位为msfactory.setConnectTimeout(5000);return factory;}}

Copyright © maxssl.com 版权所有 浙ICP备2022011180号