`
fionajw
  • 浏览: 22826 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

applicationContext.xml读取properties中属性

阅读更多
applicationContext.xml读取properties中属性
两种方法:
1、
<beans xmlns="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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
 <context:property-placeholder location="classpath:uc.properties" />
 <!-- 
 <bean id="configBean"
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations">
     <value>uc.properties</value>
     </property>
 </bean>
 -->
2、

<bean id="configBean"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
     <value>hello.properties</value>
    </property>
</bean>

引用:

<property name="list">
     <list>
      <value>aaa</value>
      <value>${name}</value>
      <ref bean="date" />
     </list>
    </property>

-----------------------------------------------------------------

如果有多个.properties档案,则可以透过locations属性来设定,例如:

  • beans-config.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
   "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans>  
     <bean id="configBean" 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
         <property name="locations">              <list>
                 <value>hello.properties</value>                  <value>welcome.properties</value>                  <value>other.properties</value>
                  </list>          </property>     
     </bean>      <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">          <property name="helloWord">              <value>${onlyfun.caterpillar.helloWord}</value>          </property>          ...      </bean> </beans>
分享到:
评论

相关推荐

    applicationContext.xml等文件.rar

    在IT行业中,尤其是在Java Web开发领域,`applicationContext.xml`、`db.properties`、`log4j.properties`以及`spring-mvc.xml`等文件是非常关键的配置文件,它们各自负责不同的功能,对于一个完整的应用程序来说不...

    Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件6

    本篇文章将深入探讨在Spring Boot中如何在不同路径下读取`applicationContext.xml`配置文件。 首先,了解Spring Boot的默认加载路径。Spring Boot会自动从`src/main/resources`目录下的`application.properties`或`...

    Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件3

    本篇文章将深入探讨在Spring Boot中如何读取不同路径下的`applicationContext.xml`配置文件。 首先,理解Spring Boot的启动流程至关重要。Spring Boot通过`SpringApplication`类来启动,它会默认寻找`src/main/...

    Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件1

    本篇文章将详细探讨如何在Spring Boot项目中读取不同路径下的`applicationContext.xml`配置文件。 首先,了解Spring Boot的核心理念是简化Spring应用程序的初始设置,它默认并不支持从XML配置文件加载bean。然而,...

    Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件5

    本篇文章将深入探讨如何在Spring Boot中读取不同路径下的`applicationContext.xml`配置文件。 1. **配置文件位置**: - Spring Boot默认会查找`src/main/resources`目录下的`applicationContext.xml`。然而,我们...

    spring读取配置文件

    而`placeholder`允许你使用`${property_name}`占位符,这些值可以从属性文件(如`application.properties`)中读取,这样可以在不修改XML配置的情况下轻松更改配置。 例如,你可以在配置文件中定义一个profile: `...

    spring mvc 读取配置文件

    首先,Spring MVC中的配置文件通常是指XML配置文件,如`applicationContext.xml`或`servlet-context.xml`,它们位于`src/main/resources`目录下。这些文件包含了Spring MVC应用的所有组件配置,如控制器(Controller...

    SSH 配置文件

    - `property`:配置Bean的属性值,可以是硬编码或者从属性文件中读取。 - `aop:config`:配置面向切面编程,定义切面、通知和切入点表达式。 - `context:component-scan`:自动扫描指定包下的类,进行组件注册。 ...

    Struts+Spring+Ibatis整合框架搭建配置文档

    接着,在Spring的配置文件`applicationContext.xml`中,需要引入`jdbc.properties`文件,并将其作为数据源配置的一部分。 ```xml &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config....

    java类文件通过$获取properties文件的属性值

    通过以上步骤,我们可以实现在Java类中通过`${}`语法读取`.properties`文件中的属性值的功能。这对于构建可配置性强的应用非常有用,特别是在微服务架构下,每个服务都可以独立配置而不影响其他服务。希望本篇文章能...

    c3p0用法步骤

    在Spring的配置文件`applicationContext.xml`中,首先需要读取`init.properties`文件中的配置: ```xml class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"&gt; &lt;value&gt;...

    加载properties配置文件的几种方法

    如果你不使用Spring,也可以直接通过`InputStream`来读取properties文件。以下是一个简单的示例: ```java InputStream is = getClass().getResourceAsStream("/config.properties"); Properties props = new ...

    【ssm】+整合配置文件

    在`applicationContext.xml`文件中,你会看到Bean的定义和配置,例如定义数据源、事务管理器、DAO(数据访问对象)和Service等组件。此外,Spring还提供了AOP(面向切面编程)支持,可以用来实现如日志记录、权限...

    spring,配置文件从属性文件读取JDBC连接的相关参数

    在Spring配置文件(如`applicationContext.xml`或`beans.xml`)中,我们可以创建一个`Properties` bean来加载`jdbc.properties`: ```xml &lt;bean id="propertyConfigurer" class="org.springframework.beans....

    java源码生成包

    `applicationContext.properties`用于存储应用的属性配置,而`applicationContext.xml`则是Spring的XML配置文件,用于定义bean的声明、依赖注入、事务管理等。Spring是Java领域一个重要的轻量级框架,提供了一种声明...

    SpringValue注解

    Spring Value 注解详解 ...在使用 @Value 注解之前,需要首先在 applicationContext.xml 文件中配置 properties 文件。这种配置方式可以使用 PropertyPlaceholderConfigurer 类来实现。下面是一个示例配置: ```xml ...

    ssh项目实例

    3. **Hibernate配置**:研究`hibernate.cfg.xml`中的数据库配置,实体类的映射,以及`hibernate.properties`中相关的设置。 4. **实体类与数据库表的关系**:学习如何设计Java实体类并用Hibernate的注解进行映射。 5...

Global site tag (gtag.js) - Google Analytics