现在的位置: 首页 > 综合 > 正文

1、Spring快速入门

2018年02月05日 ⁄ 综合 ⁄ 共 7350字 ⁄ 字号 评论关闭

1、Spring是什么?

struts是web框架,Hibernate是O/RM框架

Spring是容器框架,用于配制bean并维护bean之间关系的框架。

Spring中有一个重要概念:Bean(是java中的任何一种对象,javaBean、service、action、数据源、DAO等)

IoC,控制反转,Inverse of Control,或者DI(Dependency Injection 依赖注入)

一个框架图:

快速入门:一个例子

开发一个spring项目

1)、引入spring的jar包(最小配置spring.jar 该包把常用的jar都包括了,还要引入common-logging.jar,日志需要的包)

2)、创建spring的一个核心文件 :applicationContext.xml(spring的配置文件,默认名字,也可以起别的名字)【hibernate有核心hibernate.cfg.xml,struts核心文件struts-config.xml】,该文件一般放在src目录下,该文件中引入xsd文件:可以从给出的案例中拷贝。

创建一个类(bean)UserService

package com.cdtax.service;

public class UserService
{
	private String name;
	
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	
	public void sayHello()
	{
		System.out.println("hello" + name);
		
	}
}

3)配置bean:

<!-- 在容器文件中配置bean(service/dao/domain/action/数据源) -->

<!-- bean元素的作用是,当我们的spring框架加载时候,spring就会自动创建一个bean对象,并放入内存
相当于这样一种效果: UserService userService = new UserService();
		     userService.setName("Tom");
 -->
<bean id="userService" class="com.cdtax.service.UserService">
	<!-- 这里就体现出注入的特性 -->
	<property name="name">
		<value>Tom</value>
	</property>
</bean>

4)在Test中使用:

public class Test
{
	public static void main(String[] args)
	{
		//我们先使用传统的方法,来调用UserService的sayHello方法
//		UserService userService = new UserService();
//		userService.setName("Tom");
//		userService.sayHello();
		
		//我们现在使用spring来完成上面的调用
		//1、得到spring的applicationContext对象(容器对象),spring有一个接口ApplicationContext
		//对于ClassPathXmlApplicationContext("applicationContext.xml"),配置文件必须放在src或其子目录下,假设放在src的com下,名字applicationC.xml,则:
		//如此调用:new ClassPathXmlApplicationContext("com/applicationC.xml")
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService us = (UserService)ac.getBean("userService");
		us.sayHello();
	}
}

在增加一个类ByeService

package com.cdtax.service;

public class ByeService
{
	private String name;

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	
	public void sayBye()
	{
		System.out.println("byeService:" + name);
	}
}

在配置文件中进行配置:

<bean id="userService" class="com.cdtax.service.UserService">
	<!-- 这里就体现出注入的特性 -->
	<property name="name">
		<value>Tom</value>
	</property>
	
</bean>

<bean id="byeService" class="com.cdtax.service.ByeService">
	<property name="name" value="小明">		
	</property>
</bean>

在Test中进行调用:

package com.cdtax.test;

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

import com.cdtax.service.ByeService;
import com.cdtax.service.UserService;

public class Test
{
	public static void main(String[] args)
	{
		//我们先使用传统的方法,来调用UserService的sayHello方法
//		UserService userService = new UserService();
//		userService.setName("Tom");
//		userService.sayHello();
		
		//我们现在使用spring来完成上面的调用
		//1、得到spring的applicationContext对象(容器对象),spring有一个接口ApplicationContext
		//对于ClassPathXmlApplicationContext("applicationContext.xml"),配置文件必须放在src或其子目录下,假设放在src的com下,名字applicationC.xml,则:
		//如此调用:new ClassPathXmlApplicationContext("com/applicationC.xml")
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService us = (UserService)ac.getBean("userService");
		us.sayHello();
		
		//从ac【代表applicationContext容器】中
		ByeService byeService = (ByeService)ac.getBean("byeService");
		byeService.sayBye();
	}
}

这同第一个bean的使用是一样的

如果我们在第一个bean:UserService中增加一个属性:

package com.cdtax.service;

public class UserService
{
	private String name;
	
	private ByeService byeService;

	public ByeService getByeService()
	{
		return byeService;
	}

	public void setByeService(ByeService byeService)
	{
		this.byeService = byeService;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}
	
	public void sayHello()
	{
		System.out.println("hello" + name);
		byeService.sayBye();
	}
}

然后在sayHello()中调用byeService的sayBye()方法,

这时再执行Test,在打印出hello Tom后,出现空指针异常java.lang.NullPointerException,这是因为UserService中不知道byeService,byeService为空,怎么做才能让UserService知道呢?

修改配置文件,对UserService配置进行修改

<bean id="userService" class="com.cdtax.service.UserService">
	<!-- 这里就体现出注入的特性 -->
	<property name="name">
		<value>Tom</value>
	</property>
	<!-- 在userService中引用byeService bean -->
	<property name="byeService" ref="byeService"></property>
</bean>

<bean id="byeService" class="com.cdtax.service.ByeService">
	<property name="name" value="小明">		
	</property>

增加的红色部分,name的byeService指的是UserService的属性名byeService,而ref的byeService是指的引用bean的id,即配置文件中的bean id=“byeService”这个id的值,二者不要混淆。这里就体现了配置文件维护bean之间的关系。

具体运行原理图:

对上面案例进行总结:

spring实际上是一个容器框架,可以配置各种bean(action/service/domain/dao),并且可以维护bean与bean之间的关系,当我们需要使用某个bean的时候,我们可以使用getBean(id)使用即可。

ioc是什么:ioc——inverse of control,控制反转,所谓控制反转,就是把创建对象(bean),和维护对象(bean)的关系的权利从程序中转移到Spring的容器(applicationContext.xml),而程序本身不在维护。

学习框架,最主要的就是学习各种配置。

DI是什么?:DI——Dependency Injection,依赖注入,实际上,Di和ioc是同一个概念,spring设计者认为DI能更准确体现spring的核心。

使用spring,编程的粒度变大,如汇编是指令编程,c语言是语句编程,java是对象编程,spring是组件编程。

applicationContext是重量级的,我们把applicationContext做成单例的:

提供一个工具类

package com.cdtax.util;

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

public class ApplicationContextUtil
{
	private static ApplicationContext ac = null;
	
	private ApplicationContextUtil()
	{
		
	}
	
	static
	{
		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	public static  ApplicationContext getApplicationContext()
	{
		return ac;
	}
}

在Test中,可以直接使用:

((UserService)ApplicationContextUtil.getApplicationContext().getBean("userService")).sayHello();

5)、细节讨论:传统的方法和使用spring的区别

5.1)使用spring,没有new对象,我们把创建对象的任务交给spring框架

2、对于第一张图,web层由struts来充当web层,接管jsp/action/表单,主要体现出了mvc的数据输入、数据处理和数据的显示分离,从大的方面讲,model层包含业务层+dao+持久层,在一个项目中,不一定model的三层都包括,可以灵活搭配。持久层体现了oop,主要解决关系模型和对象模型之间的阻抗

注释:spring相关jar包的说明

Jar文件

说明

外部依赖

spring-aop.jar

这个jar 文件包含在应用中使用Spring AOP 特性时所需的类和源码级元数据支持。使用基于AOP Spring特性,如声明型事务管理(Declarative
Transaction Management),也要在应用里包含这个jar包。 

spring-core,

 (spring-beans, AOP Alliance)

spring-beans.jar

这个jar 文件是所有Spring应用都要用到的,它提供对JavaBean的支持及管理。

spring-core

spring-context.jar

这个jar 文件为Spring 核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。

spring-beans,

(spring-aop, JMX API, EJB API)

spring-context-support.jar

这个jar 文件包含支持UI模版(VelocityFreeMarkerJasperReports),邮件服务,脚本服务(JRuby),缓存CacheEHCache),任务计划Schedulinguartz)方面的类。

spring-context,

(spring-aop, spring-jdbc, Velocity, FreeMarker, JasperReports, BSH, Groovy, JRuby, Quartz, EHCache)

spring-core.jar

这个jar包含spring框架基本的核心工具类,spring其他组件都要使用到这个包里的类。是其他组件的基本核心。

Commons Logging,

(Commons Attributes)

spring-jdbc.jar

这个jar 文件包含对Spring JDBC 数据访问进行封装的所有类。

spring-beans, spring-tx

spring-jms.jar

这个jar包提供了对JMS 1.0.2/1.1的支持类。

spring-beans, spring-tx, JMS API

spring-orm.jar

提供了对JDOJPAHibernateTopLinkiBATIS 的支持

spring-jdbc, (spring-web)

spring-test.jar

测试框架,提供了对 JUnit JNDI mocksServlet API mocksPortlet
API mocks的支持

spring-core,

(spring-context, spring-jdbc, spring-web, JUnit, Servlet API, Portlet API)

spring-tx.jar

提供事务管理功能,提供对JCA DAO的支持

spring-core,

(spring-aop, spring-context, JTA API, JCA API)

spring-web.jar

提供Web 应用开发时,用到Spring 框架时所需的核心类,包括自动载入Web
Application Context 
特性的类、Struts JSF 集成类、文件上传的支持类、Filter 类和大量工具辅助类。 

spring-context, Servlet API,

(JSP API, JSTL)

spring-webmvc.jar

这个jar 文件包含Spring MVC 框架相关的所有类。包括框架的ServletsWeb
MVC
框架,控制器和视图支持。当然,如果你的应用使用了独立的MVC 框架,则无需这个JAR 文件里的任何类。 

spring-web, (spring-context-support)

spring-webmvc-portlet.jar

spring自己实现的一个类似Spring MVC的框架。包括一个MVC框架和控制器。 

spring-web, Portlet API, (spring-webmvc)

spring-webmvc-struts.jar

提供对Struts 1.x 框架的支持

spring-web, Struts API, (spring-webmvc)

开发一个基本的spring Application所需要用到的最小包:

spring-beans.jar,spring-core.jar

开发一个基本的spring WebApplication所需要用到的最小包:

spring-beans.jar,spring-core.jar,spring-web.jar,spring-context.jar,spring-webmvc.jar

使用基本数据访问,持久化工具等数据功能所用到得包:

spring-aop.jar,spring-jdbc.jar, spring-orm.jar, spring-tx.jar

 

 

【上篇】
【下篇】

抱歉!评论已关闭.