The use of @PropertySource, @ImportResource, @Bean in the SpringBoot project

@PropertySource load the specified configuration file

application.properties or application.yml is the default configuration file of Spring Boot and will be loaded automatically. Suppose we now have a src / main / resources / person.properties:

person.lastName=Jhon
person.age=18
person.boss=false
person.birth=2020/03/21
person.maps.k1=v1
person.maps.k2=v2
person.lists=Tom,Sam
person.dog.name=small dog
person.dog.age=2

First of all, this custom configuration file will not be loaded by SpringBoot, so we cannot map with our configuration class Person.java, so we can add @PropertySource annotation to the configuration class to achieve loading:

package com.wong.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 将配置文件中配置的每一个属性值,映射到这个组件中
 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
 * 			prefix = "person":配置文件中哪个下面的所有属性值进行一一映射
 * 	注意:只有这个组件是容器的组件,才能使用容器提供的ConfigurationProperties功能
 * @PropertySource加载指定的配置文件
 */
@Configuration
@PropertySource("classpath:person.properties")
@ConfigurationProperties(prefix = "person")
public class Person{
	private String lastName;
	private int age;
	private boolean boss;
	private Date birth;

	private Map<String,Object> maps;
	private List<Object> lists;
	private Dog dog;

	// omit setter getter
}

@ImportResource is specifically used to load Spring configuration files

1. Define the component HelloService

package com.wong.service;

public class HelloService {
}

2. Create a Spring configuration file src / main / resources / beans.xml

<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloService" class="com.wong.service.HelloService"/>
</beans>

The value of id is the unique identifier of the component after it is added to the Spring container.
3. Add this annotation to the configuration class (the class with @Configuration annotation) so that it can be loaded at startup. For convenience, it is now placed on the startup class

package com.wong;

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

@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class MainApplication{

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

4. Run the test class src / test / java / com / wong / SpringBootConfigTests.java

package com.wong;

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 SpringBootConfigTests {

    @Autowired
    private ApplicationContext ioc;
    @Test
    public void testBeans(){
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);// true
    }

}

@Bean adds components to the container

SpringBoot recommends adding components to the container in a fully annotated way:

  • The configuration class is the Spring configuration file
  • Use @Bean to add components to the container,
    so you can replace the above method of loading the Spring configuration file using @ImportResource with the following method:

1. Create a configuration class src / main / java / com / wong / config / AppConfig.java

package com.wong.config;

import com.wong.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public HelloService myHelloService(){
        return new HelloService();
    }
}

The @Configuration annotation will cause the configuration class to be scanned at startup.
@Bean will put an instance of HelloService into the Spring container, and the only identifier is its method name, such as myHelloService in this example.
2. Run the test class src / test / java / com / wong / SpringBootConfigTests.java

package com.wong;

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 SpringBootConfigTests {

    @Autowired
    private ApplicationContext ioc;

    @Test
    public void testMyHelloService(){
        boolean b = ioc.containsBean("myHelloService");
        System.out.println(b); // true
    }

}

Published 381 original articles · praised 85 · 80,000 views +

Guess you like

Origin blog.csdn.net/weixin_40763897/article/details/105107499