Spring系列:Spring实现HelloWord
2014-03-13 11:20
274 查看
Animal.java
package com.project; public class Animal { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Main.java
package com.project; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class Main { public static void main(String[] args) { Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory beanFactory=new XmlBeanFactory(resource); Animal animal=(Animal)beanFactory.getBean("animal"); System.out.println(animal.getName()); } }
applicationContext.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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="animal" class="com.project.Animal"> <property name="name"> <value>Dog</value> </property> </bean> </beans>
运行结果:
相关文章推荐
- cmd设置Jdk环境变量并执行java文件
- java 解析webservice 中的soapheader
- [转]创建节约内存的JavaBean
- java.lang.NullPointerException: Expected timestamp in the Flume event headers, but it was null
- javascritp第十课:面向对象
- Java7编程高手进阶读书笔记—集合框架
- java 三大框架
- Java设计模式之工厂模式
- Java中的equals()和hashCode()契约
- Struts2入门一搭建开发环境及相应案例
- 深入java虚拟机(2)
- 类加载器的工作原理
- 2011级java第一周(春)项目---求2个整数的最大公约数
- Java解析JSON
- java操作sql
- iOS客户端、java服务器的通信用RSA加密
- java io 转
- 关于java SSH的一些知识点的解析
- myeclipse自动对齐代码不换行
- Eclipse EasyExplorer打开文件夹的插件