Spring之@ConfigurationProperties注解

@ConfigurationProperties的使用方式有两种:

  1. 在类上使用
  2. 在工厂方法上使用该注解,和@Bean一起使用

1.在类上使用:

创建一个People实体类:

package com.config.server.endpoint;


public class People {
    private String username;
    private String password;
    private int age;
    private int sex;

	//省略seter、getter方法
}

在配置文件application.properties添加属性:

com.yaomy.demo.username=玛丽
com.yaomy.demo.password=123456
com.yaomy.demo.age=12
com.yaomy.demo.sex=1

在工厂方法上使用@ConfigurationProperties注解:

package com.config.server;

import com.config.server.endpoint.People;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@EnableConfigServer
@SpringBootApplication
public class ServerApplication {

    @Bean(name = "my-bean-test1")
    @ConfigurationProperties(prefix = "com.yaomy.demo")
    public People getBean(){
        return new People();
    }
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ServerApplication.class, args);
        People obj = context.getBean("my-bean-test1", People.class);
        System.out.println(obj.getUsername());
        People obj1 = context.getBean("my-bean-test1", People.class);
        System.out.println(obj1.getSex());
    }
}

2.在工厂方法上使用该注解,和@Bean一起使用:

创建一个People实体类:

package com.config.server.endpoint;


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

@ConfigurationProperties(prefix = "com.yaomy.demo")
public class People {
    private String username;
    private String password;
    private int age;
    private int sex;

	//省略getter setter方法
}

在工厂方法上使用@ConfigurationProperties注解:

package com.config.server;

import com.config.server.endpoint.People;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@EnableConfigServer
@SpringBootApplication
public class ServerApplication {

    @Bean(name = "my-bean-test1")
    public People getBean(){
        return new People();
    }
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ServerApplication.class, args);
        People obj = context.getBean("my-bean-test1", People.class);
        System.out.println(obj.getUsername());
        People obj1 = context.getBean("my-bean-test1", People.class);
        System.out.println(obj1.getSex());
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值