通过配置文件来赋值有两个注解
@Value
@ConfigurationProperties
@Value如何注入值
package com.springboot.helloword.configuration;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
DruidDataSource dataSource=null;
@Value("${jdbc.driverClassName}")
String driverClassName;
@Value("${jdbc.url}")
String url;
@Value("${jdbc.username}")
String username;
@Value("${jdbc。password}")
String password;
@Bean
public DataSource dataSource(){
new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setName(username);
dataSource.setPassword(password);
return dataSource();
}
}
代码部分注解的解释
@Configuration
写在类上
表示这个类是一个配置类
@PropertySource("classpath:jdbc.properties)
相当于springboot中使用标签引入外部属性文件<context:property-placeholder location="classpath:jdbc.properties"/>
写在类上
@Bean
将方法的返回值注入到bean 容器中 容器组件的id默认是方法名
写在方法上
@Value
用于配置文件值的注入
建议写在属性名上
@Value相当于<bean> <properties name="" value></properties></bean>
Jdbc.Properties
jdbc.driverClassName=com。mysql。jdbc。Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboot
jdbc.username=root
jdbc.password=root
@Configuration值的注入
kage com.springboot.helloword.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.xml.crypto.Data;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix="person")
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Data birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBoss(boolean boss) {
this.boss = boss;
}
public void setBirth(Data birth) {
this.birth = birth;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getLastName() {
return lastName;
}
public Integer getAge() {
return age;
}
public boolean isBoss() {
return boss;
}
public Data getBirth() {
return birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public List<Object> getLists() {
return lists;
}
public Dog getDog() {
return dog;
}
}
application.yml
server:
port:8081
person:
lastName:小谎言
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1, k2: 12}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 14
@ConfigurationProperties(prefix="")
告诉springboot本类中所有的属性和配置文件相关的配置进行绑定
写在类上
总结:
要想用@ConfigurationProperties(prefix=“”)
yml文件要与@ConfigurationProperties下的类的属性值一一对应
如果需要@ConfigurationProperties(prefic="")有提示,可以在pom中加入spring-boot-configuration-processor。
@ ComPonent是为了解决@ConfigurationProperties爆红的问题
@ConfigurationProperties下的类的属性值必须要有get set 方法
与@Bean结合为属性赋值
与@PropertySource(只能用于properties文件)结合读取指定文件
@Value与@ConfigurationProperties的比较
Name | @ConfigurationProperties | @Value |
---|---|---|
功能 | 批量注入文件的属性 | 一个个注入 |
松散绑定 | 支持 | 不支持 |
Spel | 不支持 | 支持 |
复杂性封装 | 支持 | 不支持 |
JSR303数据校验 | 支持 | 不支持 |
Untitled |
配置文件yml还是properties他们都能获取值
只有某个业务,单个属性@Value
专门编写一个JavaBean来和配置文件进行映射,我们使用@ConfigurationProperties