Springboot(六)自定义配置文件属性值注入两种方法

前言:

    在配置文件中自己定义的值,怎么注入到程序中,比如:mq队列的名称,缓存的key,文件的路径等。


代码:

首先建一个springboot工程

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xhx.springboot</groupId>
    <artifactId>springboot2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot2</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

自己定义一个配置文件test.properties

my.name=xuhaixing
my.age=25
my.sex=man

启动类:

package com.xhx.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Springboot2Application {

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



第一种方式@Value注入:

其中@PropertySource指明要加载的配置文件

@Value("${}"),注入配置文件中的值

package com.xhx.springboot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * made by xuhaixing
 * 18-4-8 下午9:01
 */


@Configuration  //证明这是一个配置类
@PropertySource(value = {"classpath:test.properties"}, ignoreResourceNotFound = true)//可以放多个,{}里面用,分开
public class User {

    //可以不用set方法,直接就能注入,属于注入
    @Value("${my.name}")
    private String name;

    @Value("${my.age}")
    private int age;

    @Value("${my.sex}")
    private String sex;


    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getSex() {
        return sex;
    }

}

可以在controller中把user类注入,然后测试一下:

package com.xhx.springboot.controller;

import com.xhx.springboot.config.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * made by xuhaixing
 * 18-4-8 下午9:48
 */
@RestController
public class HelloController {

    @Autowired
    private User user;

    @RequestMapping(value = "/getUser")
    public String getUser() {
        return user.getName() + " " + user.getSex() + " " + user.getAge();
    }
}


第二种方式 prefix:

package com.xhx.springboot.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * made by xuhaixing
 * 18-4-8 下午9:58
 */
@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "my")  //需要有set方法
public class UserPrefix {
    private String name;
    private int age;
    private String sex;


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

    public void setAge(int age) {
        this.age = age;
    }

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getSex() {
        return sex;
    }
}

然后注入到controller中,测试

package com.xhx.springboot.controller;

import com.xhx.springboot.config.UserPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * made by xuhaixing
 * 18-4-8 下午10:01
 */
@RestController
@EnableConfigurationProperties({UserPrefix.class})  //激活配置,测试了一下,也可以没有
public class HelloPrefixController {

    @Autowired
    private UserPrefix userPrefix;

    @RequestMapping(value = "/getUserPrefix")
    public String getUserPrefix() {
        return userPrefix.getName() + " " + userPrefix.getSex() + " " + userPrefix.getAge();
    }

}

我的github地址


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012326462/article/details/80686462
个人分类: springboot
所属专栏: SpringBoot
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭