zoukankan      html  css  js  c++  java
  • 024 使用@PropertySoruce 注入配置文件

    一 .概述

    在前面我们说到,我们获取属性值的最大途径就是从外部的配置文件之中获取.

    spring为我们提供了@PropertySoruce注解完成属性文件的属性值的获取.


     二 .测试

    [1] 创建一个配置文件

    trek.name=trek
    trek.age=11

    [2]配置类

    @Configuration
    @PropertySource(value="classpath:value.properties")
    public class PropertyConfig {
        
        @Value("${trek.name}")
        private String  name ;
        
        @Value("${trek.age}")
        private Integer age;
        
        @Bean
        public Person person() {
            Person person = new Person();
            person.setName(name);
            person.setAge(age);
            return person;
        }
    }

    我们使用${}的方式将配置文件的属性注入到配置类之中.

    测试类:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes=PropertyConfig.class)
    public class PropertyTest {
        @Autowired
        private ApplicationContext context;
        
        @Test
        public void test() {
            System.out.println(context.getBean("person"));
        }
    }

    就是那么简单,很容易就获取了配置文件的属性了.

  • 相关阅读:
    Oracle ASH报告生成和性能分析
    Oracle AWR报告生成和性能分析
    oracle和mysql批量合并对比
    Oracle
    Delphi
    Delphi
    Delphi
    Delphi
    Delphi
    Delphi
  • 原文地址:https://www.cnblogs.com/trekxu/p/9094883.html
Copyright © 2011-2022 走看看