【SpringBoot 】 組件管理 + 屬性注入

SpringBoot 2020 核心知識點整理!

組件管理

@Component 管理單個組件

在 springboot 中可以管理自定義的 簡單組件 對象的創建可以直接使用註解形式創建:

  • @Component 用來管理單個組件(包掃描形式)

1、使用 @Repository@Service@Controller、以及 @Component 管理不同簡單對象;
比如要通過工廠創建自定義 User 對象:User.java

@Component
@Data
public class User {
    private String id;
    private String name;
    // ......
}

2、通過工廠創建之後可以在使用處任意注入該對象;
比如在控制器中使用自定義簡單對象創建:

@Controller
@RequestMapping("hello")
public class HelloController {
    @Autowired
    private User user;
  	// ......
}

@Configuration + @Bean 管理多個組件

在 springboot 中如果要管理 複雜對象 必須使用 @Configuration + @Bean 註解進行管理;

  • @Configuration 主要用來生產多個組件交給工廠管理 (註冊形式)

1、管理複雜對象的創建
注意:這裏的 User 不需要加 @Component 註解,與上面的管理單個對象區分開來。

@ToString
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
    private String id;
    private String name;
}
@Configuration // @Component 也可以, 不推薦
public class BeanConfigs {

    @Bean   // @Bean 將當前返回值作爲工廠中的一個對象進行管理
    public User getUser() {
        return new User();
    }

    @Bean //(name = "aaa") // 用來將該方法的返回值交給springboot管理 在工廠中默認標識: 類名(首字母小寫)
    @Scope("prototype") // prototype表示多例; 默認是singleton, 單例的
    public Calendar getCalendar() {
        return Calendar.getInstance();
    }

}

2、使用複雜對象

@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private User user;
    
    @Autowired
    private Calendar calendar1;

    @Autowired
    private Calendar calendar2;

    @GetMapping("/hello")
    public String hello() {
        System.out.println(user); // 管理單個對象
        // 管理多個對象, 默認是單例的, 需要設置 prototype
        System.out.println(calendar1.getTime()); // 成功創建出對象
        System.out.println(calendar1 == calendar2); // 默認是單例的, 需要設置 prototype 才爲多例
        return "hello spring boot!!!";
    }

屬性注入

SpringBoot 使用一個全局的配置文件,配置文件名是固定的:

  • application.properties
  • application.yml

配置文件的作用:修改 SpringBoot 自動配置的默認值;SpringBoot 在底層都給我們自動配置好了;

springboot 中提供了兩種注入方式:基本屬性注入對象注入

基本屬性注入 @Value

基本屬性包括:intStringDateString[]ListMap 等。

基本屬性注入:使用註解 @Value("${xxx}")

@RestController
@RequestMapping("hello")
public class HelloController {
	
    @Value("${server.port}") 
    private int port;

    @Value("${str}") 
    private String str;
    
    @Value("${bir}")
    private Date bir;

    @Value("${strs}")
    private String[] strs;

    @Value("${list}")
    private List<String> list;

    @Value("#{${maps}}") // map注入取值有點特殊
    private Map<String, String> maps;
    
     @GetMapping("hello")
    public String hello() {
    
    	// port = 8989
        System.out.println("port = " + port);
        // str =  zhenyu
        System.out.println("str= " + str);
        // time = Wed Dec 12 12:12:12 CST 2012
        System.out.println("time = " + bir);
        // aa
		// bb
		// cc
        for (String str : strs) {
            System.out.println(str);
        }
        // zhangsan
		// lisi
		// wangwu
        list.forEach( v -> System.out.println(v));
        // k = aa, v = xiaoyi
		// k = bb, v = xiaoer
		// k = cc, v = xiaosan
        maps.forEach((k, v) -> {
            System.out.println("k = " + k + ", v = " + v);
        });
        
        return "hello spring boot!";
        
    }
}

application.properties 中配置:

server.port = 8989

# 屬性注入
str = zhenyu
bir = 2012/12/12 12:12:12

strs = aa, bb, cc
list = zhangsan, lisi, wangwu
maps = {'aa':'xiaoyi', 'bb':'xiaoer', 'cc':'xiaosan'}

對象方式注入 @ConfigurationProperties

對象方式注入使用註解:@ConfigurationProperties(prefix="前綴")

@ConfigurationProperties 告訴 SpringBoot 本類中的所有屬性在配置文件中進行綁定;

  • 只有這個組件是容器中的組件,才能容器提供的 @ConfigurationProperties 功能,所以需要 @Component
@ToString
@Data // 必要
@Component // @Configuration 也可以
@ConfigurationProperties(prefix = "user") // 必要
public class User {
    private String id;
    private String name;
    private Integer age;
    private Date bir;
}

控制器中使用:@Autowired 完成自動注入;

@RestController
@RequestMapping("hello")
public class HelloController {

    @Autowired
    private User user;

    @GetMapping("hello")
    public String hello() {
        System.out.println(user); 
        // User(id=1721, name=yusael, age=20, bir=Wed Dec 12 12:12:12 CST 2012)
        return "hello spring boot!";
    }
}

application.properties 中配置:

# 自定義對象屬性注入
user.id = 1721
user.name = zhenyu
user.age = 20
user.bir = 2012/12/12 12:12:12

注:可以通過引入依賴 —— 配置文件處理器,構建自定義注入元數據。

  • 意思就是引入這個依賴後,在 配置文件中寫注入對象會有提示,不引入也不影響什麼。
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

兩種注入方式比較

@ConfigurationProperties @Value
功能 批量注入配置文件中的屬性 一個個指定
鬆散綁定(鬆散語法) 支持 不支持
SpEL 不支持 支持
JSR303 數據校驗 支持 不支持
複雜類型封裝 支持 不支持

如果說,我們只是在某個業務邏輯中需要獲取一下配置文件中的某項值,使用 @Value

如果說,我們專門編寫了一個 javaBean 來和配置文件進行映射,我們就直接使用@ConfigurationProperties

注入細節

配置文件注入值數據校驗 @Validated

@Component
@ConfigurationProperties(prefix = "person")
@Validated // 配置文件注入值數據校驗
public class Person {

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}從環境變量、配置文件中獲取值/#{SpEL}"></property>
     * <bean/>
     */

   //lastName必須是郵箱格式
    @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;

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

加載指定的配置文件 @PropertySource

@PropertySource 加載指定的配置文件;

// 加載指定的配置文件
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
	private String lastName;
	private Integer age;
	private Boolean boss;
}

導入 Spring 的配置文件 @ImportResource

@ImportResource:導入 Spring 的配置文件,讓配置文件裏面的內容生效;

  • Spring Boot 裏面沒有 Spring 的配置文件,我們自己編寫的配置文件,也不能自動識別;
    想讓 Spring 的配置文件生效,加載進來;@ImportResource 標註在一個配置類上;
// 導入Spring的配置文件讓其生效
@ImportResource(locations = {"classpath:beans.xml"})

然後編寫 Spring 的配置文件:

<?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.atguigu.springboot.service.HelloService"></bean>
</beans>

配置文件佔位符

隨機數:

${random.uuid}
${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1024,65536]}

: 指定佔位符中的默認值:

person.last-name=張三${random.uuid} # 隨機數
person.age=${random.int} # 隨機數
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog # 指定默認值
person.dog.age=15
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章