SpringBoot 入門之二:獲取Properties中的值,通過類配置來替代原SpringXML的配值和注入方式
阿新 • • 發佈:2018-11-28
•application.properties
•application.yml
person.last-name=\u674E\u56DB
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15
1 通過@ConfigurationPropertie注入 :該注入適用於全域性application.properties , @ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置檔案中相關的配置進行繫結
@Component @ConfigurationProperties(prefix = "person") //@Validated public class Person { private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; // Set Get methods }
2 通過@Value注入 ,該注入適用於全域性application.properties
@Component //@ConfigurationProperties(prefix = "person") //@Validated public class Person { @Value("${person.lastName}") private String lastName; @Value("${person.age}") private Integer age; @Value("true}") private Boolean boss; @Value("${person.birth}") private Date birth; @Value("${person.maps}") private Map<String,Object> maps; private List<Object> lists; private Dog dog; // Set Get methods }
3 通過 @PropertySource(value = {“classpath:person.properties”}) 該注入適用於非區域性自定義屬性值的配置
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
如果屬性的key value配置是在spring XML配置檔案中,則可以用以下方法獲取
Bean.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="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
</beans>
4 通過@ImportResource(locations = {“classpath:beans.xml”})
@ImportResource:匯入Spring的配置檔案,讓配置檔案裡面的內容生效; Spring Boot裡面沒有Spring的配置檔案,我們自己編寫的配置檔案,也不能自動識別; 想讓Spring的配置檔案生效,必須載入進來;@ImportResource標註在一個配置類上
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigApplication.class, args);
}
}
import com.atguigu.springboot.bean.Person;
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 SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Autowired
ApplicationContext ioc;
@Test
public void testHelloService(){
Object hellobean = ioc.getBean("helloService02");
boolean b = ioc.containsBean("helloService02");
System.out.println(hellobean);
}
}
5 通過註解的方式類@Configuration 獲取spring配置的內容
SpringBoot推薦給容器中新增元件的方式;推薦使用全註解的方式
1、配置類@Configuration 替代 Spring配置檔案
2、使用@Bean給容器中新增元件
package com.atguigu.springboot.config;
import com.atguigu.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration:指明當前類是一個配置類;就是來替代之前的Spring配置檔案
*
* 在配置檔案中用<bean><bean/>標籤新增元件
*
*/
@Configuration
public class MyAppConfig {
//將方法的返回值新增到容器中;容器中這個元件預設的id就是方法名
@Bean
public HelloService helloServiceHere(){
System.out.println("配置類@Bean給容器中新增元件了...");
return new HelloService();
}
}
注意:要獲取的bean的名字,必須跟@Configuration檔案中標註為@Bean所獲取的物件的方法名一致,即 ”helloServiceHere“
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Autowired
ApplicationContext ioc;
@Test
public void testHelloService(){
Object hellobean = ioc.getBean("helloServiceHere");
System.out.println(hellobean);
}
}