DreamWeixinAutoConfiguration.java 1.6 KB
Newer Older
如梦技术's avatar
如梦技术 已提交
1 2
package net.dreamlu.weixin.config;

3
import lombok.RequiredArgsConstructor;
如梦技术's avatar
如梦技术 已提交
4 5
import net.dreamlu.weixin.cache.SpringAccessTokenCache;
import net.dreamlu.weixin.properties.DreamWeixinProperties;
如梦技术's avatar
v1.3.0  
如梦技术 已提交
6
import net.dreamlu.weixin.spring.MsgInterceptor;
如梦技术's avatar
如梦技术 已提交
7
import org.springframework.boot.context.properties.EnableConfigurationProperties;
如梦技术's avatar
如梦技术 已提交
8 9
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
10
import org.springframework.cache.annotation.EnableCaching;
如梦技术's avatar
如梦技术 已提交
11 12
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
如梦技术's avatar
v1.3.0  
如梦技术 已提交
13 14
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
如梦技术's avatar
如梦技术 已提交
15

如梦技术's avatar
如梦技术 已提交
16 17 18 19 20
/**
 * 微信自动配置
 *
 * @author L.cm
 */
21
@Configuration
22
@EnableCaching
如梦技术's avatar
如梦技术 已提交
23
@EnableConfigurationProperties(DreamWeixinProperties.class)
如梦技术's avatar
如梦技术 已提交
24 25 26
public class DreamWeixinAutoConfiguration {

	@Bean
27 28
	public SpringAccessTokenCache springAccessTokenCache(CacheManager cacheManager,
														 DreamWeixinProperties properties) {
如梦技术's avatar
v1.3.0  
如梦技术 已提交
29
		Cache cache = cacheManager.getCache(properties.getAccessTokenCache());
如梦技术's avatar
如梦技术 已提交
30 31 32
		return new SpringAccessTokenCache(cache);
	}

如梦技术's avatar
v1.3.0  
如梦技术 已提交
33
	@Configuration
34
	@RequiredArgsConstructor
如梦技术's avatar
v1.3.0  
如梦技术 已提交
35
	public static class MsgConfiguration extends WebMvcConfigurerAdapter {
如梦技术's avatar
v1.3.0  
如梦技术 已提交
36 37 38 39 40 41 42
		private final DreamWeixinProperties properties;

		@Override
		public void addInterceptors(InterceptorRegistry registry) {
			String urlPattern = properties.getUrlPatterns();
			MsgInterceptor httpCacheInterceptor = new MsgInterceptor(properties);
			registry.addInterceptor(httpCacheInterceptor)
如梦技术's avatar
如梦技术 已提交
43
				.addPathPatterns(urlPattern);
如梦技术's avatar
v1.3.0  
如梦技术 已提交
44 45
		}
	}
如梦技术's avatar
如梦技术 已提交
46
}