当前位置:网站首页>Three usage scenarios of annotation @configurationproperties

Three usage scenarios of annotation @configurationproperties

2022-07-07 08:41:00 Dream and sea depth @ none

stay Spring Boot Notes in @ConfigurationProperties There are three usage scenarios , In general, we only use one of the scenes most . This article takes you to understand the use of three scenarios .

Scene one

Use @ConfigurationProperties and @Component Annotation to bean On the definition class , here @Component Refers to the same class instantiation Bean Annotations .

The basic examples are as follows :

//  Define a class as a bean Annotations , such as  @Component,@Service,@Controller,@Repository
//  perhaps  @Configuration
@Component
//  Indicates that the prefix in the configuration file is user1 The value of the property initializes the bean Definition produces bean The same name property of the instance 
//  When used, this definition produces bean when , Its properties name Would be Tom
@ConfigurationProperties(prefix = "user1")
public class User {
	private String name;
	//  Omit getter/setter Method 
}

Corresponding application.properties The contents of the configuration file are as follows :

user1.name=Tom

In this scenario , When Bean When instantiated ,@ConfigurationProperties The following properties of the corresponding prefix will be associated with Bean Object's properties match . If the conditions are met, the assignment will be made .

Scene two

Use @ConfigurationProperties and @Bean The annotation is in the configuration class Bean In the way of definition . Take data source configuration for example :

@Configuration
public class DataSourceConfig {

	@Primary
	@Bean(name = "primaryDataSource")
	@ConfigurationProperties(prefix="spring.datasource.primary")
	public DataSource primaryDataSource() {
		return DataSourceBuilder.create().build();
	}
}

Here is the prefix “spring.datasource.primary” Properties of , Assign a value to DataSource Corresponding property value .

@Configuration The configuration class of the annotation is passed through @Bean Annotation defines the object returned by a method as a Bean, And use the corresponding properties in the configuration file to initialize the Bean Properties of .

Scene three

Use @ConfigurationProperties Annotation to common class , And then through @EnableConfigurationProperties Defined as Bean.

@ConfigurationProperties(prefix = "user1")
public class User {
	private String name;
	//  Omit getter/setter Method 
}

here User Object is not used @Component Related note .

And the User Class is used as follows :

@SpringBootApplication
@EnableConfigurationProperties({User.class})
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

In the above code , adopt @EnableConfigurationProperties Yes User When instantiating , Will be used to @ConfigurationProperties The function of , Match and assign properties .

原网站

版权声明
本文为[Dream and sea depth @ none]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070556099218.html