springboot 自动配置@ConfigurationProperties

1.加在@Bean

package com.knife.EhcacheBoot;

public class ServiceA {

	private String id;
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public String toString(){
		return "[id:"+id+",name:"+name+"]";
	}
}
package com.knife.EhcacheBoot;

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

@Configuration
public class Config {

	@Bean
	@ConfigurationProperties(prefix = "service")
	public ServiceA createServiceA() {
		return new ServiceA();
	}

}

配置文件

service.id=001
service.name=test

测试

package com.knife.EhcacheBoot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)//这里的App是springboot的启动类名
@WebAppConfiguration
public class UnitTest {

	
	@Autowired
	ServiceA sa;
	
	@Test
	public void test(){
		System.out.println(sa);
	}
}

结果

[id:001,name:test]

2.加在class上

package com.knife.EhcacheBoot;

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

@ConfigurationProperties(prefix = "service")
public class ServiceA {

	private String id;
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public String toString(){
		return "[id:"+id+",name:"+name+"]";
	}
}
package com.knife.EhcacheBoot;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {

	@Bean
	public ServiceA createServiceA() {
		return new ServiceA();
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对的,Spring Boot的@ConfigurationProperties注解用于将属性值绑定到bean的属性上。通过@ConfigurationProperties注解,我们可以从配置文件中读取属性值,并将其注入到指定的bean属性中。 要使用@ConfigurationProperties注解,需要按照以下步骤进行操作: 1. 在应用的配置类上添加@ConfigurationProperties注解,并指定要绑定的属性前缀。例如,如果要绑定的属性是以"myapp"开头的,可以在配置类上添加@ConfigurationProperties(prefix = "myapp")注解。 2. 在配置类的属性上添加相应的注解,用于指定要绑定的属性名。常用的注解有@Value、@ConfigurationProperties、@Component等。 3. 在应用的配置文件(一般是application.properties或application.yml)中,添加要绑定的属性和对应的值。属性名需要按照一定规则与配置类属性名对应。 通过以上步骤,我们就可以将配置文件中的属性值绑定到bean的属性上,从而实现配置的灵活性和可管理性。 例如,假设有一个名为MyAppConfig的配置类,其中有一个名为name的属性,我们可以这样使用@ConfigurationProperties注解: ```java @Configuration @ConfigurationProperties(prefix = "myapp") public class MyAppConfig { private String name; // getter和setter方法 // 其他属性 } ``` 在应用的配置文件(application.properties或application.yml)中,添加如下配置: ```properties myapp.name=My Application ``` 这样,在应用启动时,Spring Boot会自动配置文件中的属性值注入到MyAppConfig的name属性中。 希望以上解答对你有所帮助,如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值