当前位置: 首页 > news >正文

南坪网站建设哪里好企业网站开发报价

南坪网站建设哪里好,企业网站开发报价,网站怎么做丰富的tag标签页,wordpress简洁模板下载前言Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。Conditional的源码定义://此注解可以标注在类和方法上 Target({ElementType.TYPE, ElementType.METHOD}) Retention(RetentionPolicy.RUNTI…
  1. 前言

@Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。

@Conditional的源码定义:

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

从代码中可以看到,需要传入一个Class数组,并且需要继承Condition接口类:

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

Condition是个接口类,需要实现matches方法,返回true则注入bean,false则不注入。


  1. 举个栗子

首先,创建Person类:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Person {private String name;private Integer age;
}

定义一个BeanConfig类,用于配置两个Person实例并注入,一个是比尔盖茨,一个是林纳斯。

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

接着定义一个测试类进行验证这两个Bean是否注入成功。

public class ConditionalTest {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);@Testpublic void beanTest(){Map<String, Person> map = applicationContext.getBeansOfType(Person.class);System.out.println(map);}
}

运行,输出结果是这样的,两个Person实例被注入进容器。


2.1 条件注入bean

如果想根据当前操作系统来注入Person实例,windows下注入bill,linux下注入linus,怎么实现呢?

这就需要用到@Conditional注解了,前言中提到,需要实现Condition接口,并重写方法来自定义match规则。

创建WindowsCondition类

public class WindowsCondition implements Condition {/*** @param conditionContext:判断条件能使用的上下文环境* @param annotatedTypeMetadata:注解所在位置的注释信息* */@Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {//获取ioc使用的beanFactoryConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();//获取类加载器ClassLoader classLoader = conditionContext.getClassLoader();//获取当前环境信息Environment environment = conditionContext.getEnvironment();//获取bean定义的注册类BeanDefinitionRegistry registry = conditionContext.getRegistry();//获得当前系统名String property = environment.getProperty("os.name");//包含Windows则说明是windows系统,返回trueif (property.contains("Windows")){return true;}return false;}
}

matches方法的两个参数的意思在注释中讲述了,值得一提的是,conditionContext提供了多种方法,方便获取各种信息,也是SpringBoot中 @ConditonalOnXX注解多样扩展的基础。

创建LinuxCondition类

public class LinuxCondition implements Condition {@Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {Environment environment = conditionContext.getEnvironment();String property = environment.getProperty("os.name");if (property.contains("Linux")){return true;}return false;}
}

接着就是使用这两个类了,因为此注解可以标注在方法上和类上,所以分开测试:


2.2 标注在方法上

修改BeanConfig:

@Configuration
public class BeanConfig {//只有一个类时,大括号可以省略//如果WindowsCondition的实现方法返回true,则注入这个bean    @Conditional({WindowsCondition.class})@Bean(name = "bill")public Person person1(){return new Person("Bill Gates",62);}//如果LinuxCondition的实现方法返回true,则注入这个bean@Conditional({LinuxCondition.class})@Bean("linus")public Person person2(){return new Person("Linus",48);}
}

修改测试方法,使其可以打印当前系统名:

    @Testpublic void test1(){String osName = applicationContext.getEnvironment().getProperty("os.name");System.out.println("当前系统为:" + osName);Map<String, Person> map = applicationContext.getBeansOfType(Person.class);System.out.println(map);}

运行结果如下:

我是运行在windows上的所以只注入了bill,嗯,没毛病。

接着实验linux下的情况,不能运行在linux下,但可以修改运行时参数:

修改后启动测试方法:

一个方法只能注入一个bean实例,所以@Conditional标注在方法上只能控制一个bean实例是否注入。


2.3 标注在类上

一个类中可以注入很多实例,@Conditional标注在类上就决定了一批bean是否注入。

我们试一下,将BeanConfig改写,修改后的BeanConfig如下:

如果WindowsCondition返回true,则两个Person实例将被注入。

(注意:上一个测试将os.name改为linux,这是我将把这个参数去掉),

@Conditional({WindowsCondition.class})
@Configuration
public class BeanConfig {@Bean(name = "bill")public Person person1(){return new Person("Bill Gates",62);}@Bean("linus")public Person person2(){return new Person("Linus",48);}
}

结果两个实例都被注入:

如果将类上的WindowsCondition.class改为LinuxCondition.class,结果应该可以猜到:

在windows环境下运行,则注入的结果就是空的,类中所有bean都没有注入。


  1. 多个条件类

前言中说,@Conditional注解传入的是一个Class数组,存在多种条件类的情况。

这种情况貌似判断难度加深了,测试一波,新增新的条件类,实现的matches返回false(这种写死返回false的方法纯属测试用,没有实际意义)

public class ObstinateCondition implements Condition {@Overridepublic boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {return false;}
}

BeanConfig修改一下:

@Conditional({WindowsCondition.class,ObstinateCondition.class})
@Configuration
public class BeanConfig {@Bean(name = "bill")public Person person1(){return new Person("Bill Gates",62);}@Bean("linus")public Person person2(){return new Person("Linus",48);}
}

结果:

现在如果将ObstinateCondition的matches方法返回值改成true,两个bean就被注入进容器:

结论得:

第一个条件类实现的方法返回true,第二个返回false,则结果false,不注入进容器。

第一个条件类实现的方法返回true,第二个返回true,则结果true,注入进容器中。

http://www.yayakq.cn/news/330/

相关文章:

  • wordpress 子站点提供网站建设框架
  • 郑州建立一个网站需要哪些互联网推广是什么意思
  • 网站建设通报淘宝运营培训班
  • 建设银行网站链接石家庄做网站的公司
  • 做网站有哪些好公司建设网站聊天室
  • wordpress 微信关注插件郑州众志seo
  • 如何提高网站访客数济南优化网络营销
  • 男女做羞羞视频网站网站建设费是否应当入无形资产
  • 山东省建设协会网站首页wordpress免登录支付宝
  • 网站交易模块怎么做成都 网站建设 公司哪家好
  • 淘客网站推广怎么做app是什么意思怎么用
  • 高校网站建设需求单公司名称起名大全免费
  • 长春 美容 网站建设响应式网页设计项目
  • 免费建自己域名的网站网站建设捌金手指下拉十七
  • 随州学做网站电子商务网站建设步
  • 房地产企业网站开发注册公司一般需要多少钱
  • 高端网站建设行业网站从哪里找的
  • 免费制作的企业网站苏州网站建设永阳网络
  • 大学生做兼职上什么网站好南京网站设计制作公司排名
  • 内网网站建设的亮点特点用asp做网站题目
  • 金坛做网站哪家好天津网站大全
  • 自助式建网站企信查官网
  • 内蒙古建设厅建筑网站做网站需要哪些
  • 网页网站开发大概多少钱seo先上排名后收费
  • wordpress php7拓展aso优化排名违法吗
  • 网站维护机构功能型网站开发
  • 国外室内设计网站大全网站wordpress首页锚点
  • 织梦做的的网站首页显示空白长沙企业网页设计哪家专业
  • 做外贸生意上国外网站灯会公司
  • 广东网站建设开发钱多网站