目录
spring的概述
什么是spring框架
spring框架就是一个 开源的轻量级的java开发框架,是一个分层的javaEE/SE full-stack一站式开发框架
一站式开发框架:就是对EE开发的每一层都有一个解决方案
- WEB层:springMVC
- service层:spring的Bean管理、spring的声明式事物
- DAO层:spring的jdbc模板,spring的ORM模块
为什么学习spring框架
◆JAVA EE应该更加容易使用。
◆面向对象的设计比任何实现技术(比如JAVA EE)都重要。
◆面向接口编程,而不是针对类编程。Spring将使用接口的复杂度降低到零。(面向接口编程有哪些复杂度?)
◆代码应该易于测试。Spring框架会帮助你,使代码的测试更加简单。
◆JavaBean提供了应用程序配置的最好方法。
◆在Java中,已检查异常(Checked exception)被过度使用。框架不应该迫使你捕获不能恢复的异常。
spring的工厂类
- 老版本的工厂类:BeanFactory
调用getBean()方法的时候,才会生成类的实例
- 新版本的工厂类:AplicationContext
该工厂类有两个实现类,
加载配置文件的时候,就会将Spring管理的类都实例化
- ClassPathXmlApplicationContext :加载类路径下的配置文件
- FileSystemXmlApplicationContext:加载文件系统下的配置文件
spring的配置
XML提示信息的配置
就是在xml文件中写的时候,出现标签的提示信息。
Bean的相关的配置
- bean标签的id和name的配置
- id :使用了约束中的唯一约束。里面不能出现特殊字符的。
- name :没有使用约束中的唯一约束(理论上可以出现重复的,但是实际开发不能出现的)。里面可以出现特殊字符。
- Spring和Struts1框架整合的时候
<bean name=”/user” class=””/>
Bean的生命周期的配置(了解)
init-method :Bean被初始化的时候执行的方法
destroy-method :Bean被销毁的时候执行的方法(Bean是单例创建,工厂关闭)
Bean的作用范围的配置(重点)
- scope :Bean的作用范围
singleton :默认的,Spring会采用单例模式创建这个对象。
prototype :多例模式。(Struts2和Spring整合一定会用到)
- request :应用在web项目中,Spring创建这个类以后,将这个类存入到request范围中。
- session :应用在web项目中,Spring创建这个类以后,将这个类存入到session范围中。
- globalsession :应用在web项目中,必须在porlet环境下使用。但是如果没有这种环境,相对于session。
spring的入门(IOC)
什么是 IOC 和 DI
-
IOC:控制反转,就是将对象交给spring管理,原来是自己new对象,现在是交给程序(spring框架)new对象,所以就是控制反转
- DI:依赖注入,前提必须有IOC的环境,Spring管理这个类的时候将类的依赖的属性注入(设置)进来。
-
面向对象的时候
依赖
Class A{}
Class B{
public void xxx(A a){}
}
继承:is a
Class A{}
Class B extends A{}
聚合:has a
spring入门IOC实例
- 下载spring开发包
- 解压spring开发包
docs:spring的开发规范和API
libs:spring的开发所需要的jar包和源码
schema:spring框架的配置文件所需要的约束
- 创建web项目,引入jar包
引入核心的jar包
- 创建接口和类
package com.dao;
public interface UserDao {
/**
* 用户管理Dao层接口
*/
public void save();
}
======================================================
package com.dao.impl;
import com.dao.UserDao;
public class UserDaoImpl implements UserDao {
/**
* 用户管理Dao层实现类
*/
public void save() {
System.out.println("save方法运行啦");
}
}
**出现的问题**
如果底层的实现切换了,需要修改源代码,能不能不修改程序源代码对程序进行扩展?
- 将实现类交给spring管理
在spring的解压路径下spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html 的html中有scamer约束,复制下来就可以。
<?xml version="1.0" encoding="UTF-8" ?>
<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">
<!--spring的入门配置-->
<bean id="userDao" class="com.dao.impl.UserDaoImpl" ></bean>
</beans>
- 编写测试类
public class SpringDemo {
@Test
public void demo1(){
//创建spring的工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.save();
}
}
spring的Bean管理(XML方式)
Spring的Bean的实例化方式(了解)
Bean已经都交给Spring管理,Spring创建这些类的时候,有几种方式:
- 构造方法创建(默认)
- 创建Bean1类
public class Bean1 {
public Bean1(){
System.out.println("无参的构造方法执行了--");
}
}
- 配置applicationContext.xml文件
<!--无参构造方法实例化-->
<bean id="bean1" class="com.domain.Bean1"></bean>
- 静态工厂实例化创建
- 创建Bean2类 和 BeanFactory类
public class Bean2 {
public Bean1(){
System.out.println("无参的构造方法执行了--");
}
}
=====================================
public class BeanFactory {
public static Bean2 createBean2(){
System.out.println("BeanFactory工厂方法createBean2执行了");
return new Bean2();
}
}
- 配置applicationContext.xml文件
<!--静态工厂实例化-->
<bean id="bean2" class="com.domain.BeanFactory"
factory-method="createBean2"></bean>
- 实例工厂实例化创建
- 创建Bean3类 和 BeanFactory类
public class Bean3 {
public Bean1(){
System.out.println("无参的构造方法执行了--");
}
}
=====================================
public class BeanFactory {
public Bean3 createBean3(){
System.out.println("BeanFactory工厂方法crateBean3执行了");
return new Bean3();
}
}
- 配置applicationContext.xml文件
<!--实例化工厂的实例化方式-->
<bean id="beanFactory" class="com.domain.BeanFactory"></bean>
<bean id="bean3" factory-bean="beanFactory" factory-method="createBean3"></bean>
spring的属性注入(DI)
DI:依赖注入,前提必须有IOC的环境,当spring管理这个类的时候,也就是new这个对象的时候,要把这个对象需要的属性的值注入进来,属性有基本类型,引用类型、集合,数组
- 构造方法注入
必须要有有参的构造方法,才能实现属性的注入。在注入引用类型的变量,使用ref属性,属性的值是交给spring管理的对象的name属性的值
Car.java类
public class Car {
private String name;
private double price;
private Bean1 bean1;
public Car(String name, double price, Bean1 bean1) {
this.name = name;
this.price = price;
this.bean1 = bean1;
}
}
applicationContext.xml文件
<!--sping的注入,前提创建对象是根据构造方法创建的对象-->
<!--Car类中必须有有参的构造方法才可以!,引用的是交给spring管理的类id=bean1 -->
<bean id="bean1" class="com.domain.Bean1"></bean>
<bean id="car" class="com.domain.Car">
<!--spring的基本类型注入-->
<constructor-arg name="name" value="奔驰"></constructor-arg>
<constructor-arg name="price" value="12"></constructor-arg>
<!--spring的引用类型注入-->
<constructor-arg name="bean1" ref="bean1"></constructor-arg>
</bean>
- set方法注入
Customer.java类
public class Customer {
private String name;
private Car car;
public void setName(String name) {
this.name = name;
}
public void setCar(Car car) {
this.car = car;
}
public Customer() {
System.out.println("无参构造执行了");
}
public Customer(String name, Car car) {
this.name = name;
this.car = car;
}
}
applicationContext.xml
<!--spring的set方法的注入,前提是这个类实例化是根据的构造方法实例化的,
然后属性的注入是根据set方法注入的-->
<bean id="customer" class="com.domain.Customer">
<property name="name" value="王五"></property>
<!--spring的引用类型注入-->
<property name="car" ref="car"></property>
</bean>
- p命名空间注入(spring2.5版本之后)
需要在aplicationContext.xml文件中加上scamer约束
- 通过引入p名称空间完成属性的注入:
- 写法:
- 普通属性 p:属性名=”值”
- 对象属性 p:属性名-ref=”值”
Customer.java类与上面一样
applicationContext.xml
<!--使用P名称空间注入,实际上使用的Customer类中的set方法注入的-->
<bean id="customer1" class="com.domain.Customer" p:name="王二" p:car-ref="car"></bean>
- SpEL注入
Customer.java类与上面一样
applicationContext.xml
<!--使用SpEL注入-->
<!--SpEL注入的时候,可以用对象里面的属性添加到要注入的对象中的属性中去,
也可以用对象里面的方法返回的值注入到要注入的对象中的属性中去,这只是获得要注入的值。
将要注入的值注入到要注入对象的属性上,还是使用的set方法注入的要注入的对象中的。
-->
<bean id="customer" class="com.domain.Customer">
<!--<property name="name" value="#{car.name}"></property>-->
<property name="name" value="#{car.carName()}"></property>
<!--<property name="name" value="#{'王二'}"></property>-->
<property name="car" value="#{car}"></property>
</bean>
集合属性注入(了解)
集合类型的注入也是根据set方法注入的,具体配置如下
<bean id="customer" class="com.domain.Customer">
<!-- 数组类型 -->
<property name="list" >
<list>
<!--基本类型的注入-->
<value>嘿嘿</value>
<!--引用类型的注入-->
<ref bean="car"></ref>
</list>
</property>
<!-- 注入list集合 -->
<property name="list">
<list>
<value>李兵</value>
<!--引用类型的注入-->
<ref bean="car"></ref>
</list>
</property>
<!-- 注入set集合 -->
<property name="set">
<set>
<value>aaa</value>
<!--引用类型的注入-->
<ref bean="car"></ref>
</set>
</property>
<!-- 注入Map集合 -->
<property name="map" >
<map>
<!--基本类型的注入-->
<entry key="aaa" value="111"></entry>
<!--引用类型的注入-->
<entry key="bbb" value-ref="car"></entry>
</map>
</property>
</bean>
spring的分模块开发配置
- 在加载配置文件的时候,连续加载多个配置文件
ApplicationContext applicationContext
= new ClassPathXmlApplicationContext(
"applicationContext.xml","applicationContext2.xml");
- 在一个配置文件中引入多个配置文件,加载的时候就一起加载
在applicationContext.xml配置文件中加入
<!--在一个配置文件中引入多个配置文件-->
<import resource="applicationContext2.xml"></import>
CRM综合案例
代码实现
- 创建数据库和表
- 创建web项目,引入jar包
- 引入struts2的开发的jar包
- 引入struts2的配置文件
web.xml文件
struts.xml文件
- 引入页面
- 编写Action类
- 配置Action
- 修改页面提交到Action
- 编写Action的save方法
不在Action中直接创建Service,将Service交给Spring管理。
创建Service接口和实现类
- 1.8.1.8 引入Spring的环境
引入jar包
引入配置文件
- 将Service交给Spring
- 在Action中调用Service
- 编写DAO并且完成配置
- 在Service中使用DAO
问题描述
- 程序问题
每次请求都会创建一个Spring的工厂,这样浪费服务器资源,应该一个项目只有一个Spring的工厂。
在服务器启动的时候,创建一个Spring的工厂。
创建完工厂,将这个工厂类保存到ServletContext中。
每次使用的时候都从ServletContext中获取。
- 使用ServletContextListener
监听ServletContext对象的创建和销毁。
- 解决方案
使用Spring核心监听器ContextLoaderListener(整合web项目)
- 引入jar包
spring-web.jar
- 配置监听器
默认加载的配置文件applicationContext.xml文件在/WEB-INF/applicationContext.xml目录下,可以通过配置文件进行修改,使其加载src目录下的xml文件
修改路径的方法
Spring配置文件在WEB-INF下面
这种情况你可以不去管他,不进行配置,因为spring会默认去加载,如果一定要配置呢,可以这样
WEB-INF/applicationContext.xmlSpring配置文件在WEB-INF下的某个文件夹下,比如config下,可以这样配置
WEB-INF/config/applicationContext.xmlSpring配置文件在src下面,可以这样配置
WEB-INF/classes/applicationContext.xml 或者
classpath:applicationContext.xmlSpring配置文件在src下的某个包里,比如com.config,可以这样配置
WEB-INF/classes/com/config/applicationContext.xml 或者
classpath:com/config/applicationContext.xml
- 在Action中获取工厂