Spring Boot 属性注入

1. 属性注入

Spring Boot全局配置文件设置属性时,如果配置属性是Spring Boot已有属性,例如服务端口server.port,那么Spring Boot内部会自动扫描并读取这些配置文件中的属性值并覆盖默认属性。如果配置的属性是用户自定义属性,必须在程序中注入这些配置属性方可生效。

1.1. 属性注入常用注解

@Configuration:声明一个类作为配置类

@Bean:声明在方法上,将方法的返回值加入Bean容器

@Value:属性注入

@ConfigurationProperties(prefix = "jdbc"):批量属性注入

@PropertySource("classpath:/jdbc.properties")指定外部属性文件。在类上添加

1.2. @Value属性注入

  1. 引入数据源连接依赖

    com.github.drtrang
    druid-spring-boot2-starter
    1.1.10

  1. application.properties添加信息
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboot_h
jdbc.username=root
jdbc.password=123
  1. 配置数据源

创建JdbcConfiguration类:使用spring中的value注解对每个属性进行注入,用bean注解将返回值添加到容器中

@Configuration
public class JdbcConfiguration {
    private static final Logger log = LoggerFactory.getLogger(JdbcConfiguration.class);

    @Value("${jdbc.url}")
    String url;

    @Value("${jdbc.driverClassName}")
    String driverClassName;

    @Value("${jdbc.username}")
    String username;

    @Value("${jdbc.password}")
    String password;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        log.info("url:{}, driverClassName:{}, username:{}, password:{}", url, driverClassName, username, password);
        return dataSource;
    }
}
  1. 启动项目,可以在日志中看到数据库相关属性成功被注入
2021-09-26 01:03:19.464  INFO 67490 --- [           main] c.e.s.config.JdbcConfiguration           : url:jdbc:mysql://127.0.0.1:3306/springboot_h, driverClassName:com.mysql.jdbc.Driver, username:root, password:123

1.3. @ConfigurationProperties批量注入

预先准备两个实体类文件,通过@ConfigurationProperties注解,将全局配置文件中的属性注入到实体类中。

  1. 分别在项目中新建一个pojo包,并在该包下创建两个实体类pet和Person

Pet类

public class Pet {
    private String type;
    private String name;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Pet{" +
                "type='" + type + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

Person类

使用@Component注解将Person类作为Bean注入到Spring容器中,通过@ConfigurationProperties将全局配置文件中以person开头的属性注入到该类。

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private int id; //id
    private String name; //姓名
    private List hobby; //爱好
    private String[] family; //家庭成员
    private Map map;
    private Pet pet; //宠物

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public List getHobby() {
        return hobby;
    }

    public void setHobby(List hobby) {
        this.hobby = hobby;
    }

    public String[] getFamily() {
        return family;
    }

    public void setFamily(String[] family) {
        this.family = family;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", hobby=" + hobby +
                ", family=" + Arrays.toString(family) +
                ", map=" + map +
                ", pet=" + pet +
                '}';
    }
}

@ConfigurationProperties(prefix = "person")注解的作用是将配置文件中以person开头的属性值通过 setXX()方法注入到实体类对应属性中

@Component注解的作用是将当前注入属性值的Person类对象作为Bean组件放到Spring容器中,只有 这样才能被@ConfigurationProperties注解进行赋值

  1. 加入Spring Boot提供的配置处理器依赖

    org.springframework.boot
    spring-boot-configuration-processor
    true

编写application.properties配置文件时,由于要配置的Person对象属性是我们自定义的,Spring Boot无法自动识别,所以不会有任何书写提示。在实际开发中,为了出现代码提示的效果来方便配置, 在使用@ConfigurationProperties注解进行配置文件属性值注入时,可以在pom.xml文件中添加一个 Spring Boot提供的配置处理器依赖。在pom.xml中添加上述配置依赖后,还需要重新运行项目启动类重构当前Spring Boot项目方可生效。

1.3.1. application.properties配置样例

person.id=1
person.name=张三
person.hobby=吃饭,睡觉,玩游戏
person.family=爸爸,妈妈
person.map.key1=value1
person.map.key2=value2
person.pet.type=dag
person.pet.name=小花

在application.properties配置文件中编写的属性需要对应Person类设置的属性。

  • 查看application.properties配置文件中注入的属性是否生效

查看application.properties配置文件是否正确,同时查看属性配置效果,打开通过IDEA工具创建 的项目测试类,在该测试类中引入Person实体类Bean,并进行输出测试

@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootDemoApplicationTests {

    @Autowired
    private Person person;

    @Test
    void configurationTest() {
        System.out.println(person);
    }

}

打印结果:

configurationTest执行结果

1.3.2. application.yaml配置样例

YAML文件格式是Spring Boot支持的一种JSON超集文件格式,以数据为中心,比properties、xml等更适合做配置文件:

  • yml和xml相比,少了一些结构化的代码,使数据更直接,一目了然相比properties文件更简洁;
  • YAML文件的扩展名可以使用.yml或者.yaml;
  • application.yml文件使用 “key:(空格)value”格式配置属性,使用缩进控制层级关系。
  1. value值为普通数据类型(例如数字、字符串、布尔等)

当YAML配置文件中配置的属性值为普通数据类型时,可以直接配置对应的属性值,同时对于字符 串类型的属性值,不需要额外添加引号,示例代码如下

server:
  port: 8080
  servlet:
    context-path: /hello
  1. value值为数组和单列集合

当YAML配置文件中配置的属性值为数组或单列集合类型时,主要有两种书写方式:缩进式写法和行内式写法。

其中,缩进式写法还有两种表示形式,示例代码如下

person:
  hobby:
    - play
    - read
    - sleep

或者使用如下示例形式

person:
  hobby:
    play,
    read,
    sleep

上述代码中,在YAML配置文件中通过两种缩进式写法对person对象的单列集合(或数组)类型的爱好 hobby赋值为play、read和sleep。其中一种形式为“-(空格)属性值”,另一种形式为多个属性值之前 加英文逗号分隔(注意,最后一个属性值后不要加逗号)。

person:
  hobby: [play,read,sleep]

通过上述示例对比发现,YAML配置文件的行内式写法更加简明、方便。另外,包含属性值的中括 号“[]”还可以进一步省略,在进行属性赋值时,程序会自动匹配和校对

  1. value值为Map集合和对象

当YAML配置文件中配置的属性值为Map集合或对象类型时,YAML配置文件格式同样可以分为两种书写方式:缩进式写法和行内式写法。
其中,缩进式写法的示例代码如下

person: 
    map:
        k1: v1
    k2: v2

对应的行内写法示例代码如下

person:
  map: {k1: v1,k2: v2}

在YAML配置文件中,配置的属性值为Map集合或对象类型时,缩进式写法的形式按照YAML文件格式编 写即可,而行内式写法的属性值要用大括号“{}”包含。

接下来,在Properties配置文件演示案例基础上,通过配置application.yaml配置文件对Person对象进 行赋值,具体使用如下

  • 在项目的resources目录下,新建一个application.yaml配置文件,在该配置文件中编写为Person 类设置的配置属性
person:
  id: 2
  name: 李四
  hobby: [吃饭,睡觉,玩游戏]
  family: [爸爸,妈妈]
  map: {key1: value1,key2: value2}
  pet: {type: dog,name: 小白}

再次执行测试:

configurationTest执行结果

可以看出,测试方法configurationTest()同样运行成功,并正确打印了Person实体类对象。

1.4. 第三方配置

除了@ConfigurationProperties用于注释类之外,还可以在公共@Bean方法上使用它。当要将属性绑定到控件之外的第三方组件时,这样做特别有用。

效果演示:

  • 创建一个其他组件类
public class AnotherComponent {
    private boolean enabled;
    private InetAddress inetAddress;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public InetAddress getInetAddress() {
        return inetAddress;
    }

    public void setInetAddress(InetAddress inetAddress) {
        this.inetAddress = inetAddress;
    }
}
  • 创建MyService
@Configuration
public class MyService {
    @ConfigurationProperties("another")
    @Bean
    public AnotherComponent anotherComponent(){
        return new AnotherComponent();
    }
}
  • 配置文件
another:
  enabled: true
  inet-address: 192.168.1.1
  
  • 测试类
@Autowired
private MyService myService;

@Test
void myServiceTest() {
    System.out.println(myService.anotherComponent());
}

通过测试类可以获得AnotherComponent组件的实例对象

myServiceTest输出结果

1.5. 松散绑定

Spring Boot使用一些宽松的规则将环境属性绑定到@ConfigurationProperties bean,因此环境属性名 和bean属性名之间不需要完全匹配

  • 例如属性类:
@Component
@ConfigurationProperties("acme.my-person.person")
public class OwnerProperties {
    private String firstName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Override
    public String toString() {
        return "OwnerProperties{" +
                "firstName='" + firstName + '\'' +
                '}';
    }
}
  • 配置文件:
acme:
  my-person:
    person:
      first-name: 王二小
  • 测试类
@Autowired
private OwnerProperties ownerProperties;

@Test
void ownerPropertiesTest() {
    System.out.println(ownerProperties);
}

可以正常将属性注入

ownerPropertiesTest输出结果
  • 松散配置参考:
属性文件中配置 说明
acme.my-project.person.first-name 羊肉串模式case,推荐使用
acme.myProject.person.firstName 标准驼峰模式
acme.my_project.person.first_name 下划线模式
ACME_MYPROJECT_PERSON_FIRSTNAME 大写下划线,如果使用系统环境时候推荐使用

你可能感兴趣的:(Spring Boot 属性注入)