【@ConfigurationProperties获取配置文件的值】
@ConfigurationProperties获取配置文件的值方法一:将@ConfigurationProperties加到类上java代码配置文件测试效果java代码部分也可以使用如下方式去掉@Component注解,在springboot启动类上添加一个@Bean方法生成CustomerizeConfig实例给Spring容器管理或者新建一个@Configuration类,在里面同样添加一个
·
@ConfigurationProperties获取配置文件的值
@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;
}
}
测试
获取自定义配置文件中的值
对比@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;
}
}
测试
更多推荐
已为社区贡献1条内容
所有评论(0)