Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	README_EN.md
#	pom.xml
  • Loading branch information
chentianming11 committed Aug 27, 2021
2 parents d4548a5 + d43a90a commit 165af17
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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 {
Expand All @@ -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);
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -54,14 +58,49 @@ 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);
}
}
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<BeanDefinitionHolder> beanDefinitions) {
GenericBeanDefinition definition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Person> getPerson(@Query("id") Long id);
}

0 comments on commit 165af17

Please sign in to comment.