Solomon_ Schog shell architecture with everyone “bounce” SpringBoot overall design and core function points, so today to see how to define and run custom Condition, usually used for specific special conditions of the scene. Thumbs up, thumbs up, thumbs up. Follow my public account Solomon for more surprises
Conditional
Conditionality is the core judgment function of SpringFramework, on which SpringBoot defines @conditionalonclass and @conditionalonproperty annotations to achieve richer execution judgments.
Custom condition annotations
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional(SolomonPropertyCondition.class)
public @interface ConditionalOnSolomon {
/** * Service key */
String key(a);
/** * Service key */
String value(a);
}
Copy the code
Customize the Condition class
Create CSolomonPropertyCondition class and implement Condition, judge annotations within the value equals to Solomon returns true, otherwise it returns false
public class SolomonPropertyCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnSolomon.class.getName());
String propertyName = String.valueOf(attributes.get("key"));
String propertyValue = String.valueOf(attributes.get("value"));
return "Solomon".equals(propertyValue); }}Copy the code
Run the custom Condition
public class ConditionalSolomonBootstrap {
// Create a string Bean with the property value Solomon
@Bean
@ConditionalOnSystemProperty(key = "http://www.xiaoailiang.com", value = "Solomon")
public String CreateSolomon(a) {
return "Solomon ";
}
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionalSolomonBootstrap.class)
.web(WebApplicationType.NONE)
.run(args);
// Get the Bean of type String by name
String solomonBeanName = context.getBean("Solomon", String.class);
// Print the Bean name
System.out.println(solomonBeanName);
// Close the contextcontext.close(); }}Copy the code
Follow-up explanation, SpringBoot custom boot Logo implementation scheme
yourLike and followIs the continuing power of the Solomon_ Shogo shell structure.
Hot historical Articles
-
🔥Serverless Microservices elegant shutdown practices
-
🔥 this algorithm can not understand! How are the 9 images presented
-
🔥Java is how to take off the beautiful woman’s clothes
-
🔥 High-performance gateway was originally designed this way
-
🔥REST FUL look still don’t understand, you play me!
-
How much 🔥Serverless affects programmers
-
🔥 How does distributed transaction XID connect all microservices in tandem
-
🔥 Microservices Distributed Transaction TCC core implementation
-
🔥 hundreds of millions of traffic site performance optimization methodology steps
-
🔥 microservice Nacos implements proximity access through CMDB to improve performance
-
🔥 Micro service architecture DNS service registration and discovery mechanism
-
. More and more