天天看點

SpringBoot項目中的@PropertySource、@ImportResource、@Bean的用途

@PropertySource 加載指定配置檔案

application.properties或application.yml是SpringBoot的預設配置檔案,會被自動加載。假如我們現在有一個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      

首先這個自定義的配置檔案不會被SpringBoot加載,那麼我們就無法與我們的配置類Person.java做映射,是以我們可以在配置類上加下@PropertySource注解來實作加載:

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專門用來加載Spring的配置檔案

1、定義元件HelloService

package com.wong.service;

public class HelloService {
}      

2、建立Spring配置檔案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>      

id的值就是元件在加入Spring容器後的唯一辨別。

3、在配置類(有@Configuration注解的類)上加入此注解,以便在啟動時,可以被加載,為了友善,現将其放到啟動類上

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、運作測試類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給容器中添加元件

SpringBoot推薦使用全注解的方式給容器中添加元件:

  • 配置類即Spring配置檔案
  • 使用@Bean給容器中添加元件

    是以可以将上面使用@ImportResource加載Spring配置檔案的方式換成以下的方式來實作:

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

}