一、通过 context:property-placeholder 标签实现配置文件加载
复制代码 代码如下:
<context:property-placeholder ignore-unresolvable="true" location="classpath:redis-key.properties"/>
?
1
2
3
4
|
<!-- 基本属性 url、user、password -->
<property name= "url" value= "${jdbc.url}" />
<property name= "username" value= "${jdbc.username}" />
<property name= "password" value= "${jdbc.password}" />
|
3、在java文件中使用:
?
1
2
|
@value ( "${jdbc_url}" )
ivate string jdbcurl; // 注意:这里变量不能定义成static
|
二、通过 util:properties 标签实现配置文件加载
复制代码 代码如下:
<util:properties id="util_spring" local-override="true" location="classpath:jeesite.properties"/>
?
1
2
|
<property name= "username" value= "#{util_spring['jdbc.username']}" />
<property name= "password" value= "#{util_spring['jdbc.password']}" />
|
3、在java文件中使用:
?
1
2
|
@value (value= "#{util_spring['util_service_one']}" )
private string util_service_one;
|
三、通过 @propertysource 注解实现配置文件加载
1、用法示例:在java类文件中使用 propertysource 注解:
?
1
2
3
4
5
|
@propertysource (value={ "classpath:redis-key.properties" })
public class readproperties {
@value (value= "${jdbc.username}" )
private string user_name;
}
|
2、在java文件中使用:
?
1
2
|
@value (value= "${jdbc.username}" )
private string user_name;
|
四、通过 propertyplaceholderconfigurer 类读取配置文件
1、用法示例:在 spring.xml 中使用 <bean>标签进行配置
?
1
2
3
4
5
6
7
|
<bean class = "org.springframework.beans.factory.config.propertyplaceholderconfigurer" >
<property name= "locations" >
<list>
<value>classpath:redis-key.properties</value>
</list>
</property>
</bean>
|
2、 propertyplaceholderconfigurer 配置方法,等价于 方式一,用法参考方法一。
五、 还可以使用 org.springframework.beans.factory.config.propertiesfactorybean 加载,这里不再逐一列举了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/haha_sir/article/details/79105951
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 36
-
2025-05-25 39
-
2025-05-25 58
-
2025-05-25 102
-
2025-05-29 94
热门评论