• 读取application.properties参数注入到Bean属性


    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>www.tszr</groupId>
        <artifactId>chapter021</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    package com.itheima.domain;
    
    public class Pet {
        private String type;
        private String name;
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Pet{" +
                    "type='" + type + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    package com.itheima.domain;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "person")
    public class Person {
        private int id; // id
        private String name; // 名称
        private List<String> hobby; // 爱好
        private String[] family; // 家庭成员
        private Map<String, String> map;
        private Pet pet; // 宠物
    
    
        public int getId() {
            return id;
        }
    
    
        public void setId(int id) {
            this.id = id;
        }
    
    
        public String getName() {
            return name;
        }
    
    
        public void setName(String name) {
            this.name = name;
        }
    
    
        public List<String> getHobby() {
            return hobby;
        }
    
    
        public void setHobby(List<String> hobby) {
            this.hobby = hobby;
        }
    
    
        public String[] getFamily() {
            return family;
        }
    
    
        public void setFamily(String[] family) {
            this.family = family;
        }
    
    
        public Map<String, String> getMap() {
            return map;
        }
    
    
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
    
    
        public Pet getPet() {
            return pet;
        }
    
    
        public void setPet(Pet pet) {
            this.pet = pet;
        }
    
    
        @Override
        public String toString() {
            return "Person{" + "id=" + id + ", name='" + name + '\'' + ", hobby=" + hobby + ", family="
                    + Arrays.toString(family) + ", map=" + map + ", pet=" + pet + '}';
        }
    }
    person.id=1
    person.name=tom
    person.hobby=play,read,sleep
    person.family=father,mother
    person.map.k1=v1
    person.map.k2=v2
    person.pet.type=dog
    person.pet.name=kity
    package com.itheima;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    package com.itheima;
    
    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.test.context.junit4.SpringRunner;
    
    import com.itheima.domain.Person;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class ApplicationTest {
        @Autowired
        private Person person;
    
        @Test
        public void contextLoads() {
            System.out.println(person);
        }
    }

  • 相关阅读:
    java初学者之java语言主要知识点三
    C++类的对象和类的指针的区别
    win32多线程: 线程创建与结束等待
    多线程学习:win32多线程编程基本概念(转)
    C++常用数据类型和Windows常见数据类型
    VC++2017关于项目出现"const char *" 类型的实参与 "char *" 类型的形参不兼容错误的解决方法
    Linux环境下vi/vim编辑器常用命令
    c++学习笔记之类模板
    c++学习笔记之函数重载和模板理解
    c++学习笔记之多态和虚函数
  • 原文地址:https://www.cnblogs.com/tszr/p/15907160.html
Copyright © 2020-2023  润新知