@ConfigurationProperties定义:

在这里插入图片描述

Annotation for externalized configuration. Add this to a class definition or a @Bean method in a @Configuration class if you want to bind and validate some external Properties (e.g. from a .properties file).

野鸡翻译:
本注解是用于外部配置的。 如果你想外部属性请把@ConfigurationProperties注解加在一个类上或者 @Configuration class类的@Bean方法上。

方法一:将@ConfigurationProperties加到类上

java代码

package com.xl.test.springtest.test;

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

@ConfigurationProperties(prefix="xl")
@Component
public class CustomerizeConfig {
	
	private String ename;
	
	private String location;

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}
}

配置文件

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring_test?useUnicode=true&characterEncoding=utf8&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=pw@123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

server.port=8087

#Mybatis扫描
mybatis.mapper-locations=classpath*:mapping/*.xml

#自定义配置
xl.ename=micle
xl.location=cqdj
xl.total.height=173

测试

package com.xl.test.springtest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	
	@Autowired
	com.xl.test.springtest.test.CustomerizeConfig customerizeConfig;
	@RequestMapping("/cusconf")
	public String getCunstomerizeConf() {
		return customerizeConfig.getEname();
	}
}

效果

在这里插入图片描述
-----------------------------------------------分割线----------------------------------------------

java代码部分也可以使用如下方式

去掉@Component注解,在springboot启动类上添加一个@Bean方法生成CustomerizeConfig实例给Spring容器管理

去掉注解
在这里插入图片描述
启动类中添加

@Bean
	public CustomerizeConfig customerizeConfig() {
		return new CustomerizeConfig();
	}

完成如下:

package com.xl.test.springtest;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.xl.test.springtest.test.CustomerizeConfig;

@SpringBootApplication
@MapperScan("com.xl.test.springtest.dao")
public class SpringTestApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringTestApplication.class, args);
	}
	
	@Bean
	public CustomerizeConfig customerizeConfig() {
		return new CustomerizeConfig();
	}

}

或者新建一个@Configuration类,在里面同样添加一个@Bean方法生成CustomerizeConfig实例给Spring容器管理。

本分方法同上面方法的区别,只是将启动类里面的@Bean已到了@Configuration类中了

@Configuration类

package com.xl.test.springtest.configclass;

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

import com.xl.test.springtest.pojo.Couple;
import com.xl.test.springtest.pojo.Moon;
import com.xl.test.springtest.test.CustomerizeConfig;

@Configuration
public class ConfigurationAnnotationTest {
	
	@Bean
	public CustomerizeConfig customerizeConfig() {
		return new CustomerizeConfig();
	}
	
	@Bean
	public Moon testBeanAnnotation(Couple couple) {
		Moon moon = new Moon();
		moon.setBrightLevel(couple.getNames()+couple.getLocation()+" say: moon is so bright!");
		moon.setFullLevel("圆");
		return moon;
	}
	
	@Bean
	public Couple setInfo() {
		Couple couple = new Couple();
		couple.setLocation("under the tree");
		couple.setNames("Lily && Lucy");
		return couple;
	}
	
}

在这里插入图片描述
在这里插入图片描述

方法二,将@ConfigurationProperties加在@Configuration类中的@Bean方法上

java代码

package com.xl.test.springtest.configclass;

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

import com.xl.test.springtest.pojo.Couple;
import com.xl.test.springtest.pojo.Moon;
import com.xl.test.springtest.test.CustomerizeConfig;

@Configuration
public class ConfigurationAnnotationTest {
	
	@ConfigurationProperties(prefix="xl")
	@Bean
	public CustomerizeConfig customerizeConfig() {
		return new CustomerizeConfig();
	}
	
	@Bean
	public Moon testBeanAnnotation(Couple couple) {
		Moon moon = new Moon();
		moon.setBrightLevel(couple.getNames()+couple.getLocation()+" say: moon is so bright!");
		moon.setFullLevel("圆");
		return moon;
	}
	
	@Bean
	public Couple setInfo() {
		Couple couple = new Couple();
		couple.setLocation("under the tree");
		couple.setNames("Lily && Lucy");
		return couple;
	}
	
}

测试

在这里插入图片描述

获取自定义配置文件中的值

Springboot获取自定义配置文件的的值

对比@Value注解

在这里插入图片描述
@Value作用于域或者方法/构造器参数

package com.xl.test.springtest.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	
	@Value("${xl.location}")
	String myLocation;
	
	@RequestMapping("/config")
	public String getCunstomerizeConf() {
		return myLocation;
	}
}

测试
在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐