快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

springboot的conditioanal的解析

一:

公司主营业务:网站设计制作、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出鲁甸免费做网站回馈大家。

package com.zcp.springstart2;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication

public class App {

public static void main(String[] args) {

ConfigurableApplicationContext context = SpringApplication.run(App.class, args);

System.out.println(System.getProperty("file.encoding"));

System.out.println(context.getBeansOfType(EncodingConvert.class));

System.out.println(">>>>>>>>>>>>>start>>>>>>>>>>>>");

System.out.println(context.getBeansOfType(Runnable.class));

System.out.println(">>>>>>>>>>>>>end>>>>>>>>>>>>");

context.close();

}

}

接口:

package com.zcp.springstart2;

public interface EncodingConvert {

}

接口的实现:

package com.zcp.springstart2;

public class UTF8EncodingConvert implements EncodingConvert {

}

package com.zcp.springstart2;

public class GBKEncodingConvert implements EncodingConvert {

}

package com.zcp.springstart2;

import org.springframework.context.annotation.Condition;

import org.springframework.context.annotation.ConditionContext;

import org.springframework.core.type.AnnotatedTypeMetadata;

public class GBKCondition implements Condition {

@Override

public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

String encoding = System.getProperty("file.encoding");

if(null != encoding){

return "gbk".equals(encoding.toLowerCase());

}

return false;

}

}

package com.zcp.springstart2;

import org.springframework.context.annotation.Condition;

import org.springframework.context.annotation.ConditionContext;

import org.springframework.core.type.AnnotatedTypeMetadata;

public class UTF8Condition implements Condition {

@Override

public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

String encoding  = System.getProperty("file.encoding");

if(null != encoding){

return "utf-8".equals(encoding.toLowerCase());

}

return false;

}

}

常用的conditional的使用:

package com.zcp.springstart2;

import org.springframework.boot.SpringBootConfiguration;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import org.springframework.context.annotation.Bean;

@SpringBootConfiguration

public class UserConfig {

@Bean

@ConditionalOnProperty(name="runnable.enabled",havingValue="true",matchIfMissing=true)

public Runnable createRunnable(){

return new Runnable() {

@Override

public void run() {

System.out.println("UserConfig中的Runnable");

}

};

}

@Bean

@ConditionalOnClass(name="com.zcp.springstart2.User")

public Runnable createRunnableOnClass(){

return new Runnable() {

@Override

public void run() {

System.out.println("UserConfig中的createRunnableOnBean");

}

};

}

@Bean

@ConditionalOnBean(name="user")

public Runnable createRunnableOnBean(){

return new Runnable() {

@Override

public void run() {

System.out.println("UserConfig中的createRunnableOnBean");

}

};

}

}


分享标题:springboot的conditioanal的解析
文章路径:http://6mz.cn/article/pisjho.html

其他资讯