Spring 使用 properties 文件两种读取方式:xml 直接读取和 @Value 注解获取

Spring 使用 properties 文件两种读取方式:xml 直接读取和 @Value 注解获取

因为我们在交付项目时,我们不应该再让用户去修改代码去配置,但是我们经常需要用户去配置一些自己的内容,不管是对用户保密的也好,让用户自定义也好,肯定有需要用户去定义的部分

常见使用环境:

(1)最经常用到的就是数据库连接配置,用户名密码

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/数据库名?serverTimezone=UTC
jdbc.user=用户名
jdbc.password=密码

(2)还有就是用的一些存储路径

file.upload.location=D:/workspace/Team10/user_file

第一步:加载 properties 文件

只有加载到项目中,才能使用

<context:property-placeholder location="classpath*:jdbcConfig.properties"/>

也可以使用其属性,提升对用户输入内容,容错性

<context:property-placeholder location="classpath*:jdbcConfig.properties" ignore-unresolvable="true"/>

第二步:读取 properties 文件变量

(1)XML 文件直接使用:

不管是哪种方式,加载完只有都可以使用了,例如

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
</bean>

(2)在 Controller 等层使用 @Value 注解读取:

只能作为全局变量,不清楚,下方有示例

//读取properties文件位置变量
@Value("${file.upload.location}")
String realPath;

【注意】:不能放在内部方法里面作为本地变量使用,只能放在public类中,直接作为全局变量使用,例如:

@RestController
@RequestMapping("/logic/studentProject")
public class DspStudentReportController {

    @Autowired
    @Qualifier("dspTeacherStudentServiceImpl")
    private IDspTeacherStudentService dspTeacherStudentService;

    //读取properties文件位置变量
    @Value("${file.upload.location}")
    String realPath;
}
您可以使用yaml库来读取yml文件使用@Value注释将其注入到Spring Boot应用程序中。首先,您需要在pom.xml文件中添加yaml依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.5.4</version> </dependency> ``` 然后,在您的Spring Boot应用程序中,您可以使用以下代码来读取yml文件并将其注入到应用程序中: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource(value = "classpath:application.yml", factory = YamlPropertySourceFactory.class) public class YamlConfig { @Value("${property.key}") private String propertyValue; public String getPropertyValue() { return propertyValue; } } ``` 在上面的代码中,我们使用@PropertySource注释将yml文件路径指定为"classpath:application.yml",并使用YamlPropertySourceFactory类来加载yml文件。然后,我们使用@Value注释将yml属性注入到应用程序中。 请注意,YamlPropertySourceFactory类是一个自定义工厂类,用于将yml文件转换为属性源。以下是该类的代码: ```java import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.support.PropertySourceFactory; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; import java.io.IOException; import java.util.Properties; public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { Resource yamlResource = new ClassPathResource("application.yml"); Properties propertiesFromYaml = loadYamlIntoProperties(yamlResource); String sourceName = name != null ? name : resource.getResource().getFilename(); return new PropertiesPropertySource(sourceName, propertiesFromYaml); } private Properties loadYamlIntoProperties(Resource yaml) throws IOException { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(yaml); Properties properties = factory.getObject(); return properties != null ? properties : new Properties(); } } ``` 这样,您就可以使用@Value注释将yml文件中的属性注入到Spring Boot应用程序中了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肖朋伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值