Skip to content

Commit

Permalink
未配置baseUrl忽视该接口
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Aug 5, 2021
1 parent 1a9852c commit 878e3a8
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.12</version>
<version>2.2.13</version>
</dependency>
```

Expand All @@ -63,7 +63,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.12</version>
<version>2.2.13</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.12</version>
<version>2.2.13</version>
</dependency>
```

Expand All @@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions.
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.12</version>
<version>2.2.13</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.12</version>
<version>2.2.13</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down
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,32 @@ protected boolean isCandidateComponent(
Class<?> target = ClassUtils.forName(
beanDefinition.getMetadata().getClassName(),
classLoader);
return !target.isAnnotation();

return !target.isAnnotation() && legalBaseUrl(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 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 878e3a8

Please sign in to comment.