- 说明
- 简化使用者的操作,不需要在在启动类上手动加入扫描 
@SpringBootApplication(scanBasePackages="com.xx.xx.**") 
 - 简化使用者的操作,不需要在在启动类上手动加入扫描 
 - 参考
 - 验证
- 接口注入版(手动注入对于复杂的项目更友好)
- 新建一个自动注入的注解
 - 使用方使用
- 启动类加入 EnableConfig 注解 ```java
 
 
 
 - 接口注入版(手动注入对于复杂的项目更友好)
 
注入注解
package com.databstech.doc.standalone.annotation;
import com.databstech.doc.standalone.bean.SwaggerBean; import com.databstech.doc.standalone.config.ServerConfig; import com.databstech.doc.standalone.config.SwaggerConfig; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
- @author tn
 - @ClassName EnableConfig
 - @description 自动注入spring容器注解方式
 - @date 2020-09-27 09:54 / @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented //1.把需要注入spring容器的类加入进来 @Import({ServerConfig.class, SwaggerBean.class, SwaggerConfig.class}) //2.设置扫描路径:最好是直接把本项目所有类的共有目录放进去 @ComponentScan(“com.databstech.doc.standalone.*“) public @interface EnableConfig { }
 
使用方 ↓
package com.cr.code.eco.ecoapi;
import com.databstech.doc.standalone.annotation.EnableConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
//使用了 EnableConfig就不需要在用 scanBasePackages 了 //@SpringBootApplication(scanBasePackages = {“com.cr.code.eco.ecoapi.“,”com.databstech.“}) @SpringBootApplication @EnableConfig public class EcoapiApplication {
public static void main(String[] args) {SpringApplication.run(EcoapiApplication.class, args);}
}
1. 自动注入版1. 新建一个资源目录 └─resources└─META-INF<br /> ii. 新建一个全局的配置文件 EnableAutoScanConfigurationiii. 新建配置文件 spring.factories```javaEnableAutoScanConfiguration文件内容package com.databstech.doc.standalone.scan;import com.databstech.doc.standalone.bean.SwaggerBean;import com.databstech.doc.standalone.config.ServerConfig;import com.databstech.doc.standalone.config.SwaggerConfig;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Import;/*** @author tn* @ClassName EnableScan* @description 自动扫描* @date 2020-09-27 10:17*/@ConditionalOnWebApplication#引入需要加入spring容器类,这些配置累@Import({ServerConfig.class, SwaggerBean.class, SwaggerConfig.class})#设置扫描路径:最好是直接把本项目所有类的共有目录放进去@ComponentScan("com.databstech.doc.standalone.**")public class EnableAutoScanConfiguration {}spring.factories 配置文件 内容org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.databstech.doc.standalone.scan.EnableAutoScanConfiguration# \是换行# 多个以逗号分割
- 注入第二版(不聚合<@Import>配置文件,配置文件自己使用@ComponentScan) ```java 同样构建 配置文件 但是这里直接指向了需要注入的配置类 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.databstech.apis.resultexception.handler.ControllerExceptionHandler
 
改配置类中加入 @ComponetScan 即可 @Slf4j @RestControllerAdvice @ComponentScan(basePackages = “com.databstech.apis.resultexception.handler.**”) public class ControllerExceptionHandler {}
2. 出现的错误1. 在聚合gateway时,配置的gateway全局异常无法使用```java1. 同样使用了通用的公共类进行聚合后注入 如下↓package com.databstech.doc.gateway.scan;import com.databstech.doc.gateway.config.ServerConfig;import com.databstech.doc.gateway.exception.core.ExceptionHandlerConfiguration;import com.databstech.doc.gateway.factory.DocNameRoutePredicateFactory;import com.databstech.doc.gateway.sawagger.config.SwaggerResourceConfig;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;import org.springframework.context.annotation.Import;/*** @author tn* @ClassName EnableScan* @description 自动扫描* @date 2020-09-27 10:17*/@ConditionalOnWebApplication@Import({SwaggerResourceConfig.class,DocNameRoutePredicateFactory.class,ExceptionHandlerConfiguration.class,ServerConfig.class})@ComponentScan( basePackages = "com.databstech.doc.gateway.**" )public class EnableAutoScanConfiguration {}2. 但是 ExceptionHandlerConfiguration 出来了无法使用的情况,还是抛出了默认的错误返回,没有返回我是指定的格式# 默认格式{"timestamp": "2020-09-27T12:37:41.912+00:00","path": "/api/basic/ribbon/test01","status": 500,"error": "Internal Server Error","message": "Connection refused: no further information: /192.168.0.57:1012","requestId": "031d5022-1"}我要的格式{"code": 503,"message": "Failed to handle request [GET http://192.168.2.57:200/api/basic/ribbon/test01?param=qweq]: 503 SERVICE_UNAVAILABLE \"Unable to find instance for server-basic\"","data": null,"success": false,"ts": 1601210351325}3. 最后分开注入成功,具体如下/*** @author tn* @ClassName EnableScan* @description 自动扫描* @date 2020-09-27 10:17*/@ConditionalOnWebApplication@Import({SwaggerResourceConfig.class,DocNameRoutePredicateFactory.class,ServerConfig.class})@ComponentScan( basePackages = "com.databstech.doc.gateway.**",excludeFilters= {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = ExceptionHandlerConfiguration.class)})public class EnableAutoScanConfiguration {}# spring.factoriesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.databstech.doc.gateway.scan.EnableAutoScanConfiguration,\com.databstech.doc.gateway.exception.core.ExceptionHandlerConfiguration
- 最开始配置 spring.factories 时文件位置放错了,直接放在了 resources 中,导致配置没有生效
 
