diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/AutoConfiguredRetrofitScannerRegistrar.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/AutoConfiguredRetrofitScannerRegistrar.java index 2fb72d0..00902d4 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/AutoConfiguredRetrofitScannerRegistrar.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/AutoConfiguredRetrofitScannerRegistrar.java @@ -9,8 +9,10 @@ import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.boot.autoconfigure.AutoConfigurationPackages; +import org.springframework.context.EnvironmentAware; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; @@ -23,7 +25,7 @@ * * @author 陈添明 */ -public class AutoConfiguredRetrofitScannerRegistrar implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware { +public class AutoConfiguredRetrofitScannerRegistrar implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware { private static final Logger logger = LoggerFactory.getLogger(AutoConfiguredRetrofitScannerRegistrar.class); @@ -32,6 +34,7 @@ public class AutoConfiguredRetrofitScannerRegistrar implements BeanFactoryAware, private ResourceLoader resourceLoader; private ClassLoader classLoader; + private Environment environment; @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { @@ -54,7 +57,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B } // Scan the @RetrofitClient annotated interface under the specified path and register it to the BeanDefinitionRegistry - ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader); + ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader, environment); if (resourceLoader != null) { scanner.setResourceLoader(resourceLoader); } @@ -75,4 +78,9 @@ public void setResourceLoader(ResourceLoader resourceLoader) { public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } } diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java index 7e4300c..f65cf7f 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/ClassPathRetrofitClientScanner.java @@ -8,8 +8,10 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; +import org.springframework.core.env.Environment; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; import java.util.Arrays; import java.util.Objects; @@ -23,10 +25,12 @@ public class ClassPathRetrofitClientScanner extends ClassPathBeanDefinitionScann private final ClassLoader classLoader; private final static Logger logger = LoggerFactory.getLogger(ClassPathRetrofitClientScanner.class); + private final Environment environment; - public ClassPathRetrofitClientScanner(BeanDefinitionRegistry registry, ClassLoader classLoader) { + public ClassPathRetrofitClientScanner(BeanDefinitionRegistry registry, ClassLoader classLoader, Environment environment) { super(registry, false); this.classLoader = classLoader; + this.environment = environment; } public void registerFilters() { @@ -54,7 +58,8 @@ protected boolean isCandidateComponent( Class target = ClassUtils.forName( beanDefinition.getMetadata().getClassName(), classLoader); - return !target.isAnnotation(); + + return !target.isAnnotation() && (legalBaseUrl(target) || legalServiceId(target)); } catch (Exception ex) { logger.error("load class exception:", ex); } @@ -62,6 +67,40 @@ protected boolean isCandidateComponent( return false; } + private boolean legalBaseUrl(Class target) { + final RetrofitClient retrofitClient = target.getAnnotation(RetrofitClient.class); + final String baseUrl = retrofitClient.baseUrl(); + if (StringUtils.isEmpty(baseUrl)) { + logger.warn("No config baseUrl! interface={}", target); + return false; + } + + try { + environment.resolveRequiredPlaceholders(baseUrl); + } catch (Exception e) { + logger.warn("No config baseUrl! interface={}", target); + return false; + } + return true; + } + + private boolean legalServiceId(Class target) { + final RetrofitClient retrofitClient = target.getAnnotation(RetrofitClient.class); + final String serviceId = retrofitClient.serviceId(); + if (StringUtils.isEmpty(serviceId)) { + logger.warn("No config serviceId! interface={}", target); + return false; + } + + try { + environment.resolveRequiredPlaceholders(serviceId); + } catch (Exception e) { + logger.warn("No config serviceId! interface={}", target); + return false; + } + return true; + } + private void processBeanDefinitions(Set beanDefinitions) { GenericBeanDefinition definition; diff --git a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitClientScannerRegistrar.java b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitClientScannerRegistrar.java index bc846ed..4ac3d41 100644 --- a/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitClientScannerRegistrar.java +++ b/src/main/java/com/github/lianjiatech/retrofit/spring/boot/core/RetrofitClientScannerRegistrar.java @@ -3,9 +3,11 @@ import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitScan; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.EnvironmentAware; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.Assert; @@ -19,12 +21,14 @@ /** * @author 陈添明 */ -public class RetrofitClientScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware { +public class RetrofitClientScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware { private ResourceLoader resourceLoader; private ClassLoader classLoader; + private Environment environment; + @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { AnnotationAttributes attributes = AnnotationAttributes @@ -33,7 +37,7 @@ public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionR return; } // Scan the @RetrofitClient annotated interface under the specified path and register it to the BeanDefinitionRegistry - ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader); + ClassPathRetrofitClientScanner scanner = new ClassPathRetrofitClientScanner(registry, classLoader, environment); if (resourceLoader != null) { scanner.setResourceLoader(resourceLoader); } @@ -78,4 +82,9 @@ public void setResourceLoader(ResourceLoader resourceLoader) { public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } } diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/IllegalBaseUrlTestApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/IllegalBaseUrlTestApi.java new file mode 100644 index 0000000..6b187bd --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/IllegalBaseUrlTestApi.java @@ -0,0 +1,19 @@ +package com.github.lianjiatech.retrofit.spring.boot.test.http; + +import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; +import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person; +import com.github.lianjiatech.retrofit.spring.boot.test.entity.Result; +import retrofit2.converter.gson.GsonConverterFactory; +import retrofit2.converter.jaxb.JaxbConverterFactory; +import retrofit2.http.GET; +import retrofit2.http.Query; + +/** + * @author 陈添明 + */ +@RetrofitClient(baseUrl = "${not.baseUrl}", converterFactories = {GsonConverterFactory.class, JaxbConverterFactory.class}) +public interface IllegalBaseUrlTestApi { + + @GET("person") + Result getPerson(@Query("id") Long id); +}