How SpringBoot Auto‑Configuration Works and Build Your Own Logging Starter

This article explains SpringBoot’s auto‑configuration mechanism, walks through the DispatcherServlet example, and demonstrates how to build a custom logging starter—including annotation, properties class, AOP aspect, auto‑configuration class, Maven packaging, and usage—showcasing the efficiency and reusability benefits for backend developers.

JD Cloud Developers
JD Cloud Developers
JD Cloud Developers
How SpringBoot Auto‑Configuration Works and Build Your Own Logging Starter

1. What is SpringBoot Auto‑Configuration

SpringBoot is a Java framework built on Spring that lets developers quickly set up a development environment by importing starters, which automatically configure common components such as Spring MVC, Dubbo, MyBatis, Redis, etc.

2. SpringBoot Starter Auto‑Configuration Example

Using spring-boot-starter-web, developers no longer need to manually configure DispatcherServlet and Tomcat; the starter brings the necessary beans automatically.

3. Source Code Analysis of DispatcherServlet Auto‑Configuration

3.1 @EnableConfigurationProperties(WebMvcProperties.class)

Enables @ConfigurationProperties on WebMvcProperties, binding properties prefixed with spring.mvc from the application file.

3.2 @Conditional({DefaultDispatcherServletCondition.class})

Loads the bean only when the specified condition is met; the condition interface is org.springframework.context.annotation.Condition.

public interface Condition {
    boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);
}

3.3 @ConditionalOnClass

The condition is satisfied when a given class exists on the classpath; internally it uses Class.forName to check.

protected static Class<?> resolve(String className, ClassLoader classLoader) throws ClassNotFoundException {
    return classLoader != null ? Class.forName(className, false, classLoader) : Class.forName(className);
}

3.4 Summary

The auto‑configuration process involves reading configuration properties, evaluating conditional annotations, and loading the auto‑configuration class listed in META-INF/spring.factories.

4. Building a Custom Logging Starter

4.1 Define @PrintLog annotation

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrintLog {}

4.2 LogProperties class

// Bind properties prefixed with "log.switch"
@ConfigurationProperties("log.switch")
public class LogProperties {
    private Boolean enabled = false;
    private Boolean printIp = false;
    private Boolean printUrl = false;
}

4.3 LogAspect (AOP advice)

@Aspect
public class LogAspect {
    private static final Log LOGGER = LogFactory.getLog(LogAspect.class);
    private LogProperties logProperties;

    @Pointcut("@annotation(com.zl.annotation.PrintLog)")
    public void Log(){}

    @Around("Log()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // retrieve request, log URL, IP, method, parameters, result
        // ... (omitted for brevity)
        return result;
    }
}

4.4 LogAutoConfigure

@Configuration
@EnableConfigurationProperties({LogProperties.class})
@ConditionalOnProperty(prefix = "log.switch", name = "enabled", havingValue = "true")
public class LogAutoConfigure {
    @Bean
    @ConditionalOnClass(Advice.class)
    public LogAspect webLogAspect(LogProperties logProperties){
        return new LogAspect(logProperties);
    }
}

The starter is registered in META-INF/spring.factories:

META-INF/spring.factories=\
  com.zl.autoConfigure.LogAutoConfigure

4.5 Usage

Add the starter dependency, configure log.switch in application.yml, annotate controller methods with @PrintLog, and run the application. The console will show URL, IP, method name, parameters, and return value.

5. Conclusion

SpringBoot auto‑configuration provides “out‑of‑the‑box” convenience, dramatically improving development efficiency and enabling reusable modules. By creating a custom starter, developers can encapsulate cross‑cutting concerns such as logging, making them easy to share across projects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentloggingSpringBootauto-configurationCustom Starter
JD Cloud Developers
Written by

JD Cloud Developers

JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.