Spring Condition Notes

Spring Condition annotation main role is to inject bean container according to the condition, the condition referred to as annotations. It is a new feature introduced in spring 4.0

@Component
public class Cat {
}


@Component
@Conditional(Match.class)
public class Dog {
}

public class Match implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return false; } } public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext("com.edu.condition"); String[] beanNames = annotationConfigApplicationContext.getBeanDefinitionNames(); for(String name:beanNames){ System.out.println(name); } } }

Match achieve a Condition, rewrite matches method, when the method returns true when the conditions expressed.

Which can be obtained from the parameter context

1.context.getEnvironment () information such as the environment

String osName = context.getEnvironment () getProperty ( "os.name");. Gets the name of the operating system

2.context.getRegistry () registered bean

3.context.getResourceLoader () to load the resource

4.context.getBeanFactory()   beanFactory

 

Guess you like

Origin www.cnblogs.com/chenzhubing/p/11199110.html