Spring3 初体验

版本:spring-framework-3.0.5.RELEASE-dependencies

1. 下载相关jar包

    1.1 Spring3框架包

        1.1.1 下载地址: https://github.com/spring-projects/spring-framework

    1.2 Spring3依赖包

        1.2.1 日志包 :commons-logging-1.1.3.jar

             地址: http://commons.apache.org/proper/commons-logging/

2. Demo工程配置:

    2.1 新建工程 org.spring3.demo

    2.2 添加依赖包到工程,所有框架包和日志包

    2.3 在工程下新建包和Bean类org. spring3.demo. Demo

           

package io.spring3.demo;

public class Demo {

	private int demoId;

	private String demoName;

	private String demoDescription;

	public int getDemoId() {
		return demoId;
	}

	public void setDemoId(int demoId) {
		this.demoId = demoId;
	}

	public String getDemoName() {
		return demoName;
	}

	public void setDemoName(String demoName) {
		this.demoName = demoName;
	}

	public String getDemoDescription() {
		return demoDescription;
	}

	public void setDemoDescription(String demoDescription) {
		this.demoDescription = demoDescription;
	}
}

    2.4 在类路径下添加Spring配置文件 beans.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>
	<bean id="demo" class="io.spring3.demo.Demo">
		<property name="demoId">
			<value>1</value>
		</property>
		<property name="demoName">
			<value>Small apple</value>
		</property>
		<property name="demoDescription">
			<value>The hottest Divine Comedy is the Small apple in 2014.</value>
		</property>
	</bean>
</beans>

    2.6 添加测试类 org. spring3.demo.DemoTest

           

package io.spring3.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoTest {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		Demo demo = context.getBean("demo",Demo.class);
		System.out.println(demo.getDemoId());
		System.out.println(demo.getDemoName());
		System.out.println(demo.getDemoDescription());
	}
}

    2.7 运行测试

           
     
 

     

猜你喜欢

转载自ihuning.iteye.com/blog/2189194