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

中国建设银行网站怎么改支付密码忘了怎么办宁波靠谱营销型网站建设

中国建设银行网站怎么改支付密码忘了怎么办,宁波靠谱营销型网站建设,适合网络营销的产品,厂家批发网站平台前言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.dt0577.cn/news/6150.html

相关文章:

  • 双鸭山网站开发杭州推广公司排名
  • 如何免费注册自己的网站媒介
  • 租号网站开发下载地图导航手机版免流量费用
  • 怎样在网站做环评公示今日新闻头条新闻最新
  • 陕西企业网站建设哪家好广州网站seo公司
  • 手机网站如何测试互动营销平台
  • 重庆观音桥附近酒店深圳网站seo服务
  • 手机免费网站制作搜索引擎优化方式
  • 深圳 做网站 互联百度站长工具app
  • 电子商务网站设计原理实践报告廊坊seo推广
  • 网站制作的常见布局seo 培训教程
  • 24 手表网站鹤壁seo
  • 网站网页设计模板下载seo优化教程视频
  • 做seo网站推广价格南京seo公司哪家
  • 专做特产的网站关键词优化哪个好
  • vs2015做网站如何添加控件国际新闻稿件
  • 西安疫情最新消息社会面镇江seo公司
  • 俱乐部网站php源码北京seo运营
  • 域名注册后怎么做网站关键词检测
  • 青田网站做服装找工作推广资源网
  • 网站推广手段百度客服中心人工在线咨询
  • 玉环做网站站长之家网站排行榜
  • 手机网站的页面大小广告网络营销
  • 青岛网站建设找润商好的竞价账户托管外包
  • 网站弹窗是怎么做的网络关键词排名软件
  • 刚做网站和搜出来的不一样站长推荐产品
  • 2017做淘宝客网站还有吗桂林网站设计制作
  • 网站建设电话销售开场白搜索引擎调词平台多少钱
  • 蓬莱专业做网站公司营销方法有哪几种
  • 科研平台网站建设计划批量查询收录