SpringBoot获取properties配置

前言:在项目中,很多时候需要把配置写在properties里,部署的时候也需要切换不同的环境来选择正确的配置的参数,也有时候需要将mq redis等第三方配置新建一个properties文件在项目中引用。

1.因为是Spring的环境,当然首先需要搭建好Spring环境。

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2016/10/13.
 */
@Component
public class ValueTest {
    public String name = "注入对象的的属性";
    @Autowired
    public Environment env;//当前环境的application.properties的 配置
    @Value("注入普通字符串")//注入普通字符串
    public String test1;
    @Value("#{systemProperties['os.name']}")//系统属性配置
    public String test2;
    @Value("#{ T(java.lang.String).valueOf(111)}")//执行某个类的方法
    public String test3;
    @Value("#{valueTest.name}")//某个类的公有属性
    public String test4;
    @Value("${name}")//Springboot的properties,或者配置在PropertySourcesPlaceholderConfigurer Bean里的properties文件的值
    public String test5;

}

需要注意的是通过 Environment 对象只能获取 Springboot的propertie文件的参数,比如 application-dev.properties。如果是不是application开头的的配置文件,需要单独指定properties的路径

@PropertySource("classpath:config.properties")//引用其他单独的properties

如果前置一样可以统一配置

@ConfigurationProperties(prefix = "spring.wnagnian",locations = "classpath:config/xxx.properties")  

 

2.如果直接用 @Value("${name}") 来取配置的值需要配置 PropertySourcesPlaceholderConfigurer 用来引入properties文件

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

/**
 * Created by Administrator on 2016/10/13.
 */
@Configuration
public class PropertiesConfig {

    @Bean
    public PropertySourcesPlaceholderConfigurer createPropertySourcesPlaceholderConfigurer() {
        ClassPathResource resource = new ClassPathResource("config.properties");
        PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setLocation(resource);
        return propertyPlaceholderConfigurer;
    }
}

如果是Spring xml配置

   <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
       <property name="locations">  
           <list>  
               <value>classpath:config.properties</value>  
           </list>  
       </property>  
    </bean>  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
        <property name="properties" ref="configProperties" />  
    </bean>  

取值

@Value("#{configProperties['name']}")
private String name;

 

博客地址:https://my.oschina.net/wangnian

转载于:https://my.oschina.net/wangnian/blog/758009

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,可以使用 `@ConfigurationProperties` 注解来读取配置文件。当配置文件中有一个以特定前缀开头的配置项集合时,可以使用 `@ConfigurationProperties` 注解与 `@Bean` 注解结合使用来读取。 例如,假设我们有一个配置文件 `application.properties`,其中包含以下配置项: ``` myapp.items[0].name=apple myapp.items[0].price=5 myapp.items[1].name=banana myapp.items[1].price=3 ``` 我们可以通过以下方式读取该配置项集合: 首先,我们需要创建一个 Java 类来表示每个配置项的属性。例如,我们可以创建一个 `Item` 类: ```java public class Item { private String name; private int price; // 省略 getter 和 setter 方法 } ``` 然后,我们可以创建一个 `ItemsConfiguration` 类来读取配置项集合: ```java @Configuration @ConfigurationProperties(prefix = "myapp") public class ItemsConfiguration { private List<Item> items = new ArrayList<>(); public List<Item> getItems() { return items; } public void setItems(List<Item> items) { this.items = items; } @Bean public List<Item> itemList() { return items; } } ``` 在上面的代码中,我们使用 `@ConfigurationProperties` 注解来指定配置项的前缀为 `myapp`,并且定义了一个 `List<Item>` 类型的属性 `items` 来保存配置项集合。我们还定义了一个 `itemList()` 方法,用于将 `items` 属性作为 Bean 注册到 Spring 容器中。 最后,在需要使用该配置项集合的地方,我们可以使用 `@Autowired` 注解来注入 `List<Item>` 类型的 Bean: ```java @RestController public class MyController { @Autowired private List<Item> items; @GetMapping("/items") public List<Item> getItems() { return items; } } ``` 这样就可以通过访问 `/items` 接口来获取配置项集合了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值