☰
Spring配置文件中加载properties文件
2026/10/13 4:22:26 网站建设 项目流程

目录

  • 一、基于XML
    • 1.配置文件初始格式
    • 2.开启context命名空间
    • 3.使用context空间加载properties文件
    • 4.使用${}读取properties文件
  • 二、基于注解
    • 1.@PropertySource
    • 2.使用${}读取properties文件

一、基于XML

加载properties文件是为了在Spring的Bean配置文件中使用properties中的属性。

1.配置文件初始格式

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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"><beanid="UsersMapperImpl"class="org.example.dao.UsersMapperImpl"/></bean>

2.开启context命名空间

  • 将xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"复制一份并修改为xmlns:context="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为context
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><beanid="UsersMapperImpl"class="org.example.dao.UsersMapperImpl"/></bean>

3.使用context空间加载properties文件

使用同一个context空间可以一次性读取多个properties文件,但是推荐使用*进行后缀匹配加载所有properties文件,且添加classpath:确保读取的是类路径下的properties文件,防止读取到第三方jar包中的properties文件。

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--读取jdbc.properties属性配置文件 system-properties-mode:是否加载环境变量,如果不设置默认可以使用环境变量,且当properties文件中的属性与环境变量重名时会优先使用环境变量 --><context:property-placeholderlocation="classpath:*.properties"system-properties-mode="NEVER"/><beanid="UsersMapperImpl"class="org.example.dao.UsersMapperImpl"/></bean>

4.使用${}读取properties文件

<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--读取jdbc.properties属性配置文件--><context:property-placeholderlocation="jdbc.properties"></context:property-placeholder><!--创建数据源--><beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="driverClassName"value="${jdbc.driverClassName}"></property><propertyname="url"value="${jdbc.url}"></property><propertyname="username"value="${jdbc.username}"></property><propertyname="password"value="${jdbc.password}"></property></bean></bean>

二、基于注解

1.@PropertySource

@Configuration相当于配置文件的初始格式,有关配置类的内容见Spring IoC注解。
@PropertySource用于开启context命名空间和加载properties文件。

@Configuration@ComponentScan("org.example")@PropertySource("jdbc.properties")publicclassMyConfig{}

2.使用${}读取properties文件

@ComponentpublicclassUsersServiceImplimplementsUsersService{@Autowired// 依赖注入UsersMapperusersMapper;@Value("${jdbc.name}")Stringname;}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询