本文讲解一个不规范的spring quick start.
1. 下载spring的插件包,什么版本之类的不用管了。反正能用。
spring.jar
http://www.boxcn.net/shared/yg306zac1h
common-logging.jar
http://www.boxcn.net/shared/ix93ziqljv
2. 进入eclipse,File - New - Java Project.
projectname = spring001 ===> Next
在新建导向的第二页,是Java Settings, 选择Libraries -> Add External JARS -> 添加上面2个jar
finish
3. 进入Package Explorer, 在src下新建一个class.
Package = com.java114.spring.test
Name = HelloWordSpring
再复选框:public static void main(String[] args) 钩上
4. 在HelloWordSpring.java 输入以下的代码
package com.java114.spring.test;
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 HelloWordSpring
{
private String msg;
public void setMsg(String msg)
{
this.msg = msg;
}
public void sayHello()
{
System.out.println(msg);
}
public static void main(String[] args)
{
Resource res = new ClassPathResource("com/java114/spring/test/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
HelloWordSpring hello = (HelloWordSpring) factory.getBean("helloBean");
hello.sayHello();
}
}
5. 在和HelloWordSpring.java 相同目录下面,再新建一个xml文件,名字是bean.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="helloBean" class="com.java114.spring.test.HelloWordSpring">
<property name="msg" value="simple spring demo"/>
</bean>
</beans>
为什么这样写,我也不知道,不管他。
6. 鼠标右键选择HelloWordSpring.java, 选择Run As - Java Applications, 得到结果:
2010-6-16 21:39:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/java114/spring/test/bean.xml]
simple spring demo
posted on 2010-06-16 20:13
张辰 阅读(244)
评论(0) 编辑 收藏 所属分类:
Dr. Oops