Spring框架中Condidtion注解
1.1 装配bean简单过滤
1.1.1 实现接口EcondingConvert
package com. spring. condition;
public interface EcondingConvert {
}
1.1.2 创建GBKEcondingConvert,UTF8EcondingConvert实现EcondingConvert
package com. spring. condition;
public class GBKEcondingConvert implements EcondingConvert {
}
package com. spring. condition;
public class UTF8EcondingConvert implements EcondingConvert {
}
1.1.3 创建GBKCondition,UTF8Condition实现Condition接口
package com. spring. condition;
import org. springframework. context. annotation. Condition;
import org. springframework. context. annotation. ConditionContext;
import org. springframework. core. type. AnnotatedTypeMetadata;
import org. springframework. core. type. AnnotationMetadata;
import org. springframework. core. type. MethodMetadata;
public class GBKCondition implements Condition {
@Override
public boolean matches ( ConditionContext context, AnnotatedTypeMetadata metadata) {
String encoding = System. getProperty ( "file.encoding" ) ;
if ( encoding!= null) {
return encoding. equalsIgnoreCase ( "gbk" ) ;
}
return false ;
}
}
package com. spring. condition;
import org. springframework. context. annotation. Condition;
import org. springframework. context. annotation. ConditionContext;
import org. springframework. core. type. AnnotatedTypeMetadata;
import org. springframework. core. type. AnnotationMetadata;
import org. springframework. core. type. MethodMetadata;
public class UTF8Condition implements Condition {
@Override
public boolean matches ( ConditionContext context, AnnotatedTypeMetadata metadata) {
String encoding = System. getProperty ( "file.encoding" ) ;
if ( encoding!= null) {
return encoding. equalsIgnoreCase ( "utf-8" ) ;
}
return false ;
}
}
1.1.4 创建EcondingConvertConfiguration
package com. spring. condition;
import org. springframework. boot. SpringBootConfiguration;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Conditional;
@SpringBootConfiguration
public class EcondingConvertConfiguration {
@Bean
public EcondingConvert createGBKEcondingConvert ( ) {
return new GBKEcondingConvert ( ) ;
}
@Bean
public EcondingConvert createUTF8EcondingConvert ( ) {
return new UTF8EcondingConvert ( ) ;
}
}
1.1.5 创建ConditionApplication启动项 执行启动项
package com. spring. condition;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. SpringBootApplication;
import org. springframework. context. ConfigurableApplicationContext;
@SpringBootApplication
public class ConditionApplication {
public static void main ( String[ ] args) {
ConfigurableApplicationContext context = SpringApplication. run ( ConditionApplication. class , args) ;
System. out. println ( System. getProperty ( "file.encoding" ) ) ;
System. out. println ( context. getBeansOfType ( EcondingConvert. class ) ) ;
context. close ( ) ;
}
}
Connected to the target VM, address: '127.0.0.1:56032' , transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \
\\/ ___) | | _) | | | | | || ( _| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-22 15:40:59.254 INFO 19612 --- [ main] c.spring.condition.ConditionApplication : Starting ConditionApplication on DESKTOP-K6DA22C with PID 19612 (C:\project\learnProject\springboot\SpringBoot\SpringCondition\target\classes started by Administrator in C:\project\learnProject\springboot\SpringBoot\SpringCondition)
2020-07-22 15:40:59.261 INFO 19612 --- [ main] c.spring.condition.ConditionApplication : No active profile set, falling back to default profiles: default
2020-07-22 15:40:59.878 INFO 19612 --- [ main] c.spring.condition.ConditionApplication : Started ConditionApplication in 1.171 seconds (JVM running for 2.553)
UTF-8
{createGBKEcondingConvert=com.spring.condition.GBKEcondingConvert@7927bd9f, createUTF8EcondingConvert=com.spring.condition.UTF8EcondingConvert@532721fd}
Disconnected from the target VM, address: ' 127.0.0.1:56032', transport: ' socket'
Process finished with exit code 0
1.1.6 在装配的bean的注解上加上 @Conditional(GBKCondition.class)和@Conditional(UTF8Condition.class)
package com.spring.condition;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
@SpringBootConfiguration
public class EcondingConvertConfiguration {
@Bean
@Conditional(GBKCondition.class)
public EcondingConvert createGBKEcondingConvert(){
return new GBKEcondingConvert();
}
@Bean
@Conditional(UTF8Condition.class)
public EcondingConvert createUTF8EcondingConvert(){
return new UTF8EcondingConvert();
}
}
Connected to the target VM, address: '127.0.0.1:56968' , transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \
\\/ ___) | | _) | | | | | || ( _| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-22 15:46:28.580 INFO 12720 --- [ main] c.spring.condition.ConditionApplication : Starting ConditionApplication on DESKTOP-K6DA22C with PID 12720 (C:\project\learnProject\springboot\SpringBoot\SpringCondition\target\classes started by Administrator in C:\project\learnProject\springboot\SpringBoot\SpringCondition)
2020-07-22 15:46:28.593 INFO 12720 --- [ main] c.spring.condition.ConditionApplication : No active profile set, falling back to default profiles: default
2020-07-22 15:46:29.222 INFO 12720 --- [ main] c.spring.condition.ConditionApplication : Started ConditionApplication in 1.187 seconds (JVM running for 2.587)
UTF-8
{createUTF8EcondingConvert=com.spring.condition.UTF8EcondingConvert@388ffbc2}
Disconnected from the target VM, address: ' 127.0.0.1:56968', transport: ' socket'
Process finished with exit code 0
1.1.6 启动项设置参数
1.1.6.1 设置启动参数 -Dfile.encoding=GBK
Connected to the target VM, address: '127.0.0.1:58315' , transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \
\\/ ___) | | _) | | | | | || ( _| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-22 15:55:07.341 INFO 19016 --- [ main] c.spring.condition.ConditionApplication : Starting ConditionApplication on DESKTOP-K6DA22C with PID 19016 (C:\project\learnProject\springboot\SpringBoot\SpringCondition\target\classes started by Administrator in C:\project\learnProject\springboot\SpringBoot\SpringCondition)
2020-07-22 15:55:07.346 INFO 19016 --- [ main] c.spring.condition.ConditionApplication : No active profile set, falling back to default profiles: default
2020-07-22 15:55:07.980 INFO 19016 --- [ main] c.spring.condition.ConditionApplication : Started ConditionApplication in 1.181 seconds (JVM running for 2.575)
GBK
{createGBKEcondingConvert=com.spring.condition.GBKEcondingConvert@5ae81e1}
Disconnected from the target VM, address: ' 127.0.0.1:58315', transport: ' socket'
Process finished with exit code 0
1.1.6.2 设置启动参数 -Dfile.encoding=UTF-8
Connected to the target VM, address: '127.0.0.1:58966' , transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \
\\/ ___) | | _) | | | | | || ( _| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-22 15:58:47.411 INFO 20276 --- [ main] c.spring.condition.ConditionApplication : Starting ConditionApplication on DESKTOP-K6DA22C with PID 20276 (C:\project\learnProject\springboot\SpringBoot\SpringCondition\target\classes started by Administrator in C:\project\learnProject\springboot\SpringBoot\SpringCondition)
2020-07-22 15:58:47.416 INFO 20276 --- [ main] c.spring.condition.ConditionApplication : No active profile set, falling back to default profiles: default
2020-07-22 15:58:48.030 INFO 20276 --- [ main] c.spring.condition.ConditionApplication : Started ConditionApplication in 1.169 seconds (JVM running for 2.597)
UTF-8
{createUTF8EcondingConvert=com.spring.condition.UTF8EcondingConvert@ca27722}
Disconnected from the target VM, address: ' 127.0.0.1:58966', transport: ' socket'
Process finished with exit code 0
1.1.7 作用在类的情况
1.1.7.1 默认为utf-8 都装配成功
package com. spring. condition;
import org. springframework. boot. SpringBootConfiguration;
import org. springframework. context. annotation. Bean;
import org. springframework. context. annotation. Conditional;
@SpringBootConfiguration
@Conditional ( UTF8Condition. class )
public class EcondingConvertConfiguration {
@Bean
public EcondingConvert createGBKEcondingConvert ( ) {
return new GBKEcondingConvert ( ) ;
}
@Bean
public EcondingConvert createUTF8EcondingConvert ( ) {
return new UTF8EcondingConvert ( ) ;
}
}
Connected to the target VM, address: '127.0.0.1:60538' , transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \
\\/ ___) | | _) | | | | | || ( _| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-22 16:08:40.312 INFO 5496 --- [ main] c.spring.condition.ConditionApplication : Starting ConditionApplication on DESKTOP-K6DA22C with PID 5496 (C:\project\learnProject\springboot\SpringBoot\SpringCondition\target\classes started by Administrator in C:\project\learnProject\springboot\SpringBoot\SpringCondition)
2020-07-22 16:08:40.318 INFO 5496 --- [ main] c.spring.condition.ConditionApplication : No active profile set, falling back to default profiles: default
2020-07-22 16:08:40.919 INFO 5496 --- [ main] c.spring.condition.ConditionApplication : Started ConditionApplication in 1.159 seconds (JVM running for 2.524)
UTF-8
{createGBKEcondingConvert=com.spring.condition.GBKEcondingConvert@1a6f5124, createUTF8EcondingConvert=com.spring.condition.UTF8EcondingConvert@1edb61b1}
Disconnected from the target VM, address: ' 127.0.0.1:60538', transport: ' socket'
Process finished with exit code 0
1.1.7.1 设置参数为gbk 都没有装配
Connected to the target VM, address: '127.0.0.1:60912' , transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \
\\/ ___) | | _) | | | | | || ( _| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-22 16:10:50.498 INFO 12832 --- [ main] c.spring.condition.ConditionApplication : Starting ConditionApplication on DESKTOP-K6DA22C with PID 12832 (C:\project\learnProject\springboot\SpringBoot\SpringCondition\target\classes started by Administrator in C:\project\learnProject\springboot\SpringBoot\SpringCondition)
2020-07-22 16:10:50.502 INFO 12832 --- [ main] c.spring.condition.ConditionApplication : No active profile set, falling back to default profiles: default
2020-07-22 16:10:51.107 INFO 12832 --- [ main] c.spring.condition.ConditionApplication : Started ConditionApplication in 1.211 seconds (JVM running for 2.61)
GBK
{}
Disconnected from the target VM, address: ' 127.0.0.1:60912', transport: ' socket'
Process finished with exit code 0
1.1.8 总结
1 在没有加上@Conditional(GBKCondition.class)和@Conditional(UTF8Condition.class)注解时,
可以看到都是将两个bean
(GBKEcondingConvert,UTF8EcondingConvert)装配上去了
2 在加上@Conditional(GBKCondition.class)和@Conditional(UTF8Condition.class)注解是,只装配了
UTF8EcondingConvert
3 @Conditional除了用在方法上面还可以使用在类上面,使用在类上面,可以他的作用域就是整个类,例如我将
@Conditional(UTF8Condition.class)
放在EcondingConvertConfiguration类上面,项目默认是utf-8南无返回的就是ture 两个UTF8EcondingConvert,
GBKEcondingConvert就是装配spring的容器中,
1.2 condition其他的使用方式
1.2.1 Conditional其他注解
ConditionalOnBean ConditionalOnClass ConditionalOnCloudPlatform ConditionalOnExpression ConditionalOnJava ConditionalOnJndi ConditionalOnMissingBean ConditionalOnMissingClass ConditionalOnNotWebApplication ConditionalOnProperty ConditionalOnResource ConditionalOnSingleCandidate ConditionalOnWarDeployment ConditionalOnWebApplication
1.2.2 ConditionalOnProperty实例
1.2.2.1 UserConfiguration配置类
package com. spring. condition;
import org. springframework. boot. SpringBootConfiguration;
import org. springframework. boot. autoconfigure. condition. ConditionalOnProperty;
import org. springframework. context. annotation. Bean;
@SpringBootConfiguration
public class UserConfiguration {
@Bean
@ConditionalOnProperty ( name = "runnable.enabled" , havingValue = "true" , matchIfMissing = true )
public Runnable createRunnable ( ) {
return ( ) - > { } ;
}
}
1.2.2.2 application.properties
#runnable. enabled= false
#runnable. enabled= true
1.2.2.3 runnable.enabled=true时 ,装配成功
Connected to the target VM, address: '127.0.0.1:49888' , transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \
\\/ ___) | | _) | | | | | || ( _| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-22 16:43:42.300 INFO 20404 --- [ main] c.spring.condition.ConditionApplication : Starting ConditionApplication on DESKTOP-K6DA22C with PID 20404 (C:\project\learnProject\springboot\SpringBoot\SpringCondition\target\classes started by Administrator in C:\project\learnProject\springboot\SpringBoot\SpringCondition)
2020-07-22 16:43:42.307 INFO 20404 --- [ main] c.spring.condition.ConditionApplication : No active profile set, falling back to default profiles: default
2020-07-22 16:43:42.946 INFO 20404 --- [ main] c.spring.condition.ConditionApplication : Started ConditionApplication in 1.188 seconds (JVM running for 2.564)
GBK
{}
{createRunnable=com.spring.condition.UserConfiguration$$Lambda $205 /727250772@2e77b8cf}
Disconnected from the target VM, address: ' 127.0.0.1:49888', transport: ' socket'
1.2.2.4 runnable.enabled=false时 ,装配失败
Connected to the target VM, address: '127.0.0.1:64176' , transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \
\\/ ___) | | _) | | | | | || ( _| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-22 16:32:05.902 INFO 7268 --- [ main] c.spring.condition.ConditionApplication : Starting ConditionApplication on DESKTOP-K6DA22C with PID 7268 (C:\project\learnProject\springboot\SpringBoot\SpringCondition\target\classes started by Administrator in C:\project\learnProject\springboot\SpringBoot\SpringCondition)
2020-07-22 16:32:05.918 INFO 7268 --- [ main] c.spring.condition.ConditionApplication : No active profile set, falling back to default profiles: default
2020-07-22 16:32:06.518 INFO 7268 --- [ main] c.spring.condition.ConditionApplication : Started ConditionApplication in 1.152 seconds (JVM running for 2.503)
GBK
{}
{}
Disconnected from the target VM, address: ' 127.0.0.1:64176', transport: ' socket'
Process finished with exit code 0