新增spring-annotation-conditional

master
xuchengsheng 2023-10-11 22:37:49 +08:00
parent b925d19284
commit 8b4def7ba7
18 changed files with 228 additions and 0 deletions

View File

@ -20,6 +20,7 @@
<module>spring-annotation-componentScan</module>
<module>spring-annotation-dependsOn</module>
<module>spring-annotation-lazy</module>
<module>spring-annotation-conditional</module>
</modules>
</project>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-annotation</artifactId>
<groupId>com.xcs.spring</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-annotation-conditional</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@ -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 20230807 1621
**/
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);
}
}
}

View File

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

View File

@ -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();
}
}

View File

@ -0,0 +1,7 @@
package com.xcs.spring.bean.entity;
public class User1 {
public String sayHello() {
return "Hello from User!";
}
}

View File

@ -0,0 +1,7 @@
package com.xcs.spring.bean.entity;
public class User2 {
public String sayHello() {
return "Hello from User!";
}
}

View File

@ -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 20230807 1621
**/
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);
}
}
}

View File

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

View File

@ -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();
}
}

View File

@ -0,0 +1,4 @@
package com.xcs.spring.configuration.entity;
public class User3 {
}

View File

@ -0,0 +1,4 @@
package com.xcs.spring.configuration.entity;
public class User4 {
}

View File

@ -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 20230807 1621
**/
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);
}
}
}

View File

@ -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 {
}

View File

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

View File

@ -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();
}
}

View File

@ -0,0 +1,4 @@
package com.xcs.spring.custom.entity;
public class User5 {
}

View File

@ -0,0 +1,4 @@
package com.xcs.spring.custom.entity;
public class User6 {
}