如何替换Application context定义文件中自定义的property

博客介绍了替换Application context定义文件中自定义property的方法。先描述了在相关文件中需自定义property的情况,如datasource定义等。接着给出解决方案,即使用org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer类,并说明了具体用法,还提及定义Application根路径的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何替换Application context定义文件中自定义的property

问题描述:

Application context定义文件(如xxx-servlet.xmlapplicationContext.xml)中,有时需要一些自定义的property,如datasource的定义

       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

              <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>

              <property name="url"><value>${jdbc.url}</value></property>

              <property name="username"><value>${jdbc.username}</value></property>

              <property name="password"><value>${jdbc.password}</value></property>

       </bean>

或者可能有时还需要Application的根路径或系统变量。

解决方案:

       使用org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer

这个类的设计意图就是为了解决这个问题。它首先使用用户自定义的变量,然后使用系统变量,然后是这个配置文件的变量。

具体用法:

1.         在配置文件中声明这个类
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

2.         设定参数(可以为空)

l         location:指定一个properties文件的路径
<property name="location"><value>/WEB-INF/jdbc.properties</value></property>

l         locations:指定一组properties文件的路径
<property name=" locations ">
    <list>
        <value>/WEB-INF/jdbc.properties</value>
        <value>/WEB-INF/classes/messages.properties</value>   
   </list> 
</property>

如果需要Application跟路径,则需要定义Application的根路径,具体步骤如下:

1.         web.xml中,添加

       <context-param>

              <param-name>webAppRootKey</param-name>

              <param-value>petclinic.root</param-value>

       </context-param>

设置跟路径名称(默认为webapp.root

2.         然后声明WebAppRootListener or Log4jConfigListener

       <listener>

              <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

       </listener>

ContextLoaderServlet

       <servlet>

              <servlet-name>context</servlet-name>

              <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

              <load-on-startup>1</load-on-startup>

       </servlet>

如果是上边的声明,则Application的根路径变量的写法是${ petclinic.root }

### Spring Boot 中从 `application.yml` 文件读取自定义配置的方法 在 Spring Boot 应用程序中,可以通过多种方式从 `application.yml` 文件中读取自定义配置项。以下是几种常见的实现方法及其具体示例。 #### 方法一:使用 `@Value` 注解读取单个属性 通过 `@Value` 注解可以直接注入 YAML 文件中的某个特定键值对。例如: ```java import org.springframework.stereotype.Component; @Component public class CustomConfig { @Value("${my.custom.property}") private String customProperty; public String getCustomProperty() { return this.customProperty; } } ``` 上述代码会将 `application.yml` 文件中的 `my.custom.property` 的值赋给 `customProperty` 变量[^1]。 --- #### 方法二:使用 `@ConfigurationProperties` 绑定复杂对象 当需要绑定一组相关的配置时,推荐使用 `@ConfigurationProperties` 注解来映射整个结构化的配置数据。这种方式更加灵活且易于维护。 ##### 步骤 1:创建一个 POJO 类表示配置结构 假设 `application.yml` 文件中有以下内容: ```yaml app: name: MyApp version: 1.0.0 settings: timeout: 3000 retries: 5 ``` 可以创建一个 Java 类来匹配该结构: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "app") // 使用前缀 app 来匹配 yml 中的内容 public class AppConfig { private String name; private String version; private Settings settings; // Getter and Setter methods public static class Settings { private int timeout; private int retries; // Getter and Setter methods } } ``` ##### 步骤 2:启用 `@EnableConfigurationProperties` 如果使用的不是默认扫描路径,则需显式启用支持: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableConfigurationProperties(AppConfig.class) // 显式注册 AppConfig public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 此时即可通过依赖注入的方式获取完整的配置对象[^4]。 --- #### 方法三:结合外部化配置文件 对于某些场景下可能需要动态加载外部资源(如 Nacos 或其他远程服务),则可通过 `bootstrap.yml` 进行初始化设置后再加载本地的 `application.yml` 文件[^3]。例如,在 `bootstrap.yml` 中指定连接地址后,再由应用层调用对应的服务端配置完成最终覆盖逻辑。 --- #### 方法四:手动加载外部属性源 除了自动装配外,还可以利用 `@PropertySource` 定义额外的属性文件位置并将其加入上下文中处理。下面是一个简单的例子展示如何操作: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:custom-config.properties") public class ExternalConfig { @Value("${external.config.value}") private String externalValue; public String getExternalValue() { return this.externalValue; } } ``` 注意这里我们指定了另一个名为 `custom-config.properties` 的文件作为补充输入来源。 --- ### 总结 以上介绍了四种主要途径用于访问存储于 `application.yml` 内部或者关联区域里的个性化设定信息。每种方案各有优劣之处,开发者应依据实际需求选取最合适的策略加以运用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值