配置文件
test.opp=testValue
引入方式
@Value直接在用的地方引用
@Value("${test.opp}")
String opp;
@ConfigurationProperties需要新建配置类
@Data
@Configuration
@ConfigurationProperties("test")//配置父级名称
public class TestProperties {
String opp;//配置子级名称
}
测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertiesTest {
@Value("${test.opp}")
String opp;
@Autowired
TestProperties testProperties;
@Test
public void test(){
System.out.println("@Value引入:"+opp);
System.out.println("@ConfigurationProperties引入:"+testProperties.getOpp());
}
}
结果
@Value引入:testValue
@ConfigurationProperties引入:testValue
动态配置
使用apollo配置中心,修改配置
@Value引入是动态的
@ConfigurationProperties引入需要设置监听并刷新Bean