【MinaFile】【一】Spring读取xml的一种方式

这是通过Spring来读取XML文件的分享。

首先。目录结构如图。是用Maven来进行Spring的Jar包管理。

一、写Maven配置文件

去官网,就能看到Spring提供的Maven配置文件。

<dependencies>
	    <dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring-context</artifactId>
	        <version>4.2.4.RELEASE</version>
	    </dependency>
	</dependencies>

把上面的依赖放入pom中就可以搭建Spring的简单环境了。博主写的这篇文章的时候,Spring的版本最高的4.2.4。反正这个依赖在官网上挺显眼的,就直接拿来用了。




二、写资源文件

2.1 资源文件由两个文件组成,一个filePath.properties和一个applicationContext.xml文件。


2.2 filePath.properties文件中的代码如下。

bjsyPath=123465
sdhlPath=1234
jcjtPath=1231
bjsy=/bjsys/files
sdhl=/sdhl/src/file


2.3 applicationContext.xml文件中的代码如下。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-merge="true">
	<!-- 配置文件的路径 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:filePath.properties</value>
            </list>
        </property>
    </bean>
    <!-- 类Bean。注意:这里的propertyName的名字要和类中的属性一致。不然会报错 -->
	<bean id="propertiesModel" class="com.minafile.model.PropertiesModel">       
	    <property name="bjsyPath" value="${bjsyPath}" />      
	    <property name="sdhlPath" value="${sdhlPath}" />      
	    <property name="jcjtPath" value="${jcjtPath}" />      
	    <property name="bjsy" value="${bjsy}" />      
	    <property name="sdhl" value="${sdhl}" />      
	</bean> 
</beans>

三、写类Model

这里的model的属性字段和配置文件中的bean的属性名一致。

package com.minafile.model;

public class PropertiesModel {
	private String <span style="background-color: rgb(255, 255, 255);">bjsyPath</span>;
	private String sdhlPath;
	private String jcjtPath;
	private String bjsy;
	private String sdhl;
	public String getBjsyPath() {
		return bjsyPath;
	}
	public void setBjsyPath(String bjsyPath) {
		this.bjsyPath = bjsyPath;
	}
	public String getSdhlPath() {
		return sdhlPath;
	}
	public void setSdhlPath(String sdhlPath) {
		this.sdhlPath = sdhlPath;
	}
	public String getJcjtPath() {
		return jcjtPath;
	}
	public void setJcjtPath(String jcjtPath) {
		this.jcjtPath = jcjtPath;
	}
	public String getBjsy() {
		return bjsy;
	}
	public void setBjsy(String bjsy) {
		this.bjsy = bjsy;
	}
	public String getSdhl() {
		return sdhl;
	}
	public void setSdhl(String sdhl) {
		this.sdhl = sdhl;
	}
}

四、可以读了

    

package com.minafile.util;
 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.minafile.model.PropertiesModel;

public class ReadProperties {
	// xml文件名。
	private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
		      "applicationContext.xml");
	public static void main(String[] args) {
		// 读取bean。
		PropertiesModel d = (PropertiesModel) context.getBean("propertiesModel");
		System.out.println(d.getBjsy());
		System.out.println(d.getBjsyPath());
	}
}

五、看输出



最新代码在github上。欢迎fork。

项目名:MinaFile

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值