三、Spring框架的第一个案例

一、Spring的JAR包注意事项


	spring各个版本中:
			在3.0以下的版本,源码有spring中相关的所有包【spring功能 + 依赖包】
				如2.5版本;
			在3.0以上的版本,源码中只有spring的核心功能包【没有依赖包】
				(如果要用依赖包,需要单独下载!)


二、Hello World

2.1 引入jar文件
1) 源码, jar文件:spring-framework-3.2.5.RELEASE
	
			commons-logging-1.1.3.jar           日志
			spring-beans-3.2.5.RELEASE.jar        bean节点
			spring-context-3.2.5.RELEASE.jar       spring上下文节点
			spring-core-3.2.5.RELEASE.jar         spring核心功能
			spring-expression-3.2.5.RELEASE.jar    spring表达式相关表

	以上是必须引入的5个jar文件,在项目中可以用户库管理!
2.2 在org/jsoft/demo引入applicationContext.xml(或bean.xml)配置文件
<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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	
	
	<bean id="user"  class="org.jsoft.a_hello.User"></bean>

</beans>   
2.3 创建实体
public class User {
	
	private String name;
	private int age;
	
	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;
	}
	
	
}
2.4 测试
public class App {
	
	@Test
	public void testIOC() {
		
		//加载配置文件,创建Bean工程
		Resource rs=(Resource) new ClassPathResource("org/jsoft/a_hello/applicationContext.xml");
		BeanFactory factory=new XmlBeanFactory(rs);
		
		//获取对象
		User u=(User) factory.getBean("user");
		System.out.println(u.getAge());
	
	}
	
	@Test
	public void testIOC2() {
		
		//加载配置文件,创建Bean工程
		ApplicationContext ac=new ClassPathXmlApplicationContext("org/jsoft/a_hello/applicationContext.xml");
		
		//获取对象
		User u=(User) ac.getBean("user");
		System.out.println(u.getAge());
	
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值