Spring4.2.6 ApplicationContext

来源:互联网 发布:网络环境是指什么意思 编辑:程序博客网 时间:2024/05/16 06:30

ClassPathXmlApplicationContext 从类加载路径下 搜索 配置文件

FileSystemXmlApplicationContext 从文件系统相对路径或者绝对路径下去所搜配置文件


beans.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="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">      <bean  id="persion" class="com.han.Persion" >          <property  name="name" value="邱于涵"/>           <property  name="age" value="19"/>            <property  name="job" value="程序员"/>      </bean></beans>
Persion.java


package com.han;/******************************* * Persion类 * CopyRight(C)2016-6-22 11:26:16 * Author:邱于涵 * QQ:1031893464 * ***********************************/public class Persion {private String name;private int age;private String job;public Persion(){}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}}

Entry.java

package com.han;/******************************* *   Java应用程序入口类 * CopyRight(C)2016-6-22 11:26:16 * Author:邱于涵 * QQ:1031893464 * ***********************************/import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Entry {public static void main(String []args){ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");Persion persion=(Persion)act.getBean("persion");System.out.println("这个人的名字是:"+persion.getName());System.out.println("年龄是:"+persion.getAge());System.out.println("职业是:"+persion.getJob());}}

运行结果:

这个人的名字是:邱于涵
年龄是:19
职业是:程序员


0 0
原创粉丝点击