Springboot配置模式(+實例)

Springboot裝配(配置)方式

一、手動裝配
1、  模式註解裝配:@Controller、@Service、@Repository等Component註解的拓展,Configruation
2、  Enable模塊裝配,常用如下:

  • @EnableWebMvc WebMvc模式
  • @EnableTransaction Management事務管理模塊
  • @EnableCaching Caching模塊
  • @EnableAutoConfiguration 自動裝配模式
  • @EnableManagementContext Actuator管理模塊
  • @EnableEurekaServer Eureka服務器模塊
  • @EnableConfigServer 配置服務器模塊
  • 自定義@Enable模塊:
    ①Import的派生註解
    ②基於註解驅動實現,可參考@EnableWebMvc註解
    ③基於接口驅動實現,ImportSelector接口(條件判斷注入);參考@EnableCaching註解
    3、條件裝配
            定義:Bean配置的前提條件,如@Profile(底層實際用@Confitional),@Confitional(編程條件配置)
    二、手動裝配實例
    創建Springboot項目,項目結構如下所示:
    在這裏插入圖片描述
    1、Enable模式裝配
    工作原理:啓動類注入註解,註解中導入配置類Config,而配置類中注入對應的類(實現類或接口),啓動該項目時,將配置類內任何內容都能成功注入到項目中(可以是任何位置)
    (1)EnableConfiguration形式實例
    1>  配置類
@Configuration
public class HelloConfig {
    /*
    將World類注入進來
    */
    @Bean
    World world(){
        System.out.println("加載HelloConfig!");
        return new World();
    }
}

2>  World類

public class World {
    public String say(){
        System.out.println("Hello World!");
        return "Hello World";
    }
}

3>  註解類

//註解相關的配置,以下3行
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
//導入配置類
@Import({HelloConfig.class})
public @interface EnableHello {
}

4>  啓動類

@EnableHello  //注入EnableHello
//@EnableHelloSelector(isLinux = false)
@RestController
@SpringBootApplication
public class HelloApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
        System.out.println("hello,獨愛空城夢!");
    }

    @GetMapping("/test")
    public Object test(){

        return "你是認真的嗎?";
    }

}

5>  項目啓動後,結果如下所示:
在這裏插入圖片描述
(2)Selector形式實例
1>  配置類

public class HelloConfigSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        //獲取接口原數據
        Map<String, Object> metaData = annotationMetadata.getAnnotationAttributes(EnableHelloSelector.class.getName());
        Boolean isLinux = (Boolean)metaData.get("isLinux");
        //加載多個,並把所有配置初始化
        return new String[]{isLinux?HelloConfig.class.getName():HelloCOnfig2.class.getName()};
    }
}

2>  Config2配置類(因Selector需要兩個配置類,另一個)

@Configuration
public class HelloCOnfig2 {
    @Bean
    World world(){
        System.out.println("加載HelloCOnfig2222222!");
        return new World();
    }
}

3>  註解類

Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import({HelloConfigSelector.class})
public @interface EnableHelloSelector {
    boolean isLinux();
}

4>  啓動類(上述啓動類內修改)

//@EnableHello  //注入EnableHello
@EnableHelloSelector(isLinux = false)

5>  項目啓動後,結果如下所示:
在這裏插入圖片描述
2、條件裝配
1>  註解類

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Conditional({SystemCondition.class})
public @interface ConditionOnSystem {
    String value();
}

2>  Condition類

public class SystemCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //獲取接口原數據
        Map<String, Object> metaData = annotatedTypeMetadata.getAnnotationAttributes(ConditionOnSystem.class.getName());
        String  value = (String)metaData.get("value");
        return value.equals("windows");
    }
}

3>  配置類(條件判斷)

@Configuration
public class HelloCOnfig2 {
    @Bean
    World world(){
        System.out.println("加載HelloCOnfig2222222!");
        return new World();
    }
    //value=“linux”時不會加載,而value=“Windows”時會加載
    @ConditionOnSystem(value = "linux/Windows")
    @Bean(name = "world2")
    World world2(){
        System.out.println("加載condition!");
        return new World();
    }
}

4>  目啓動後,結果如下所示:
在這裏插入圖片描述
三、自動裝配
自動裝配文件spring.factories放置META-INF目錄下,不用寫註解類等
實現原則
理念:約定大於配置原則
裝配:模式裝配,@Enable註解裝配,條件裝配,基於手動裝配
步驟:激活自動裝配、實現自動裝配,配置自動裝配
實例:
(1)添加自動裝配文件spring.factories
在這裏插入圖片描述

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.config.HelloConfig

(2)註釋啓動類註解

//@EnableHello  //注入EnableHello
//@EnableHelloSelector(isLinux = false)

(3)項目啓動後,結果如下所示:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章