首页 > 应用开发 > Java > SpringBoot自动配置原理解析
来源:京东云开发者
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>${spring-boot.version}</version> </dependency>
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration在这个类中存在有一个静态内部类:
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration.DispatcherServletConfiguration

public interface Condition { boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);}protected static Class<?> resolve(String className, ClassLoader classLoader) throws ClassNotFoundException { return classLoader != null ? Class.forName(className, false, classLoader) : Class.forName(className);}@ConfigurationProperties@EnableConfigurationProperties
@Conditional@ConditionalOnClass
/META-INF/spring-factoriesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=自动配置类全路径名称
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface PrintLog {}//自动注入application配置文件中已log.switch开头的配置参数@ConfigurationProperties("log.switch")public class LogProperties { //是否启用打印日志功能 private Boolean enabled = false; //是否打印调用者ip private Boolean printIp = false; //是否打印调用者url private Boolean printUrl = false}
public class LogAspect {private static final Log LOGGER = LogFactory.getLog(LogAspect.class);private LogProperties logProperties;("@annotation(com.zl.annotation.PrintLog)")public void Log(){}("Log()")public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();String methodName = method.getName();//打印调用urlif (Boolean.TRUE.equals(logProperties.getPrintUrl())){LOGGER.info("URL:" + request.getRequestURL().toString());}//打印ipif (Boolean.TRUE.equals(logProperties.getPrintIp())) {LOGGER.info("IP :" + request.getRemoteAddr());}//打印方法LOGGER.info("method :" + methodName);//打印参数LOGGER.info("parameter :" + Arrays.toString(joinPoint.getArgs()));Object result = joinPoint.proceed();//打印返回结果LOGGER.info("return :" + JSON.toJSONString(result));return result;}}
@Configuration@EnableConfigurationProperties({LogProperties.class})//表示在application配置文件中必须配置log.switch.enabled = true才启动自动配置@ConditionalOnProperty(prefix = "log.switch", value = "enabled", havingValue = "true")public class LogAutoConfigure { @Bean //Advice.class是aop切面中关键的切面方法类(@Before,@After等) //程序中有Advice.class类说明需要使用切面功能,这时才加载自定义的切面类 @ConditionalOnClass(Advice.class) public LogAspect webLogAspect(LogProperties logProperties){ return new LogAspect(logProperties); }}META-INF/spring.factoriesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.zl.autoConfigure.LogAutoConfigure
maven install
<dependency> <groupId>com.zl.demo</groupId> <artifactId>LogStarter</artifactId> <version>1.0-SNAPSHOT</version></dependency>
log: switch: enabled: true //启用打印日志功能 printIp: true //打印请求ip printUrl: true //打印请求url
@RestController@RequestMapping("/test")public class HelloWorldController { @PrintLog @RequestMapping("/hello") public String helleWorld(String test){ return "hello world!"; }}com.zl.aspect.LogAspect : URL:http://localhost:8080/test/hellocom.zl.aspect.LogAspect : IP :0:0:0:0:0:0:0:1com.zl.aspect.LogAspect : method :helleWorldcom.zl.aspect.LogAspect : parameter :[test]com.zl.aspect.LogAspect : return :"hello world!"
-end-
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70024924/viewspace-2992543/ ,如需转载,请注明出处,否则将追究法律责任。