spring-boot studies four: spring boot configuration @ PropertySource, @ ImportResource, @ Bean

1. @ PropertySource: import the specified configuration file

We saw earlier when loading configuration information, configuration information only if we have written in the spring boot of global configuration file application.properties (or application.yaml) file. spring boot also prepared to import the specified configuration file comments @PropertySource for us;

1.1 Example of use

  • Establishing a Student class, there stuName, stuAge, className three attributes;
package com.hai.bao.springboot03;

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

/**
 * @author :haibao.wang
 * @date :Created in 2019/8/31 22:02
 * @description:使用@PropertySource加载指定配置文件示例
 * @modified By:
 * @version: $
 */
@PropertySource(value = {"classpath:student.properties"})
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private String stuName;
    private int stuAge;
    private String className;//班级


    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuName='" + stuName + '\'' +
                ", stuAge=" + stuAge +
                ", className='" + className + '\'' +
                '}';
    }
}
  • New file student.properties profile, which set the property value;

 

 

  • Student class to increase @ PropertySource, @ Configurationproperties annotation, making it possible to read the configuration file student.properties;

 

  • Running the program, the attribute value can be acquired normally student.properties file;

 

2. @ ImportResource: Importing Spring configuration file, so that the contents of the configuration file which takes effect

This comment is actually imported annotation files bean.xml, the following by the following examples demonstrate their use;

  • Create a new class HelloService

 

 

  •  Create a bean.xml files, ioc configuration;
<?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="helloService" class="com.hai.bao.springboot03.HelloService"></bean>
</beans>

 

  •  Spring boot loader because there is no functional spring configuration file, so you want to load to come, @ImportResource notes need to be added to a configuration class, this class can be configured to configure the master class of spring boot (which is called the master class run);

 

 The syntax is: @ImportResource (locations = { "Document 1", "Document 2"});

  • We write the next unit test class test;

 3.spring boot the recommended way to load the spring configuration file

Notes with respect to @ImportResource spring load profile way, spring boot has its own recommended loading, that whole comment loadable spring configuration file;

Configuration using both categories: to replace the configuration file (i.e. bean.xml profile)

 

  • Create a new configuration class, used in place of the spring configuration .xml file

 

 

  • Examples of names contained in the container is the way we configured the name of class;

 

 When the name is inconsistent with the method vessel name, operation is false;

 

Guess you like

Origin www.cnblogs.com/haibaowang/p/11440744.html