加载指定配置文件
@PropertySource : 指定加载配置文件
@configurationProperties :默认从全局配置文件中获取值
-
资源目录下 创建 person.properties文件
name=cxy age=30
-
然后在我们的代码中指定加载person.properties文件
@PropertySource(value = "classpath:person.properties")
-
创建person类
package com.qiyu.springbootshiro.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /** * @program: spring-boot-shiro * @description: * @author: cxy * @create: 2021-08-03 14:13 **/ @PropertySource(value = "classpath:person.properties") //注册bean @Component public class Person2 { @Value("${name}") private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person2{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
-
测试
package com.qiyu.springbootshiro; import com.qiyu.springbootshiro.pojo.Dog; import com.qiyu.springbootshiro.pojo.Person; import com.qiyu.springbootshiro.pojo.Person2; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringBootShiroApplicationTests { @Autowired Person2 person2; @Test void contextLoads() { System.out.println(person2); } }