在SpringBoot中将properties配置文件中的信息注入到实体Bean中

SpringBoot使用一些松散的规则来绑定属性到Bean中并支持分层结构

案例一:

配置内容放在mail.properties文件中

mail.host=localhost
mail.port=25
mail.smtp.auth=false
mail.smtp.starttls-enable=false
mail.from=me@localhost
mail.username=hxt
mail.password=123456

实体类:

@Component
@PropertySource("classpath:mail.properties")
@ConfigurationProperties(prefix = "mail")
public class MailProperties {
	private String host;
	private Integer port;
	private Smtp smtp;
	private String from;
	private String username;
	private String password;

    // 省略getter、setter、toString方法
}
public class Smtp {
	private String auth;
	private String starttlsEnable;

	// 省略getter、setter、toString方法
}

测试代码:

@RestController
public class TestController {

	@Autowired
	private MailProperties mailProperties;

	@RequestMapping("/test")
	public String test() {
		return mailProperties.toString();
	}
}

SpringBoot自动会将“-”或者“_”去掉,转换为Java命名规范,如果starttls-enable转为starttlsEnable

案例二:@ConfigurationProperties+@EnableConfigurationProperties

配置内容放在application.properties中

mail.host=localhost
mail.port=25
mail.smtp.auth=false
mail.smtp.starttls-enable=false
mail.from=me@localhost
mail.username=hxt
mail.password=123456
@ConfigurationProperties(prefix = "mail")
public class MailProperties {
	private String host;
	private Integer port;
	private Smtp smtp;
	private String from;
	private String username;
	private String password;

	// 省略getter、setter、toString方法
}
public class Smtp {
	private String auth;
	private String starttlsEnable;

	// 省略getter、setter、toString方法
}

启动类上使用@EnableConfigurationProperties注解

@EnableConfigurationProperties(MailProperties.class)
@SpringBootApplication
public class SpringBootTestApplication {

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

}

测试类:

@RestController
public class TestController {

	@Autowired
	private MailProperties mailProperties;

	@RequestMapping("/test")
	public String test() {
		return mailProperties.toString();
	}
}

案例三:@Bean+@ConfigurationProperties

配置内容放在application.properties中

mail.host=localhost
mail.port=25
mail.smtp.auth=false
mail.smtp.starttls-enable=false
mail.from=me@localhost
mail.username=hxt
mail.password=123456

实体类:

public class MailProperties {
	private String host;
	private Integer port;
	private Smtp smtp;
	private String from;
	private String username;
	private String password;

	// 省略getter、setter、toString方法
}
public class Smtp {
	private String auth;
	private String starttlsEnable;

	// 省略getter、setter、toString方法
}

配置类:

@Configuration
public class MailPropertiesConfig {
	@Bean
	@ConfigurationProperties(prefix = "mail")
	public MailProperties mailProperties() {
		return new MailProperties();
	}
}

测试类:

@RestController
public class TestController {

	@Autowired
	private MailProperties mailProperties;

	@RequestMapping("/test")
	public String test() {
		return mailProperties.toString();
	}
}

注意:

SpringBoot1.5以上版本使用@ConfigurationProperties注解会提示:When using @ConfigurationProperties it is recommended to add ‘spring-boot-configuration-processor’ to your classpath to generate configuration metadata

新版本取消了对location的支持,替代方案是使用@PropertySource和@Configuration进行组合使用,如果使用前缀prefix,这时候还会使用到@ConfigurationProperties,在pom文件中添加以下依赖来解决

在pom.xml

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-configuration-processorartifactId>
			<optional>trueoptional>
		dependency>

参考:https://blog.csdn.net/weixin_41846320/article/details/81566660
https://www.cnblogs.com/duanxz/p/4520571.html

你可能感兴趣的:(在SpringBoot中将properties配置文件中的信息注入到实体Bean中)