重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

springboot的conditioanal的解析-创新互联

一:

目前创新互联公司已为上1000+的企业提供了网站建设、域名、虚拟主机绵阳服务器托管、企业网站设计、上党网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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");

}

};

}

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:springboot的conditioanal的解析-创新互联
当前网址:http://cqcxhl.cn/article/ddchsg.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP