注解@ConfigurationProperties使用方法

what?

@ConfigurationProperties  是一个配置注解,在spring boot中大量使用。该注解可以使用在类和方法上面。

how?

1 配置在类上

 

1.1 配置类编写

需要在容器中实例化使用,所以要加注解@Component

package mabatispluslearn.com.wang.mp.config;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author zhipan
 * @since
 * @see
 * {@code @ConfigurationProperties}注解
 */

@ConfigurationProperties(prefix = "spring.userconfig")
@Component
public class UserConfigProperties {
    private String name;
    private Integer age;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

1.2 配置文件配置

配置文件中使用配置类中设置的prefix(spring.userconfig)加上类变量配置需要的value 

spring.userconfig.name=test
spring.userconfig.age=27
[email protected]

 

1.3配置使用

配置对象已经注入到容器中,直接使用即可。

package mabatispluslearn;

import lombok.extern.slf4j.Slf4j;
import mabatispluslearn.com.wang.mp.config.UserConfigProperties;
import mabatispluslearn.com.wang.mp.entity.User;
import mabatispluslearn.com.wang.mp.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@Slf4j
class MabatisPlusLearnApplicationTests {

    @Autowired
    private  UserMapper userMapper ;
    @Autowired
    private UserConfigProperties userConfigProperties;
    @Test
    void contextLoads() {
    }

    @Test
    void testInsert() {
        //保存用户测试
        User user = new User();
        //从配置文件中获取到值
        user.setAge(userConfigProperties.getAge());
        user.setName(userConfigProperties.getName());
        user.setEmail(userConfigProperties.getEmail());
        userMapper.insert(user);
    }

}

 

2 配置在方法上

配置在方法上面,可以通过不同的方法注入同一类型bean的不同配置信息。

下面是我的探究案例:

2.1 自定义一个实体,模拟数据库信息

package mabatispluslearn.com.wang.mp.entity;


/**
 * @author zhipan
 */
public class MyDataSource{
    private String url;
    private String username;
    private String password;
    private String driverClass;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClass() {
        return driverClass;
    }

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    @Override
    public String toString() {
        return "MyDataSource{" +
                "url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", driverClass='" + driverClass + '\'' +
                '}';
    }
}

2.2 配置类

使用@Configuration  @Bean  注入容器

package mabatispluslearn.com.wang.mp.config;

import mabatispluslearn.com.wang.mp.entity.MyDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhipan
 *
 * 配置在方法上面,可以配置多个场景的值,例如数据库读写分离
 */
@Configuration
public class DataSourceConfig {


    @Bean(name = "readSource")
    @ConfigurationProperties(prefix = "wang.datasource.read")
    MyDataSource readSource(){
        return new MyDataSource();
    }

    @Bean(name = "writeSource")
    @ConfigurationProperties(prefix = "wang.datasource.write")
    MyDataSource writeSource(){
        return new MyDataSource();
    }
}

2.3 配置

wang.datasource.write.url=jdbc:mysql://localhost:3306/write
wang.datasource.write.username=write
wang.datasource.write.password=111
wang.datasource.write.driver-class=com.mysql.jdbc.Driver


wang.datasource.read.url=jdbc:mysql://localhost:3306/read
wang.datasource.read.username=read
wang.datasource.read.password=222
wang.datasource.read.driver-class=com.mysql.jdbc.Driver

2.4配置使用

package mabatispluslearn;


import mabatispluslearn.com.wang.mp.config.DataSourceConfig;
import mabatispluslearn.com.wang.mp.entity.MyDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
public class MyTest {

    @Resource(name = "readSource")
    private MyDataSource readSource;

    @Resource(name = "writeSource")
    private MyDataSource writeSource;

    @Test
    void test(){
        //读数据库-配置信息获取
        System.out.println("获取配置的读数据库信息==>"+readSource.toString());
        //写数据库配置信息获取
        System.out.println("获取配置的写数据库信息==>"+writeSource.toString());
    }



}

测试结果:

 

你可能感兴趣的:(Java,spring,spring,boot)