国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

Spring注解@Conditional案例解析

2024-07-09 22:40:56
字體:
來源:轉載
供稿:網友

【1】@Conditional介紹

@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進行判斷,滿足條件給容器注冊bean。

@Conditional源碼:

//此注解可以標注在類和方法上@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME) @Documentedpublic @interface Conditional {  Class<? extends Condition>[] value();}

從代碼中可以看到,需要傳入一個Class數組,并且需要繼承Condition接口:

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

Condition是個接口,需要實現matches方法,返回true則注入bean,false則不注入。

【2】@Conditional示例

首先,創建Person類:

public class Person {   private String name;  private Integer age;   public String getName() {    return name;  }   public void setName(String name) {    this.name = name;  }   public Integer getAge() {    return age;  }   public void setAge(Integer age) {    this.age = age;  }   public Person(String name, Integer age) {    this.name = name;    this.age = age;  }   @Override  public String toString() {    return "Person{" + "name='" + name + '/'' + ", age=" + age + '}';  }}

創建MyConfig類,用于配置兩個Person實例并注入,一個是Bill Gates,一個是linus。

@Configurationpublic class MyConfig {   @Bean(name = "bill")  public Person person1(){    return new Person("Bill Gates",62);  }   @Bean("linus")  public Person person2(){    return new Person("Linus",48);  }}

寫一個測試類,測試是否注入成功

public class ConditionalTest {  AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);   @Test  public void test1(){    Map<String, Person> map = applicationContext.getBeansOfType(Person.class);    System.out.println(map);  }}/**測試結果{bill=Person{name='Bill Gates',age=62},linus=Person{name='Linus',age='48'}}*/

這是一個簡單的例子,現在問題來了,如果我想根據當前操作系統來注入Person實例,windows下注入bill,linux下注入linus,怎么實現呢?

這就需要我們用到@Conditional注解了,前言中提到,需要實現Condition接口,并重寫方法來自定義match規則。

首先,創建一個WindowsCondition類:

public class WindowsCondition implements Condition {   /**   * @param conditionContext:判斷條件能使用的上下文環境   * @param annotatedTypeMetadata:注解所在位置的注釋信息   * */  @Override  public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {    //獲取ioc使用的beanFactory    ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();    //獲取類加載器    ClassLoader classLoader = conditionContext.getClassLoader();    //獲取當前環境信息    Environment environment = conditionContext.getEnvironment();    //獲取bean定義的注冊類    BeanDefinitionRegistry registry = conditionContext.getRegistry();     //獲得當前系統名    String property = environment.getProperty("os.name");    //包含Windows則說明是windows系統,返回true    if (property.contains("Windows")){      return true;    }    return false;  }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 房产| 温州市| 肃宁县| 芜湖县| 固镇县| 汝阳县| 喀什市| 杭州市| 曲松县| 纳雍县| 嘉义县| 丰镇市| 卓资县| 凌海市| 洪湖市| 永修县| 泰安市| 龙岩市| 宁国市| 安达市| 图片| 山丹县| 龙海市| 博客| 濮阳县| 灌阳县| 贡山| 枝江市| 潢川县| 开远市| 宜良县| 和静县| 合川市| 建水县| 长丰县| 苏尼特左旗| 嘉峪关市| 荥阳市| 府谷县| 新干县| 巢湖市|