properties文件使用 spring(web项目)

本文主要介绍了在Spring Web项目中如何创建并使用spring-config.xml配置文件,涵盖了配置文件的创建过程及其在项目中的作用。

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

1.新建一个spring-config.xml文件

  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
            	<!-- 
                <value>file:C://important1.properties</value>
                -->
                <value>classpath:important1.properties</value>
                
            </list>
        </property>
        <property name="fileEncoding" value="utf-8"></property>
    </bean>

2.新建一个spring-config-service.xml文件

<bean id="bean1" class="com.xxx.ATest">
        <property name="iconList" value="${com.xxx.iconList}"></property>
</bean>



3.新建一个iconList.properties文件

com.xxx.iconList=http://image1.jpg\
http://image2.jpg\
http://image3.jpg\
上面要使用\   否则注入到java类中的数据只会是
http://image1.jpg\

4.新建一个java文件
public class ATest{
private String iconList;

public static void main(){
     System.out.println(this.iconList);
}

public void setIconList(String iconList){
    this.iconList = iconList;
}

}


5.web.xml中配置如下:

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-config.xml</param-value>
</context-param>
6.当web项目启动时,iconList.propertis文件中键com.xxx.iconList对应的值就会通过bean1 setter注入到ATest的私有成员iconList,保存到内存中


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值