版权声明:来一来,看一看,有钱的捧个人场,没钱的你不得捧个人场 https://blog.csdn.net/wait_for_eva/article/details/82845438
yml
person:
age: 99
last-name: godme
# 这个不用多说,注意的就是yml一般用来配置全局信息
# 具体的bean信息不用yml
@ConfigurationProperties
// yml配置对象需要配合ConfigurationProperties使用
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
...
}
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 class="com.godme.demo.Person">
<property name="lastName">
<value>godme</value>
</property>
<property name="age">
<value>99</value>
</property>
</bean>
</beans>
@ImportSource
// ImportResource导入spring-xml配置文件
@ImportResource(value = {"classpath:bean.xml"})
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Properties
person.name=godme
person.age=23
@PropertySource
// 配合propertySource使用
@PropertySource(value = {"classpath:bean.properties"})
public class Person {
private String lastName;
private Integer age;
}
注解对照
对象
// 对象属性配置,提倡代码配置
// @Configuration标识为配置类
@Configuration
public class Config {
// bean信息配置
//对照标签<bean></bean>
@Bean
public Person person(){
return new Person();
}
}
单值
public class Person {
// <bean>
// <value>相当于这个标签</value>
//</bean>
@Value("godme")
private String name;
/**
支持类型
1. 基本类型
Integer,String,Boolean
2. 全局变量
${person.name}
3. spEL
#{10 * 20}
*/
@Value("99")
private Integer age;
// PS:不支持MAP等复杂类型的注入
}
占位限制
# 默认值设定
# 找到father.home就采用,没有则默认earth
person.home=${father.home:earth}
person.name=fucker
person.surname=mother
# 取值,自带连接
person.full-name=${person.surname}-{$name}
# 随机占位
person.age=${random.int}
# ${random.value}
# ${random.int(10)}
# ${random.int[0,100]}