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

南山住房和建设局网站建网站需要什么步骤

南山住房和建设局网站,建网站需要什么步骤,打开网站弹出窗口代码,恐怖网站代码前言Conditional是Spring4新提供的注解#xff0c;它的作用是按照一定的条件进行判断#xff0c;满足条件给容器注册bean。Conditional的源码定义#xff1a;//此注解可以标注在类和方法上 Target({ElementType.TYPE, ElementType.METHOD}) Retention(RetentionPolicy.RUNTI…前言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则注入beanfalse则不注入。举个栗子首先创建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(){MapString, Person map applicationContext.getBeansOfType(Person.class);System.out.println(map);} } 运行输出结果是这样的两个Person实例被注入进容器。2.1 条件注入bean如果想根据当前操作系统来注入Person实例windows下注入billlinux下注入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 标注在方法上修改BeanConfigConfiguration public class BeanConfig {//只有一个类时大括号可以省略//如果WindowsCondition的实现方法返回true则注入这个bean Conditional({WindowsCondition.class})Bean(name bill)public Person person1(){return new Person(Bill Gates,62);}//如果LinuxCondition的实现方法返回true则注入这个beanConditional({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);MapString, 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都没有注入。多个条件类前言中说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.w-s-a.com/news/616709/

相关文章:

  • 内蒙古城乡建设部网站首页平台网站建设ppt
  • 集约化网站建设项目官方网站建设
  • 原创先锋 北京网站建设网站开发电脑内存要多少
  • 婚恋网站建设项目创业计划书网站建设 食品
  • 免费建网站代码查询做导员的网站
  • 做网站的软件电子可以看女人不易做网站
  • 学校响应式网站模板下载仙居住房和城乡建设规划局网站
  • 推广网站的方法有拍卖网站建设
  • 网站建设网站排名优化中国网站服务器哪个好
  • asp网站应用程序网站建设需要提供的资料
  • 网站开发与设计.net微信小程序设计制作
  • 怎样做网站排名优化展馆设计费取费标准一览表
  • 网站建设去哪可接单网站建设与设计大作业
  • 休闲咖啡厅网站开发目标韩国小清新网站模板
  • 做微景观的网站制作网页模板适应不同分辨率
  • 最简单的网站系统昨天军事新闻最新消息
  • 做ps网页设计的网站有哪些wordpress内容付费
  • 有没有免费注册域名的网站科技小制作 手工 简单
  • 网站支付端口win10优化大师怎么样
  • 怎么做云购网站吗网站流量监测
  • 网站被恶意刷流量可以翻外墙的浏览器
  • 网站做直链下载存储解决方案怎么把网站设置为主页面
  • 西安做网站招聘深圳网站见
  • 网站怎么做优化百度能搜索到wordpress 子分类
  • 六安网站建设培训制作网站需要多少时间
  • 电子商务专业网站建设什么软件可以做动画视频网站
  • wordpress 分享主题做网站优化有必要
  • ftp 网站管理电商网站设计图片
  • 惠州免费建站模板营销型旅游网站建设
  • 南宁cms建站wordpress 开启缩略图