05、Spring Boot,拆分配置文件以及配置类注解
Spring Boot    2019-07-25 23:00:32    423    0    0
acme   Spring Boot

    上次讲到了如何注入属性的值,如果将所有需要注入容器的值全部放入一个配置文件中,是否会让这个配置文件过于臃肿呢?

    那么,就使用@PropertySource这个注解来拆分配置文件,改注解的作用是告诉Spring Boot需要导入的配置文件的位置。

老样子,先上pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cc_acme</groupId>
    <artifactId>springboot-split-properties</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-split-properties</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

接着是配置文件application.properties,在这个配置文件中我只是加入了端口server.port=8080这个属性其实并没有作用,因为就是默认的,可以舍弃,加着只是方便看

server.port=8080

再接着在resources目录下,与application.properties同级目录新建一个名为user.properties的配置文件

user.id=1
user.user-name=acme
user.password=123

我们讲User类的值放在这个文件中,那么该如何将内容注入呢?

package cc_acme.springbootsplitproperties.entity;


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

/**
 * User entity
 *
 * @author acme
 * @ConfigurationProperties 注解用于标识注释参数 prefix标识对应的前缀
 * @Component 标识注入Spring容器中(只有注入Spring容器才能使用)
 */
@PropertySource("classpath:user.properties")//加载指定的配置文件,也可以是数组导入多个配置文件
@ConfigurationProperties(prefix = "user")
@Component
public class User {

    private Integer id;
    private String userName;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

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

    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;
    }
}

这个时候@PropertySource就产生了作用,最后来上测试类。

package cc_acme.springbootsplitproperties;

import cc_acme.springbootsplitproperties.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootSplitPropertiesApplicationTests {

    @Autowired
    private User user;

    @Test
    public void contextLoads() {
        System.out.println(user);
    }

}

看一下运行结果

属性的确注入其中了。

这个便是配置文件的简单拆分。

不过都到了这里,Spring Boot如何导入beans.xml呢?在很久很久以前,毕竟还是将值放入这个配置文件中的,下面又要用到一个新的注解,先卖个关子。

新建一个在resources目录下新建beans.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="cc_acme.springbootsplitproperties.entity.Student">
        <property name="id" value="2"/>
        <property name="name" value="student"/>
    </bean>

</beans>

在entity包下新建一个Student类

package cc_acme.springbootsplitproperties.entity;

/**
 * Student类
 *
 * @author acme
 */
public class Student {
    private Integer id;

    private String name;

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

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

接着新建一个测试类StudentTest并且运行测试

package cc_acme.springbootsplitproperties;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentTest {

    @Autowired
    ApplicationContext applicationContext;

    @Test
    public void testStudent() {
        System.out.println(applicationContext.containsBean("student"));
    }

}

运行的测试结果如下:

并没有加载beans.xml文件,其中配置的对象未被加载到容器中。

这个时候就需要用到@ImportResource注解,并且讲该注解放到程序入口上。

package cc_acme.springbootsplitproperties;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource("classpath:beans.xml")//也可以是数组导入多个配置文件
@SpringBootApplication
public class SpringbootSplitPropertiesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootSplitPropertiesApplication.class, args);
    }

}

此时再运行测试类获取的结果

到了这里,其实还并没有结束。从上面那句很久很久以前就应该看出来,现在还有多少人愿意去用beans.xml配置文件?不是在很久很久以前,Spring Framework就已经开始官方建议使用注解来配置吗?再到后来的配置类。既然SpringBoot作为Spring现在主推的框架,官方会不推荐一波使用注解配置?

新建一个CustomerConfig配置类

package cc_acme.springbootsplitproperties.config;

import cc_acme.springbootsplitproperties.entity.Customer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Customer配置类
 *
 * @author acme
 */
@Configuration//标识当前类是一个配置类
public class CustomerConfig {
    /**
     * @return customer
     * @Bean 注解用于标识该方法返回值添加到容器中,默认方法名便是id名
     */
    @Bean
    public Customer customer() {
        return new Customer();
    }

}

在新建一个CustomerTest测试类

package cc_acme.springbootsplitproperties;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerTest {

    @Autowired
    ApplicationContext applicationContext;

    @Test
    public void testCustomer() {
        System.out.println(applicationContext.containsBean("customer"));
    }
}

运行结果表明在容器中有customer这个bean,这也是现在Spring官方推荐的写法。

Pre: 06、Spring Boot,占位符

Next: 04、Spring Boot @Value和@Configurationproperties注解的区别

423
Table of content