1. 程式人生 > 實用技巧 >springboot:獲取值和配置檔案(@ConfigurationProperties、@Value、@PropertySource、@ImportResource和@Bean)

springboot:獲取值和配置檔案(@ConfigurationProperties、@Value、@PropertySource、@ImportResource和@Bean)

1、@ConfigurationProperties(prefix = "student")方式

(1)定義兩個實體類,其中student實體類的屬性包括Course類:

@Data
@Component
@ConfigurationProperties(prefix = "student")//告訴springboot將本類中的所有屬性和配置檔案的相關配置進行繫結
public class Student {       //prefix:配置檔案中哪一個名稱下面的屬性進行一一對映
    private String sname;
    private int age;
    private Map<String,Object> maps;
    private List<Object> list;
    private Course course;
}
@Data
public class Course {
    private String courseno;
    private String coursename;
}

(2)建立yaml配置檔案:

student:
  sname: zhai
  age: 12

  maps: {k1: 12,k2: 13}
  list:
    - zhai
    - zhang
  course:
    courseno: 202007
    coursename: javaweb

(3)建立properties檔案:

#配置student
student.age=12
student.sname=zhai
student.maps.k1=1
student.maps.k2=2
student.list=a,b,c
student.course.courseno=202007
student.course.coursename=java

(4)測試類:

//可以在測試期間很方便地類似編碼一樣進行自動注入等容器的功能
@SpringBootTestclass Springboot03ApplicationTests {
    @Autowired
    Student student;
    @Test
    void contextLoads() {
        System.out.println(student);
    }
}

(5)需要匯入的依賴:配置檔案處理器,配置檔案進行繫結會有提示

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.2.1.RELEASE</version>
   </dependency>

2、@Value方式

(1)書寫配置檔案

#配置student
student.sname=zhai
student.age=12
student.maps.k1=1
student.maps.k2=2
student.list=a,b,c
student.course.courseno=202007
student.course.coursename=java

(2)獲取值:

@Data
@Component
public class Student {
    @Value("${student.sname}")
    private String sname;
    @Value("#{2*9}")
    private int age;
    private Map<String,Object> maps;
    private List<Object> list;
    private Course course;
}

(3)@ConfigurationProperties(prefix = "")方式與@Value方式的比較

@ConfigurationProperties(prefix = "")方式支援批量注入配置檔案的屬性,@Value方式需要一個個指定

@ConfigurationProperties(prefix = "")方式支援鬆散繫結,@Value方式不支援

@Value方式支援JSR303校驗

@Data
@Component
@Validated
public class Student {
    @NonNull
    private String sname;
    private int age;
    private Map<String,Object> maps;
    private List<Object> list;
    private Course course;
}

@Value方式支援SpEl

如果我們只是在某一個業務邏輯中需要獲取配置檔案的某一項值,可以使用@Value,如果是一個javaBean來和配置檔案進行對映,則要使用@ConfigurationProperties(prefix = "")方式

@RestController
public class HelloController {
    @Value("${student.sname}")
    private String sname;
    @RequestMapping("/hello")
    public String hello(){
        return "hello"+sname;
    }
}

配置檔案:

#配置student
student.sname=zhai
student.age=12
student.maps.k1=1
student.maps.k2=2
student.list=a,b,c
student.course.courseno=202007
student.course.coursename=java

3、@PropertySource

(1)配置檔案(student.properties)

#配置student
student.sname=zhai
student.age=12
student.maps.k1=1
student.maps.k2=2
student.list=a,b,c
student.course.courseno=202007
student.course.coursename=java

(2)實體類獲取值

@Data
@Component
@PropertySource(value = {"classpath:student.properties"})
public class Student {
    private String sname;
    private int age;
    private Map<String,Object> maps;
    private List<Object> list;
    private Course course;
}

@PropertySource是從指定路徑下獲取資料,預設是從application.properties下獲取資料

4、@ImportResource和@Bean

(1)指定spring的配置檔案

@SpringBootApplication(scanBasePackages = "com")
@ImportResource(locations = {"classpath:beans.xml"})
public class Springboot02Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot02Application.class, args);
    }
}

(2)書寫spring的配置檔案:beans.xml

(3)書寫如下配置,可以省略配置檔案的書寫,用註解來代替

@Configuration
public class MyAppConfig {
    @Bean
    public HelloService helloService(){
        return new HellService();
    }
}

該方式將方法的返回值新增到容器中,容器中元件的ID預設是方法名