diff --git a/spring-annotation/pom.xml b/spring-annotation/pom.xml index dc2f627..ff2c0fb 100644 --- a/spring-annotation/pom.xml +++ b/spring-annotation/pom.xml @@ -20,6 +20,7 @@ spring-annotation-componentScan spring-annotation-dependsOn spring-annotation-lazy + spring-annotation-conditional \ No newline at end of file diff --git a/spring-annotation/spring-annotation-conditional/pom.xml b/spring-annotation/spring-annotation-conditional/pom.xml new file mode 100644 index 0000000..ed66dfe --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/pom.xml @@ -0,0 +1,19 @@ + + + + spring-annotation + com.xcs.spring + 0.0.1-SNAPSHOT + + 4.0.0 + + spring-annotation-conditional + + + 11 + 11 + + + \ No newline at end of file diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/ConditionBeanApplication.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/ConditionBeanApplication.java new file mode 100644 index 0000000..604d23e --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/ConditionBeanApplication.java @@ -0,0 +1,19 @@ +package com.xcs.spring.bean; + +import com.xcs.spring.bean.config.MyBeanConfiguration; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * @author xcs + * @date 2023年08月07日 16时21分 + **/ +public class ConditionBeanApplication { + + public static void main(String[] args) { + System.setProperty("enable.bean","false"); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfiguration.class); + for (String beanDefinitionName : context.getBeanDefinitionNames()) { + System.out.println("beanDefinitionName = " + beanDefinitionName); + } + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/condition/BeanPropertyCondition.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/condition/BeanPropertyCondition.java new file mode 100644 index 0000000..1089c81 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/condition/BeanPropertyCondition.java @@ -0,0 +1,12 @@ +package com.xcs.spring.bean.condition; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +public class BeanPropertyCondition implements Condition { + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return "true".equals(context.getEnvironment().getProperty("enable.bean")); + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/config/MyBeanConfiguration.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/config/MyBeanConfiguration.java new file mode 100644 index 0000000..807180d --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/config/MyBeanConfiguration.java @@ -0,0 +1,24 @@ +package com.xcs.spring.bean.config; + +import com.xcs.spring.bean.condition.BeanPropertyCondition; +import com.xcs.spring.bean.entity.User1; +import com.xcs.spring.bean.entity.User2; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MyBeanConfiguration { + + @Bean + @Conditional(BeanPropertyCondition.class) + public User1 user1() { + return new User1(); + } + + @Bean + @Conditional(BeanPropertyCondition.class) + public User2 user2() { + return new User2(); + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/entity/User1.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/entity/User1.java new file mode 100644 index 0000000..32e1483 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/entity/User1.java @@ -0,0 +1,7 @@ +package com.xcs.spring.bean.entity; + +public class User1 { + public String sayHello() { + return "Hello from User!"; + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/entity/User2.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/entity/User2.java new file mode 100644 index 0000000..a12c411 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/bean/entity/User2.java @@ -0,0 +1,7 @@ +package com.xcs.spring.bean.entity; + +public class User2 { + public String sayHello() { + return "Hello from User!"; + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/ConditionConfigurationApplication.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/ConditionConfigurationApplication.java new file mode 100644 index 0000000..4d255b3 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/ConditionConfigurationApplication.java @@ -0,0 +1,19 @@ +package com.xcs.spring.configuration; + +import com.xcs.spring.configuration.config.MyConfigConfiguration; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * @author xcs + * @date 2023年08月07日 16时21分 + **/ +public class ConditionConfigurationApplication { + + public static void main(String[] args) { + System.setProperty("enable.config","true"); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfigConfiguration.class); + for (String beanDefinitionName : context.getBeanDefinitionNames()) { + System.out.println("beanDefinitionName = " + beanDefinitionName); + } + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/condition/ConfigPropertyCondition.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/condition/ConfigPropertyCondition.java new file mode 100644 index 0000000..a4684ad --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/condition/ConfigPropertyCondition.java @@ -0,0 +1,12 @@ +package com.xcs.spring.configuration.condition; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +public class ConfigPropertyCondition implements Condition { + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return "true".equals(System.getProperty("enable.config")); + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/config/MyConfigConfiguration.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/config/MyConfigConfiguration.java new file mode 100644 index 0000000..ed12520 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/config/MyConfigConfiguration.java @@ -0,0 +1,23 @@ +package com.xcs.spring.configuration.config; + +import com.xcs.spring.configuration.condition.ConfigPropertyCondition; +import com.xcs.spring.configuration.entity.User3; +import com.xcs.spring.configuration.entity.User4; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Conditional(ConfigPropertyCondition.class) +public class MyConfigConfiguration { + + @Bean + public User3 user3() { + return new User3(); + } + + @Bean + public User4 user4() { + return new User4(); + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/entity/User3.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/entity/User3.java new file mode 100644 index 0000000..2360c1b --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/entity/User3.java @@ -0,0 +1,4 @@ +package com.xcs.spring.configuration.entity; + +public class User3 { +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/entity/User4.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/entity/User4.java new file mode 100644 index 0000000..3731435 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/configuration/entity/User4.java @@ -0,0 +1,4 @@ +package com.xcs.spring.configuration.entity; + +public class User4 { +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/ConditionCustomApplication.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/ConditionCustomApplication.java new file mode 100644 index 0000000..0e10522 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/ConditionCustomApplication.java @@ -0,0 +1,19 @@ +package com.xcs.spring.custom; + +import com.xcs.spring.custom.config.MyCustomConfiguration; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * @author xcs + * @date 2023年08月07日 16时21分 + **/ +public class ConditionCustomApplication { + + public static void main(String[] args) { + System.setProperty("enable.custom","true"); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyCustomConfiguration.class); + for (String beanDefinitionName : context.getBeanDefinitionNames()) { + System.out.println("beanDefinitionName = " + beanDefinitionName); + } + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/annotation/ConditionalOnCustomActive.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/annotation/ConditionalOnCustomActive.java new file mode 100644 index 0000000..9183b3d --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/annotation/ConditionalOnCustomActive.java @@ -0,0 +1,16 @@ +package com.xcs.spring.custom.annotation; + +import com.xcs.spring.custom.condition.CustomActiveCondition; +import org.springframework.context.annotation.Conditional; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Conditional(CustomActiveCondition.class) +public @interface ConditionalOnCustomActive { + +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/condition/CustomActiveCondition.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/condition/CustomActiveCondition.java new file mode 100644 index 0000000..67870f2 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/condition/CustomActiveCondition.java @@ -0,0 +1,12 @@ +package com.xcs.spring.custom.condition; + +import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; + +public class CustomActiveCondition implements Condition { + @Override + public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + return "true".equals(System.getProperty("enable.custom")); + } +} \ No newline at end of file diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/config/MyCustomConfiguration.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/config/MyCustomConfiguration.java new file mode 100644 index 0000000..76f13cd --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/config/MyCustomConfiguration.java @@ -0,0 +1,22 @@ +package com.xcs.spring.custom.config; + +import com.xcs.spring.custom.annotation.ConditionalOnCustomActive; +import com.xcs.spring.custom.entity.User5; +import com.xcs.spring.custom.entity.User6; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnCustomActive +public class MyCustomConfiguration { + + @Bean + public User5 user5() { + return new User5(); + } + + @Bean + public User6 user6() { + return new User6(); + } +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/entity/User5.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/entity/User5.java new file mode 100644 index 0000000..0b309d3 --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/entity/User5.java @@ -0,0 +1,4 @@ +package com.xcs.spring.custom.entity; + +public class User5 { +} diff --git a/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/entity/User6.java b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/entity/User6.java new file mode 100644 index 0000000..0e2877b --- /dev/null +++ b/spring-annotation/spring-annotation-conditional/src/main/java/com/xcs/spring/custom/entity/User6.java @@ -0,0 +1,4 @@ +package com.xcs.spring.custom.entity; + +public class User6 { +}