新增spring-annotation-conditional
parent
b925d19284
commit
8b4def7ba7
|
@ -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>
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.xcs.spring.bean.entity;
|
||||
|
||||
public class User1 {
|
||||
public String sayHello() {
|
||||
return "Hello from User!";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.xcs.spring.bean.entity;
|
||||
|
||||
public class User2 {
|
||||
public String sayHello() {
|
||||
return "Hello from User!";
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.xcs.spring.configuration.entity;
|
||||
|
||||
public class User3 {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.xcs.spring.configuration.entity;
|
||||
|
||||
public class User4 {
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.xcs.spring.custom.entity;
|
||||
|
||||
public class User5 {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.xcs.spring.custom.entity;
|
||||
|
||||
public class User6 {
|
||||
}
|
Loading…
Reference in New Issue