SpringBoot配置文件注入Bean对象使用大全
程序员文章站
2022-06-06 11:22:25
...
通过默认配置文件注入
springboot中默认的配置文件为application.yml或者application.properties文件,所以我们可以将属性配置在这两个文件中进行注入(以application.yml文件为例)
- 添加属性app.name到application.yml文件中
server:
port: 8888
app:
name: HealDemo2
- 在代码中进行注入,将app.name注入到AppConfig.java对象中
将配置文件属性注入到bean对象种主要有两种方式,一种是@ConfigurationProperties方式注入,另一种是@Value方式注入.两种注入方式的具体区别,可参考我的另一篇博客SpringBoot @value注入与@ConfigurationProperties注入对比
此处我们使用@ConfigurationProperties方式注入,这个比较常见与食用
package com.mary.demo.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Created by marylgao on 2020/3/13.
*/
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 对AppConfig.java进行注入并访问(Component注解注入对象即可)
为了测试方便,我使用Controller来注解配置对象,并通过浏览器访问显示结果
package com.mary.demo.controller;
import com.mary.demo.configuration.AppConfig;
import com.mary.demo.configuration.DatabaseConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@Autowired
private AppConfig appConfig;
@RequestMapping(value = "getName", method = RequestMethod.GET)
String getName(){
return appConfig.getName();
}
}
- 测试
通过自定义的配置文件进行注入
- 在classpath路径下添加datasource.properties文件
- 配置datasource.properties属性内容
datasource.username = admin
datasource.password =123456
- 代码中读取配置文件并注入到属性中
package com.mary.demo.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* Created by marylgao on 2020/3/13.
*/
@Configuration
@ConfigurationProperties(prefix = "datasource")
@PropertySource("classpath:datasource.properties")
public class DatabaseConfig {
private String username;
private String password;
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
- 将配置文件bean对象注入到其他bean对象中进行操作,为了测试,我将配置bean对象注入到HellowordController类进行测试
@RestController
public class HelloWorldController {
@Autowired
private DatabaseConfig databaseConfig;
@RequestMapping(value = "getDatasource", method = RequestMethod.GET)
String getDatasourceInfo() {
return databaseConfig.getUsername() + ": " + databaseConfig.getPassword();
}
}
- 测试成功,如下
上一篇: thinkPHP中验证码的简单使用方法
下一篇: 数据结构线性表之链表