在springboot框架中,配置文件永远是最核心的,小伙伴们知道springboot中配置文件的读取有哪几种常用方式吗?下面就一起来看看吧。
一、使用@ImportResource注解#
这个注解可以用来导入Spring的配置文件,使配置文件中内容注入到配置类中,参数是一个数组,并且可以注入多个配置文件。
示例:
在SpringBoot的resources目录下创建一个xml配置文件beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="configBean" class="com.example.test.config.ConfigBean"> <property name="dbType" value="Oracle"/> <property name="driverClassName" value="jdbc.driver.Oracle.OracleDriver"/> <property name="host" value="127.0.0.1"/> <property name="userName" value="oracle"/> <property name="password" value="oracle"/> </bean> </beans>
创建配置类ConfigBean
package com.example.test.config; import lombok.Getter; import lombok.Setter; import lombok.ToString; /** * @author Vincente * @date 2020/07/12-12:29 * @desc 配置类 **/ @Setter @Getter @ToString public class ConfigBean { private String dbType; private String driverClassName; private String host; private String userName; private String password; }
添加@ImportResource注解,在springboot项目的启动类添加
package com.example.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource(locations = { "classpath:beans.xml" }) public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
测试代码
package com.example.test; import com.example.test.config.ConfigBean; import org.junit.jupiter.api.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.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) class TestApplicationTests { @Autowired private ConfigBean configBean; @Test void testConfigBean() { System.out.println(configBean); } }
结果
ConfigBean(dbType = Oracle, driverClassName = jdbc.driver.Oracle.OracleDriver, host = 127.0 .0 .1, userName = oracle, password = oracle)
二、@ConfigurationProperties注解与@Value注解
示例:
创建配置文件application.yml
db - config: db - type: Oracle driver - class - name: jdbc.driver.Ooracle.OracleDriver host: 127.0 .0 .1 user - name: Oracle password: Oracle server: port: 8080
创建配置类ConfigBean
package com.example.test.config; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @author Vincente * @date 2020/07/12-12:29 * @desc 配置类 **/ @Setter @Getter @ToString @ConfigurationProperties(prefix = "db-config") public class ConfigBean { private String dbType; private String driverClassName; private String host; private String userName; private String password; }
测试代码
package com.example.test; import com.example.test.config.ConfigBean; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @SpringBootTest @RunWith(SpringRunner.class) class TestApplicationTests { @Resource ConfigBean configBean; @Value("${server.port}") private String port; @Test void testConfigBean() { System.out.println(configBean); System.out.println(port); } }
输出结果
ConfigBean(dbType = Oracle, driverClassName = jdbc.driver.Ooracle.OracleDriver, host = 127.0 .0 .1, userName = Oracle, password = Oracle) 8080
以上就是关于springboot配置文件读取方式的所有内容,你了解了吗?还有疑问,想了解更多java项目中常见问题及答案的话,就请关注本网站来了解详情吧。
推荐阅读: