一、Spring是什么?
- Struts是web框架(Jsp/action/actionForm)
- Hibernate是orm框架,处于持久层
- Spring是容器框架,用于配置bean,维护bean之间关系的框架
- Spring-bean:是java中任何一种对象,可以是java bean/service/action/数据源/dao
- Spring-IOC:控制反转,inverse of control
- Spring-DI:依赖注入,dependency injection
二、Spring层次图
[Web层]
ActionForm.java表单
|
Login.jsp ->Action.java处理器 -> ok.jsp
(Struts充当web,接管jsp/action/表单,主要体现出mvc的数据输入,输入处理,数据显示分离)
[业务层]
User.java UserService.java
(domain对象/java bean/pojo(java简单对象)) 业务层
[dao层]
dao(userDao/hibernateUtil.java)
[持久层]
hibernate(orm框架),持久层
体现了OOP的概念,主要解决对象模型与关系模型的阻抗
[数据库]
Model层包含业务层+dao层+持久层
而Spring框架就是横跨Web层到持久层的,可管理Web层,业务层,dao层,持久层。Spring可配置各个层的组件(bean),并且管理各个组件之间的关系。可配置:
1.Web层action(解决action单例问题)
2.业务层Service/domain/dao
3.持久层数据源
三、快速入门 - 开发一个Spring项目
1.引入Spring开发包
最小配置:Spring.jar(集合了Spring常用包),common-log.jar
2.创建Spring核心文件
applicationContext.xml(就比如hibernate-hibernate.cfg.xml、struts-strutsConfig.xml),该文件一般放在Src目录下。
创建一个Java工程,注意Spring不仅适用于JavaWeb工程,也适用于Java工程,工程目录如上,将所需jar包引入,
- 其中UserService.java代码如下:
public class UserService {
private String name;
private ByeService byeService;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ByeService getByeService() {
return byeService;
}
public void setByeService(ByeService byeService) {
this.byeService = byeService;
}
public void sayHello(){
System.out.println("hello"+name);
byeService.sayBye();
}
}
- 其中ByeService.java代码如下:
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("bye bye"+name);
}
}
3.适用Spring框架
- 在不使用Spring框架情况下,适用常规方法,调用UserService的sayHello方法,在Test.java使用代码如下:
//传统方式调用UserService的sayHello方法
UserService userService = new UserService();
ByeService byeService = new ByeService();
byeService.setName("平");
userService.setName("换");
userService.sayHello();
- 当使用Spring框架时,代码如下:
先新建applicContext.xml文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="byeServices" class="com.service.ByeService">
<property name="name" value="平">
</property>
</bean>
<!-- 相当于UserService userServive = new UserService(); userServive.setName("换"); -->
<bean id="userService" class="com.service.UserService">
<!-- 这里体现注入概念,将“换”属性注入到对象实例中 -->
<property name="name">
<value>换</value>
</property>
<!-- 在userService中引用byeService bean 这样就体现了Spring维护了bean与bean之间的关系,
其中name是UserService声明ByeService名称,ref是声明ByeService bean的Id-->
<property name="byeService" ref="byeServices">
</property>
</bean>
</beans>
其中<bean>元素的作用是,当Spring框架加载的时候,会自动创建一个bean实例,装载置内存中。
在Test.java使用代码如下:
//Spring方式调用UserService的sayHello方法
//aContext代表Spring的容器
ApplicationContext aContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService2 = (UserService)aContext.getBean("userService");
userService2.sayHello();
由于ApplicationContext是个重量级的对象(存储了所有bean),所以ApplicationContext对象的获取只能一次,所以可以通过Util获取。
final public class ApplicationContextUtil {
private static ApplicationContext aContext;
private ApplicationContextUtil(){
}
static{
aContext = new ClassPathXmlApplicationContext("applicationContext.xml");
}
private static ApplicationContext getApplicationContext() {
return aContext;
}
}
4.细节讨论
- 使用Spring框架没有new对象,将此任务交给Spring框架
- Spring框架原理图:
四、总结
Spring是一个容器框架,可以配置各种bean,并且可以维护bean与bean之间的关系,当我们需要某个bean的时候,通过getBean(id)获取。
IOC是什么?
Inverse of Control ,所谓控制反转就是将创建对象(bean),与维护对象(bean)之间关系的区里从程序中转移到spring中容器(applicationContext.xml)中去,而程序本身不再维护。
DI是什么?
dependency injection,依赖注入是IOC的别名,Spring作者认为DI更能准确表示Spring的核心技术。