@configurationProperties [provided by SpringBoot]

There are too many configuration items to fetch one by one from the configuration file by @Value. So this thing, @ConfigurationProperties, comes into play.

All properties of classes marked @ConfigurationProperties are taken from the global configuration file

Here’s an example:

  1. Add the following configuration to the master configuration file
FTP: address: '192.168.1.1' Account: 'admin' Password: 'asdf' port: '20'Copy the code
  1. Used to create a configuration class
@Component 
@ConfigurationProperties(prefix = "ftp") 
public class MyFtp { 
    private String address; 
    private String account; 
    private String password; 
    private String port; 
} 
Copy the code

Here @ConfigurationProperties has a prefix parameter, which is used to specify the prefix of the configuration item in the configuration file.

  1. Import MyFtp from SpringBoot using @autoWired

@import [Spring]

@import Three ways to use it

  • Import plain Java classes directly into the container.
  • Used with custom ImportSelector.
  • Cooperate with ImportBeanDefinitionRegistrar use.

1. Import common Java classes directly

  1. Create a normal Java class.
Public class Yasuo {public void qFunction() {system.out.println (); }}Copy the code
  1. Create a configuration class and use it.
@Import({Yasuo.class}) 
@Configuration 
public class LolConfig { 

} 
Copy the code
  1. Create the test class.
public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(Lol.class);  Yasuo yasuo = context.getBean(Yasuo.class); yasuo.qFunction(); }Copy the code
  1. Running results:

High winds chop

2. Use it with a customized ImportSelector

ImportSelector is an interface with a single selectImports method that returns an array of full class names. So we can use this feature to dynamically import N beans into the container.

  1. Create a normal Java class.
public class Reven { public void qFunction(){ System.out.println("qa qa qa"); }}Copy the code
  1. Create the ImportSelector implementation class, selectImports returns the full class name of Reven.
public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata annotationMetadata) { return new String[]{"annotation.importannotation.Reven"}; }}Copy the code
  1. Create a configuration class and import MyImportSelector in addition to the original.
@Import({Yasuo.class,MyImportSelector.class}) 
@Configuration 
public class Lol { 

} 
Copy the code
  1. Creating a test class
public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(Lol.class);  Yasuo yasuo = context.getBean(Yasuo.class); yasuo.qFunction(); Reven reven = context.getBean(Reven.class); reven.qFunction(); } Duplicate codeCopy the code
  1. Running results:

The wind blows QA qa QA

You can see that the Reven object is also successfully instantiated by the IOC container.

3. Cooperate with ImportBeanDefinitionRegistrar use

ImportBeanDefinitionRegistrar is an interface, it can be manually registered bean into the container, so we can be personalized customization to the class. (You need to use @import and @Configuration together.)

  1. Create the ordinary Java class Jie.
public class Jie { public void qFunction() { System.out.println("qqq"); }}Copy the code
  1. Create ImportBeanDefinitionRegistrar implementation class, realization method directly manually register a Bean named Jie in the IOC container.
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) { RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Jie.class); / / register a name called Jie bean beanDefinitionRegistry. RegisterBeanDefinition (" Jie ", rootBeanDefinition); }}Copy the code
  1. Create a configuration class, import MyImportBeanDefinitionRegistrar class.
@Import({Yasuo.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class}) 
@Configuration 
public class LolV3 { 

} 
Copy the code
  1. Create the test class.
public static void main(String[] args) { 

ApplicationContext context = new AnnotationConfigApplicationContext(LolV3.class); 
    Yasuo yasuo = context.getBean(Yasuo.class); 
    yasuo.qFunction(); 
    
    Reven reven = context.getBean(Reven.class); 
    reven.qFunction(); 
    
    Jie jie = context.getBean(Jie.class); 
    jie.qFunction(); 
} 

Copy the code
  1. The results

High winds chop

qa qa qa

qqq

Yes, Jie objects are also registered.

@Conditional [provided by Spring]

The @Conditional annotation enables some configurations to be enabled only when certain conditions are met.

Here’s a simple example:

  1. ConditionBean creates the normal Java class ConditionBean, which validates that the Bean loaded successfully.
public class ConditionBean { public void sayHi() { System.out.println("ConditionBean sayHi()"); }} Copy the codeCopy the code
  1. Condition is an interface that has only a matches() method that returns a Boolean. If this method returns true, the Condition is true and the configuration class takes effect. Otherwise, it does not take effect. In this case we simply return true.
public class MyCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { return true; }} Copy the codeCopy the code
  1. Create the configuration class, and you can see that the @Conditionality of the configuration passes in the Condition implementation class we just created for the Condition judgment.
@Configuration @Conditional(MyCondition.class) public class ConditionConfig { @Bean public ConditionBean conditionBean(){ return new ConditionBean(); }} Copy the codeCopy the code
  1. Write test methods.
public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class); ConditionBean conditionBean = context.getBean(ConditionBean.class); conditionBean.sayHi(); } Duplicate codeCopy the code
  1. Results analysis

Since Condition’s matches method directly returns true, the configuration class will take effect. If we change matches to return false, the configuration class will not take effect.

In addition to custom conditions, Spring extends some of the commonly used conditions for us.

@Conditional extends annotation (Determine whether the specified condition is met)
@ConditionalOnJava Check whether the Java version of the system meets requirements
@ConditionalOnBean The specified Bean exists in the container.
@ConditionalOnMissingBean There is no specified Bean in the container;
@ConditionalOnExpression Satisfies the SpEL expression specification
@ConditionalOnClass There are specified classes in the system
@ConditionalOnMissingClass There is no specified class in the system
@ConditionalOnSingleCandidate There is only one specified Bean in the container, or the Bean is the preferred Bean
@ConditionalOnProperty Whether a specified property in the system has a specified value
@ConditionalOnResource Whether the specified resource file exists in the classpath
@ConditionalOnWebApplication The current environment is web
@ConditionalOnNotWebApplication The current environment is not Web
@ConditionalOnJndi JNDI has the specified entry