修改代码配置使用的自动注入方式,删除一个多余的代码

This commit is contained in:
YuCheng Hu 2023-01-29 08:45:04 -05:00
parent e28b6f25bb
commit dc9d02f0dc
12 changed files with 76 additions and 95 deletions

View File

@ -8,12 +8,17 @@
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" /> <outputRelativeToContentRoot value="true" />
<module name="wechat-j-open" /> <module name="wechat-j-open" />
<module name="wechat-official-account-spring" /> <module name="wechat-j-pay" />
<module name="wechat-j-common" /> <module name="wechat-j-common" />
<module name="wechat-j-work" />
<module name="wechat-official-account-spring" />
<module name="wechat-j-mp" /> <module name="wechat-j-mp" />
<module name="wechat-j-oa" /> <module name="wechat-j-oa" />
</profile> </profile>
</annotationProcessing> </annotationProcessing>
<bytecodeTargetLevel>
<module name="wechat-j-wecom" target="11" />
</bytecodeTargetLevel>
</component> </component>
<component name="JavacSettings"> <component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE"> <option name="ADDITIONAL_OPTIONS_OVERRIDE">

View File

@ -9,8 +9,14 @@
<file url="file://$PROJECT_DIR$/../WeChat-J/oa/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/../WeChat-J/oa/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/open/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/../WeChat-J/open/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/open/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/../WeChat-J/open/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/pay/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/pay/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/../WeChat-J/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/../WeChat-J/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/wecom/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/wecom/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/work/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/../WeChat-J/work/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component> </component>
</project> </project>

View File

@ -3,8 +3,10 @@ package com.ossez.wechat.demo.common.enums;
/** /**
* User different http client to make api call. * User different http client to make api call.
* We should only keep OK_HTTP, this enough for package * We should only keep OK_HTTP, this enough for package
*
* @author YuCheng Hu
*/ */
public enum HttpClientType { public enum HttpClientCategory {
HTTP_CLIENT, OK_HTTP OK_HTTP, HTTP_CLIENT
} }

View File

@ -0,0 +1,12 @@
package com.ossez.wechat.demo.common.enums;
/**
* WeChat's Storage Category
*
* We provide implement for MEM(RAM) and REDIS
*
* @author YuCheng Hu
*/
public enum StorageCategory {
MEM, REDIS
}

View File

@ -1,26 +0,0 @@
package com.ossez.wechat.demo.common.enums;
/**
* storage类型.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* created on 2020-08-30
*/
public enum StorageType {
/**
* 内存.
*/
Memory,
/**
* redis(JedisClient).
*/
Jedis,
/**
* redis(Redisson).
*/
Redisson,
/**
* redis(RedisTemplate).
*/
RedisTemplate
}

View File

@ -0,0 +1,37 @@
package com.ossez.wechat.demo.config;
import com.ossez.wechat.demo.common.enums.HttpClientCategory;
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatOfficialAccountServiceOkHttp;
import com.ossez.wechat.oa.config.ConfigStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* .
*
* @author someone
*/
@Configuration
@EnableConfigurationProperties(WeChatOfficialAccountProperties.class)
@Import({WeChatStorageAutoConfiguration.class})
public class WeChatConfiguration {
/**
* weChat Service Inject
*/
@Bean
@ConditionalOnMissingBean
public WeChatOfficialAccountService weChatOfficialAccountService(ConfigStorage configStorage, WeChatOfficialAccountProperties weChatOfficialAccountProperties) {
HttpClientCategory httpClientCategory = weChatOfficialAccountProperties.getConfigStorage().getHttpClientCategory();
WeChatOfficialAccountService weChatOfficialAccountService = new WeChatOfficialAccountServiceOkHttp();
weChatOfficialAccountService.setWxMpConfigStorage(configStorage);
return weChatOfficialAccountService;
}
}

View File

@ -1,17 +1,15 @@
package com.ossez.wechat.demo.config; package com.ossez.wechat.demo.config;
import com.ossez.wechat.demo.common.enums.StorageType; import com.ossez.wechat.demo.common.enums.StorageCategory;
import com.ossez.wechat.demo.properties.RedisProperties; import com.ossez.wechat.demo.properties.RedisProperties;
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties; import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.ossez.wechat.oa.config.ConfigStorage; import com.ossez.wechat.oa.config.ConfigStorage;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import com.ossez.wechat.common.redis.JedisWxRedisOps;
import com.ossez.wechat.common.redis.RedisTemplateWxRedisOps; import com.ossez.wechat.common.redis.RedisTemplateWxRedisOps;
import com.ossez.wechat.common.redis.WxRedisOps; import com.ossez.wechat.common.redis.WxRedisOps;
import com.ossez.wechat.oa.config.WxMpHostConfig; import com.ossez.wechat.oa.config.WxMpHostConfig;
import com.ossez.wechat.oa.config.ConfigStorage;
import com.ossez.wechat.oa.config.DefaultConfigStorage; import com.ossez.wechat.oa.config.DefaultConfigStorage;
import com.ossez.wechat.oa.config.impl.RedisConfigStorage; import com.ossez.wechat.oa.config.impl.RedisConfigStorage;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -35,7 +33,7 @@ import java.util.Set;
@Slf4j @Slf4j
@Configuration @Configuration
@RequiredArgsConstructor @RequiredArgsConstructor
public class WxMpStorageAutoConfiguration { public class WeChatStorageAutoConfiguration {
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
private final WeChatOfficialAccountProperties wxMpProperties; private final WeChatOfficialAccountProperties wxMpProperties;
@ -43,10 +41,10 @@ public class WxMpStorageAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(ConfigStorage.class) @ConditionalOnMissingBean(ConfigStorage.class)
public ConfigStorage wxMpConfigStorage() { public ConfigStorage wxMpConfigStorage() {
StorageType type = wxMpProperties.getConfigStorage().getType(); StorageCategory type = wxMpProperties.getConfigStorage().getType();
ConfigStorage config; ConfigStorage config;
switch (type) { switch (type) {
case RedisTemplate: case REDIS:
config = redisTemplateConfigStorage(); config = redisTemplateConfigStorage();
break; break;
default: default:

View File

@ -1,17 +0,0 @@
package com.ossez.wechat.demo.config;
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* .
*
* @author someone
*/
@Configuration
@EnableConfigurationProperties(WeChatOfficialAccountProperties.class)
@Import({ WxMpStorageAutoConfiguration.class, WxMpServiceAutoConfiguration.class })
public class WxMpAutoConfiguration {
}

View File

@ -1,30 +0,0 @@
package com.ossez.wechat.demo.config;
import com.ossez.wechat.demo.common.enums.HttpClientType;
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatOfficialAccountServiceOkHttp;
import com.ossez.wechat.oa.config.ConfigStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 微信公众号相关服务自动注册.
*
* @author someone
*/
@Configuration
public class WxMpServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public WeChatOfficialAccountService weChatOfficialAccountService(ConfigStorage configStorage, WeChatOfficialAccountProperties wxMpProperties) {
HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
WeChatOfficialAccountService weChatOfficialAccountService = new WeChatOfficialAccountServiceOkHttp();
weChatOfficialAccountService.setWxMpConfigStorage(configStorage);
return weChatOfficialAccountService;
}
}

View File

@ -40,13 +40,7 @@ public class WeChatController {
@GetMapping("/token") @GetMapping("/token")
@ResponseBody @ResponseBody
public String getAccessToken() throws WxErrorException { public String getAccessToken() throws WxErrorException {
return weChatOfficialAccountService.getAccessToken(true);
weChatOfficialAccountService.getAccessToken(true);
return "ss" ;
} }

View File

@ -1,8 +1,8 @@
package com.ossez.wechat.demo.properties; package com.ossez.wechat.demo.properties;
import com.ossez.wechat.demo.common.enums.HttpClientType; import com.ossez.wechat.demo.common.enums.HttpClientCategory;
import com.ossez.wechat.demo.common.enums.StorageType; import com.ossez.wechat.demo.common.enums.StorageCategory;
import com.ossez.wechat.demo.model.entity.WeChatHost; import com.ossez.wechat.demo.model.entity.WeChatHost;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
@ -10,7 +10,7 @@ import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.io.Serializable; import java.io.Serializable;
import static com.ossez.wechat.demo.common.enums.StorageType.Memory; import static com.ossez.wechat.demo.common.enums.StorageCategory.MEM;
/** /**
* WeChat Official Account Config * WeChat Official Account Config
@ -37,7 +37,7 @@ public class WeChatOfficialAccountProperties {
/** /**
* 存储类型. * 存储类型.
*/ */
private StorageType type = Memory; private StorageCategory type = MEM;
/** /**
* 指定key前缀. * 指定key前缀.
@ -53,7 +53,7 @@ public class WeChatOfficialAccountProperties {
/** /**
* http客户端类型. * http客户端类型.
*/ */
private HttpClientType httpClientType = HttpClientType.OK_HTTP; private HttpClientCategory httpClientCategory = HttpClientCategory.OK_HTTP;
/** /**
* http代理主机. * http代理主机.

View File

@ -48,7 +48,7 @@ spring.redis.port=6379
# KEY # KEY
wechat.official-account.app-id = wx637b82a7f94123ef wechat.official-account.app-id = wx637b82a7f94123ef
wechat.official-account.secret = 343cecbc44d69e45367a65cc9b4d3925+ wechat.official-account.secret = 343cecbc44d69e45367a65cc9b4d3925
wechat.official-account.token = 0b01a700891d4a2f93a4323771c32455 wechat.official-account.token = 0b01a700891d4a2f93a4323771c32455
wechat.official-account.aes-key = aesKey wechat.official-account.aes-key = aesKey