优化公共starter的bean加载方式,避免依赖的工程因包路径不一致而导致加载不了的情况

This commit is contained in:
zlt
2020-03-24 11:08:56 +08:00
parent 5ab0d63c2d
commit cf63212f94
15 changed files with 24 additions and 65 deletions
@@ -1,5 +1,6 @@
package com.central.search.annotation; package com.central.search.annotation;
import com.central.search.client.feign.fallback.SearchServiceFallbackFactory;
import com.central.search.client.service.impl.QueryServiceImpl; import com.central.search.client.service.impl.QueryServiceImpl;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@@ -16,8 +17,8 @@ import java.lang.annotation.Target;
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@EnableFeignClients @EnableFeignClients(basePackages = "com.central")
@Import(QueryServiceImpl.class) @Import({SearchServiceFallbackFactory.class, QueryServiceImpl.class})
public @interface EnableSearchClient { public @interface EnableSearchClient {
} }
@@ -5,7 +5,6 @@ import com.central.common.model.PageResult;
import com.central.search.client.feign.SearchService; import com.central.search.client.feign.SearchService;
import feign.hystrix.FallbackFactory; import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/** /**
* searchService降级工场 * searchService降级工场
@@ -13,7 +12,6 @@ import org.springframework.stereotype.Component;
* @author zlt * @author zlt
*/ */
@Slf4j @Slf4j
@Component
public class SearchServiceFallbackFactory implements FallbackFactory<SearchService> { public class SearchServiceFallbackFactory implements FallbackFactory<SearchService> {
@Override @Override
public SearchService create(Throwable throwable) { public SearchService create(Throwable throwable) {
@@ -1,38 +0,0 @@
package com.central.oauth2.common.config;
import com.central.oauth2.common.store.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* token存储配置
*
* @author zlt
*/
@Configuration
public class TokenStoreConfig {
@Configuration
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "db")
@Import(AuthDbTokenStore.class)
public class JdbcTokenConfig {
}
@Configuration
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true)
@Import(AuthRedisTokenStore.class)
public class RedisTokenConfig {
}
@Configuration
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "authJwt")
@Import(AuthJwtTokenStore.class)
public class AuthJwtTokenConfig {
}
@Configuration
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "resJwt")
@Import(ResJwtTokenStore.class)
public class ResJwtTokenConfig {
}
}
@@ -1,6 +1,7 @@
package com.central.oauth2.common.store; package com.central.oauth2.common.store;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
@@ -13,8 +14,8 @@ import javax.sql.DataSource;
* @author zlt * @author zlt
* @date 2018/7/24 16:23 * @date 2018/7/24 16:23
*/ */
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "db")
public class AuthDbTokenStore { public class AuthDbTokenStore {
@Autowired @Autowired
private DataSource dataSource; private DataSource dataSource;
@@ -2,6 +2,7 @@ package com.central.oauth2.common.store;
import com.central.common.model.SysUser; import com.central.common.model.SysUser;
import com.central.oauth2.common.converter.CustomUserAuthenticationConverter; import com.central.oauth2.common.converter.CustomUserAuthenticationConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.bootstrap.encrypt.KeyProperties; import org.springframework.cloud.bootstrap.encrypt.KeyProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
@@ -23,6 +24,7 @@ import java.util.Map;
* @author zlt * @author zlt
* @date 2018/7/24 16:21 * @date 2018/7/24 16:21
*/ */
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "authJwt")
public class AuthJwtTokenStore { public class AuthJwtTokenStore {
@Bean("keyProp") @Bean("keyProp")
@@ -2,6 +2,7 @@ package com.central.oauth2.common.store;
import com.central.oauth2.common.properties.SecurityProperties; import com.central.oauth2.common.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.TokenStore;
@@ -13,6 +14,7 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
* @author zlt * @author zlt
* @date 2018/7/25 9:36 * @date 2018/7/25 9:36
*/ */
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true)
public class AuthRedisTokenStore { public class AuthRedisTokenStore {
@Autowired @Autowired
private RedisConnectionFactory connectionFactory; private RedisConnectionFactory connectionFactory;
@@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil;
import com.central.common.constant.SecurityConstants; import com.central.common.constant.SecurityConstants;
import com.central.oauth2.common.converter.CustomUserAuthenticationConverter; import com.central.oauth2.common.converter.CustomUserAuthenticationConverter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties; import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
@@ -30,6 +31,7 @@ import java.util.stream.Collectors;
* @author zlt * @author zlt
* @date 2018/8/20 9:25 * @date 2018/8/20 9:25
*/ */
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "resJwt")
public class ResJwtTokenStore { public class ResJwtTokenStore {
@Autowired @Autowired
private ResourceServerProperties resource; private ResourceServerProperties resource;
@@ -1,2 +1,6 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.central.oauth2.common.config.SecurityPropertiesConfig com.central.oauth2.common.config.SecurityPropertiesConfig,\
com.central.oauth2.common.store.AuthDbTokenStore,\
com.central.oauth2.common.store.AuthRedisTokenStore,\
com.central.oauth2.common.store.AuthJwtTokenStore,\
com.central.oauth2.common.store.ResJwtTokenStore
@@ -5,7 +5,6 @@ import com.central.common.model.LoginAppUser;
import com.central.common.model.SysUser; import com.central.common.model.SysUser;
import feign.hystrix.FallbackFactory; import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/** /**
* userService降级工场 * userService降级工场
@@ -14,7 +13,6 @@ import org.springframework.stereotype.Component;
* @date 2019/1/18 * @date 2019/1/18
*/ */
@Slf4j @Slf4j
@Component
public class UserServiceFallbackFactory implements FallbackFactory<UserService> { public class UserServiceFallbackFactory implements FallbackFactory<UserService> {
@Override @Override
public UserService create(Throwable throwable) { public UserService create(Throwable throwable) {
@@ -1,2 +1,5 @@
org.springframework.context.ApplicationContextInitializer=\ org.springframework.context.ApplicationContextInitializer=\
com.central.common.config.BannerInitializer com.central.common.config.BannerInitializer
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.central.common.feign.fallback.UserServiceFallbackFactory
@@ -5,7 +5,6 @@ import com.central.common.redis.template.RedisRepository;
import com.central.common.redis.util.RedisObjectSerializer; import com.central.common.redis.util.RedisObjectSerializer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -53,18 +52,6 @@ public class RedisAutoConfigure {
return redisTemplate; return redisTemplate;
} }
/**
* Redis repository redis repository.
*
* @param redisTemplate the redis template
* @return the redis repository
*/
@Bean
@ConditionalOnMissingBean
public RedisRepository redisRepository(RedisTemplate<String, Object> redisTemplate) {
return new RedisRepository(redisTemplate);
}
@Bean(name = "cacheManager") @Bean(name = "cacheManager")
@Primary @Primary
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
@@ -20,7 +20,6 @@ import java.util.concurrent.TimeUnit;
* @date 2018/5/29 14:16 * @date 2018/5/29 14:16
*/ */
@Slf4j @Slf4j
@Component
public class RedisDistributedLock extends AbstractDistributedLock { public class RedisDistributedLock extends AbstractDistributedLock {
@Autowired @Autowired
private RedisTemplate<String, Object> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
@@ -1,2 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.central.common.redis.RedisAutoConfigure com.central.common.redis.RedisAutoConfigure,\
com.central.common.redis.lock.RedisDistributedLock,\
com.central.common.redis.template.RedisRepository
@@ -1,7 +1,6 @@
package com.central.common.swagger2; package com.central.common.swagger2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration; import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration;
@@ -10,7 +9,6 @@ import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfi
* @author zlt * @author zlt
* @date 2018/11/18 9:20 * @date 2018/11/18 9:20
*/ */
@Configuration
@ConditionalOnProperty(name = "zlt.swagger.enabled", matchIfMissing = true) @ConditionalOnProperty(name = "zlt.swagger.enabled", matchIfMissing = true)
@Import({ @Import({
Swagger2DocumentationConfiguration.class Swagger2DocumentationConfiguration.class
@@ -11,7 +11,7 @@ spring.redis.timeout=5000
##### elasticsearch配置 ##### elasticsearch配置
zlt.elasticsearch.cluster-name=my-es zlt.elasticsearch.cluster-name=my-es
zlt.elasticsearch.cluster-nodes=192.168.28.130 zlt.elasticsearch.cluster-nodes=106.12.138.245
##### sentinel配置 ##### sentinel配置
zlt.sentinel.dashboard=192.168.28.130:6999 zlt.sentinel.dashboard=192.168.28.130:6999