SpringBoot(五):自定义属性获取

Spring Boot系列
SpringBoot(一):Quick start
SpringBoot(二):多环境切换
SpringBoot(三):Respond封装
SpringBoot(四):统一异常处理

目录

一.全局配置文件配置属性
二.获取单一属性
三.映射Bean属性
四.测试

一.全局配置文件配置属性

在src/main/resources目录下,找到一个名为application-dev.properties的全局配置文件,可对一些默认配置的配置值进行修改。
ps:本次目录结构
这里写图片描述
配置信息如下

自定义属性值

server.port=8080

#测试属性值
yjgithub.name=cyj
yjgithub.email=yigithub@163.com
yjgithub.sex=man

二.获取单一属性

创建以下文件

/com/citydata/config/YjgithubConfig.java


ps:
1.需要加@Component注解
2.@Value("${yjgithub.name}") 获取到你所需要的对应值

package com.citydata.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author yjgithub
 * @Date 2018/5/29
 * @Description  测试配置文件信息
 */
@Component
public class YjgithubConfig {

    // name
    public static String name;
    @Value("${yjgithub.name}")
    public void setName(String $name) {
        this.name = $name;
    }
}

三.映射Bean属性

3.1添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

3.2 定义一个bean(YjgithubBean) 匹配你所对应的属性值

在以下路径创建该bean

/com/citydata/bean/YjgithubConfig.java


ps:
1.需要加@ConfigurationProperties(prefix="yjgithub") 注解匹配上配置文件中的属性值

package com.citydata.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Created by yjgithub on 2018/5/29.
 * name
 * email
 * sex
 */
@ConfigurationProperties(prefix="yjgithub")
public class YjgithubBean {
    private String name;
    private String email;
    private String sex;

    @Override
    public String toString() {
        return "yjgithub{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public YjgithubBean(String name, String email, String sex) {
        this.name = name;
        this.email = email;
        this.sex = sex;
    }

    public YjgithubBean() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

3.3在启动类(CitydataApplication)中加入该bean

ps:
@EnableConfigurationProperties({YjgithubBean.class})

package com.citydata;

import com.citydata.bean.YjgithubBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@EnableConfigurationProperties({YjgithubBean.class})
@SpringBootApplication
public class CitydataApplication {

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

四.测试

4.1 编写测试类(service,impl,TestController)

service

package com.citydata.service;

import com.citydata.response.ReturnData;

/**
 *
 * Created by yjgithub on 2018/5/28.
 */
public interface TestService {
    public ReturnData test(String str);
    public ReturnData exception();
    public ReturnData getSingleValue();
    public ReturnData getBeanValue();
}

impl

package com.citydata.service.impl;

import com.citydata.bean.YjgithubBean;
import com.citydata.config.YjgithubConfig;
import com.citydata.constant.ResponseCode;
import com.citydata.response.ReturnData;
import com.citydata.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by yjgithub on 2018/5/28.
 */
@Service
public class TestImpl implements TestService {
    @Override
    public ReturnData test(String str) {
        ReturnData rd = new ReturnData(ResponseCode.OPERATION_SUCCESS.getStatus(),ResponseCode.OPERATION_SUCCESS.getMsg(),str);
        return rd;
    }

    @Override
    public ReturnData exception() {
        int[] arr = {1,2,3};
        System.out.println(arr[4]);
        ReturnData rd = new ReturnData(ResponseCode.OPERATION_SUCCESS.getStatus(),ResponseCode.OPERATION_SUCCESS.getMsg());
        return rd;
    }

    @Override
    public ReturnData getSingleValue() {
        String name = YjgithubConfig.name;
        ReturnData rd = new ReturnData(ResponseCode.OPERATION_SUCCESS.getStatus(),ResponseCode.OPERATION_SUCCESS.getMsg(),"获取单一属性name:"+name);
        return rd;
    }


    @Autowired
    private YjgithubBean yjgithubBean;
    @Override
    public ReturnData getBeanValue() {
        String allInfo = yjgithubBean.toString();
        ReturnData rd = new ReturnData(ResponseCode.OPERATION_SUCCESS.getStatus(),ResponseCode.OPERATION_SUCCESS.getMsg(),"获取全部属性:"+allInfo);
        return rd;
    }
}

TestController

package com.citydata.controller;

import com.citydata.response.ReturnData;
import com.citydata.service.impl.TestImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by yjgithub on 2018/5/28.
 */
@RestController
@RequestMapping("test")
public class TestController {
    @Autowired
    TestImpl testService;

    @RequestMapping("/index")
    public ReturnData index(String Str) {
        return testService.test(Str);
    }

    @RequestMapping("/exception")
    public ReturnData exception() {
        return testService.exception();
    }

    @RequestMapping("/singleValue")
    public ReturnData singleValue() {
        return testService.getSingleValue();
    }

    @RequestMapping("/beanValue")
    public ReturnData beanValue() {
        return testService.getBeanValue();
    }
}

4.2 访问数据
单一数据:
这里写图片描述

全部数据:
这里写图片描述

到此测试成功!

PS:需要项目源代码,请邮件[email protected]联系

猜你喜欢

转载自blog.csdn.net/yjgithub/article/details/80493977