1.Spring框架入门

**Spring框架的IOC核心功能快速入门(必须掌握开发的步骤)**
	
	0. 什么是IOC的功能?
		* IOC		-- Inverse of Control,控制反转,将对象的创建权反转给Spring!!
		* 使用IOC可以解决的程序耦合性高的问题!!
	
	1. 步骤一:下载Spring框架的开发包
		* 下载路径为:http://repo.spring.io/webapp/search/artifact/?0&q=spring-framework
		* 解压后的目录结构如下
			* docs			-- API和开发规范
			* libs			-- jar包和源码
				* Spring框架的jar包的特点是每个jar包都有3个(使用的jar包、文档的jar包和源代码的jar包)			
			* schema		-- 约束
	
	2. 步骤二:创建JavaWEB项目,引入Spring的开发包
		* 引入Spring框架IOC核心功能需要的具体的jar包
			* Spring框架的IOC的功能,那么根据Spring框架的体系结构图能看到,只需要引入如下的jar包
				* 在spring-framework-3.2.0.RELEASE/libs/下
					* Beans
					* Core
					* Context
					* Expression Language
			
			* Spring框架也需要引入日志相关的jar包
				* 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1
					* com.springsource.org.apache.commons.logging-1.1.1.jar
				
				* 还需要引入log4j的jar包 spring-framework-3.0.2.RELEASE-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15
					* com.springsource.org.apache.log4j-1.2.15.jar
	
	3. 步骤三:创建对应的包结构,编写Java的类,要注意:以后使用Spring框架做开发,都需要来编写接口与实现类!!
		* com.itcast.demo1
			* UserService			-- 接口
			* UserServiceImpl		-- 具体的实现类
	
	4. 步骤四:想把UserServiceImpl实现类的创建交给Spring框架来管理,需要创建Spring框架的配置文件,完成配置
		* 1. 在src目录下创建applicationContext.xml的配置文件,名称是可以任意的,但是一般都会使用默认名称!!
		
		* 2. 引入spring的约束,需要先找到具体的约束头信息!!
				* spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html
				* 具体的约束如下:		
					<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">
					</beans>
		
		* 3. 完成UserService的配置
			<!-- Spring的快速入门 -->
			<bean id="userService" class="com.itcast.demo1.UserServiceImpl"/>
	
	5. 步骤五:编写测试程序,采用Spring框架的工厂方式来获取到UserService接口的具体实现类!!
		* 具体的代码如下:
			public void demo2(){
				// 使用Spring的工厂:
				ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
				// 通过工厂获得类:
				UserService userService = (UserService) applicationContext.getBean("userService");
				userService.sayHello();

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入schema的约束 -->
<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">

  <!-- 将User对象交给spring容器管理 -->
  <bean name="user" class="cn.itcast.bean.User"></bean>

</beans>

User

public class User {

	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
}

Demo

public class Demo {

	@Test
	public void fun1(){
		
		//1.创建容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//2.向容器要user对象
		User user = (User) ac.getBean("user");
		//3.打印user对象
		System.out.println(user);
	}
}

  

posted @ 2018-03-13 14:07  一日看尽长安花cxjj  阅读(130)  评论(0)    收藏  举报