一:
公司主營業(yè)務(wù):網(wǎng)站設(shè)計制作、做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出魯?shù)槊赓M做網(wǎng)站回饋大家。
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 {
}
接口的實現(xiàn):
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");
}
};
}
}