spring boot自动装配功能的实现

spring boot自动装配功能的实现,第1张

spring boot自动装配功能的实现 1. 在启动类上使用@EnableAutoConfiguration注解,激活spring boot的自动化配置
@EnableAutoConfiguration
public class AutoConfigBoot {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(AutoConfigBoot.class)
                .web(WebApplicationType.NONE)
                .run(args);
        String hello = context.getBean(String.class);
        System.out.println(hello);
        context.close();
    }
}
2. 定义EnableXXX注解,在这个注解上使用@import(XXX)来导入需要的configuration
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@import(HelloWorldimportSelector.class)
//@import(HelloWorldConfiguation.class)
public @interface EnableHelloWorld {
}

其中导入需要的配置类一共有两种方式

注解方式编程方式

注解方式:实现较为简单,直接导入标记了@Configuration注解的类即可(上述代码中注释掉的 //@import(HelloWorldConfiguration.class) 就是使用了这种方式)

编程方式:实现较为复杂,需要创建一个类实现importSelector接口

public class HelloWorldimportSelector implements importSelector {
    @Override
    public String[] selectimports(Annotationmetadata importingClassmetadata) {
        System.out.println("使用了Selector-------");
        return new String[]{HelloWorldConfiguation.class.getName()};
    }
}

Note:重载的 selectimports() 方法返回的是要导入的configuration类的类名,是一个字符串数组。

3. 考虑是否使用条件装配

实现方式也是两种:

注解方式编码方式

注解方式:使用较为简单,直接在自动装配的类上标注@Profile(“xx”)即可进行自动装配

编码方式:使用较为复杂,先要创建一个注解(标注有@conditional(xxx.class))和一个类(叫xxx,该类实现了Condition)

//注解
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Conditional(OnSystemPropertyCondition.class)
public @interface ConditionOnSystemProperty {

    String name();

    String value();

}

//类
public class OnSystemPropertyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypemetadata metadata) {
        Map attributes = metadata.getAnnotationAttributes(ConditionOnSystemProperty.class.getName());
        String name = String.valueOf(attributes.get("name"));//拿到注解中name的值
        String value = String.valueOf(attributes.get("value"));//拿到注解中value的值
        String systemValue = System.getProperty(name);
        return systemValue.equals(value);
    }
}

Note:matcher()方法返回false表示条件不满足,不会进行装配;matcher()返回true表示条件满足,会进行装配。@ConditionOnSystemProperty注解标注在需要进行条件判断的类上,要和@Configuration一起用。

@Configuration
@ConditionOnSystemProperty(name = "user.name",value = "cm")
public class HelloWorldConfiguation {
    @Bean
    public String helloWorld(){
        return "Hello,World 2022";
    }
}
4.在resources目录下创建一个meta-INF文件夹,并创建Spring.factories文件

spring.factories文件的内容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.peterpen.config.HelloWorldAutoConfiguation

目录结构:

并且创建一个配置类(即为上面的HelloWorldAutoConfiguration)

@Configuration
@EnableHelloWorld
@ConditionOnSystemProperty(name = "user.name",value = "54733")
public class HelloWorldAutoConfiguation {

    @Bean
    public Spring spring(){
        return new Spring("cm");
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713016.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存