SpringBootIntegration: SpringBootIntegration
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
@ConfigurationProperties
配置文件yml还是properties他们都能获取到值;
我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value
我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties
product:
courseDTOS:
- id: 111
num: 4444
- id: 222
num: 555
- id: 333
num: 666
@Data
@Configuration
@ConfigurationProperties("product")
public class ConfigurationPropertiesShow {
private List<CourseDTO> courseDTOS;
}
Value
类型
基本数值
可以写SpEL; #{}
可以写${};取出配置文件中的值
${person.nickName:默认值} 没有默认值会报错
public class Person {
@Value("zs")
private String name;
@Value("#{20-2}")
private Integer age;
@Value("${person.nickName:默认值}")
private String nickName;
person.nickName=kk
public class ValuesShowTest {
@Test
public void test(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesShow.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
Person person = (Person)applicationContext.getBean("person");
System.out.println(person.toString());
ConfigurableEnvironment environment = applicationContext.getEnvironment();
System.out.println(environment.getProperty("person.nickName"));
}
}
person:
name: zhangsan
age: 18
@Controller
public class QuickStartController {
@Value("${person.name:du}")
private String name;
@Value("${person.age}")
private Integer age;
@RequestMapping("/quick")
@ResponseBody
public String quick(){
return "springboot 访问成功! name="+name+",age="+age;
}
}
PropertySource
system.ip=http://192.168.31.206:8888/metadataserver/v1
system.login=${system.ip}/pro/proUserController/getToken
# map 结构
system.role[select]=${system.ip}/system/user/select
system.role[update]=${system.ip}/system/user/update
@Component
@PropertySource(value = {"classpath:otherSystemInterfaceConfig/usereSystem.properties"})
@ConfigurationProperties(prefix = "system")
@Getter
@Setter
public class PropertiesShow {
private String login;
private Map<String, String> role;
}
@ImportResource
导入以前的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="now" class="java.util.Date"/>
<bean id="account" class="com.du.bean.Account">
<constructor-arg name="name" value="泰斯特"/>
<constructor-arg name="age" value="18"/>
<constructor-arg name="birthday" ref="now"/>
</bean>
</beans>
@ImportResource(locations = {"classpath:DIBeanTest.xml"})
@Component
public class ImportResourceShow {
}
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class ImportResourceTest {
@Autowired
ApplicationContext applicationContext;
@Test
public void test(){
boolean account = applicationContext.containsBean("account");
System.out.println(account);
}
}