SpirngBoot自动装配原理之自己动手实现一个HelloWorld自动装配

Spring Boot 自动装配

在 Spring Boot 场景下,基于约定大于配置的原则,实现 Spring 组件自动装配的目的。

底层装配技术 :

Spring 模式注解装配 @Configuration

Spring 模块装配 @Enable

Spring 条件装配装配 @Conditional

Spring 工厂加载机制

SpringBoot自动装配的实现类: SpringFactoriesLoader.class 

相关配置资源目录和文件: META-INF/spring.factories

自动装配实现例子:

实现HelloWorld自动装配

实现方法:

1)激活自动装配  @EnableAutoConfiguration

2)实现自动装配 - XXXAutoConfiguration

3)配置自动装配实现 - META-INF/spring.factories

#  @Enable 模块加载流程: @EnableHelloWorld -> HelloWorldImportSelector -> HelloWorldConfiguration - > helloWorld

项目结构展示:

代码实现:

SpringBootApplication.class 

@EnableAutoConfiguration
public class SpringBootApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = new SpringApplicationBuilder(SpringBootApplication.class)
				.web(WebApplicationType.NONE)
				.run(args);


        String helloWorld = context.getBean("helloWorld", String.class);
        System.out.println(helloWorld);

    }

}

HelloWorldAutoConfiguration .class 


@Configuration
@EnableHelloWorld
@SystemPropertiesConditional(name = "name",value = "77")
public class HelloWorldAutoConfiguration {



}

@interface EnableHelloWorld  

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {

}

HelloWorldImportSelector.class 

public class HelloWorldImportSelector implements ImportSelector {


    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        System.out.println("******进入selector 选择满足的Configuration!!");
        return new String[]{HelloWorldConfiguration.class.getName()};
    }


}

HelloWorldConfiguration.class 

@Configuration
public class HelloWorldConfiguration {

    @Bean
    public String helloWorld(){
        return "helloWorldConfiguration";
    }


}

@interface SystemPropertiesConditional 

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnSystemPropertiesCondition.class)
public @interface SystemPropertiesConditional {

    String name();

    String value();

}

OnSystemPropertiesCondition.class 

public class OnSystemPropertiesCondition implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {


        MultiValueMap<String, Object> allAnnotationAttributes = annotatedTypeMetadata.getAllAnnotationAttributes(SystemPropertiesConditional.class.getName());
        
        String name = String.valueOf(allAnnotationAttributes.get("name"));

        String value = String.valueOf(allAnnotationAttributes.get("value"));

        System.out.println("条件判断" + name +"  "+ value);

        System.out.println("****条件满足--启用此AutoConfiguration");

        return true;



    }
}

spring.factories 

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.edu.mju.driver.configuration.HelloWorldAutoConfiguration

启动SpirngBootApplication显示结果:

Git下载:https://github.com/xiezengcheng/dirver

猜你喜欢

转载自blog.csdn.net/qq_41750725/article/details/97566478
今日推荐