springboot 加载配置文件

springboot配置文件:

​ @ConfigurationProperties:从配置文件中获取值

​ @Value:从配置文件中获取值

​ @PropertySource:加载指定的配置文件

​ @Import: 加载类

​ @ImportResource: 加载.xml

@ConfigurationProperties(扫描整个配置文件-)

  • 与@Bean结合为属性赋值
  • 与***[@PropertySource(只能用于properties文件)]***结合读取指定文件
  • 与@Validation结合,支持JSR303进行配置文件值的校验,如@NotNull@Email等

@Value(可以是普通字符串 / 操作系统属性/ 表达式/ 配置属性)

  • 为单个属性赋值
  • 支持属性上的SpEL表达式
@ConfigurationProperties@Value
功能批量注入配置文件中的属性一个个指定
松散绑定支持不支持
SpEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

    @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{11*3}")
    private Integer age;

.yml配置文件

person:
  lastName: hello
  age: 19
  boss: false
  birth: 2019/11/12
  maplist:
    - name: abc
      value: abcValue,abc
    - name: 123
      value: [123Value,123]   
      

@PropertySource:加载指定的配置文件

@PropertySource(value = {"classpath:student.properties"})
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
   private Integer id;
   private String username;
   private Integer age;

student.properties配置文件

student.id=30
student.username=王五
student.age=33
student.boss=true
student.birth=2018/03/03
student.lists=lisa,wangwu3
student.maps.k1=300
student.maps.k2=300
student.dog.name=小狗3
student.dog.age=3

@Import:导入java类

public class Cat {
    @Value("cat")
    private String name;
    @Value("3")
    private Integer age;
    
}    
public class Pig {
    @Value("pig")
    private String name;
    private Integer age;    
}
@SpringBootApplication
@Import({Pig.class, Cat.class})
public class Springboot01Application {

    public static void main(String[] args) {
		 SpringApplication.run(Springboot01Application.class, args);
	}

@ImportResource 是导入spring配置的.xml文件

beans.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="dogxml" class="com.panshi.springboot01.pojo.Dog">
        <property name="name" value="jack"></property>
        <property name="age" value="18"></property>
    </bean>
</beans>

item.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="itemService" class="com.panshi.springboot01.service.ItemService"></bean>
</beans>

@ImportResource(locations = {"classpath:beans.xml","classpath:item.xml"})
@SpringBootApplication
public class Springboot01Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot01Application.class, args);
    }
}

下面这种方式也可以注入属性

@Configuration
public class MyConfig {

    // 将方法的返回值添加到容器中,容器中这个组件的id默认为方法名
    @Bean
    public ItemService itemService() {
        return new ItemService();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值