1.什么是spring?
spring是一个轻量级的容器框架,容器内放置的都是java 对象(java bean),当容器启动时,容器为我们创建对象,容器销毁时,对象也会销毁
提供功能:ioc aop springMvc
官方版本
Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container)。 Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能。
IOC控制反转
AOP面向切面编程
1.1spirng的优点
1.方便解耦,简化开发
通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合。有了Spring,用户不必再为单实例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用。
2.AOP编程的支持
通过Spring提供的AOP功能,方便进行面向切面的编程,许多不容易用传统OOP实现的功能可以通过AOP轻松应付。
3.声明式事务的支持
在Spring中,我们可以从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活地进行事务的管理,提高开发效率和质量。
4.方便程序的测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,在Spring里,测试不再是昂贵的操作,而是随手可做的事情。例如:Spring对Junit4支持,可以通过注解方便的测试Spring程序。
5.方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,相反,Spring可以降低各种框架的使用难度,Spring提供了对各种优秀框架(如Struts,Hibernate、Hessian、Quartz)等的直接支持。
6.降低Java EE API的使用难度
Spring对很多难用的Java EE API(如JDBC,JavaMail,远程调用等)提供了一个薄薄的封装层,通过Spring的简易封装,这些Java EE API的使用难度大为降低。
1.2spring体系结构
核心容器
核心容器由spring-core,spring-beans,spring-context,spring-context-support和spring-expression(SpEL,Spring表达式语言,Spring Expression Language)等模块组成,它们的细节如下:
- spring-core模块提供了框架的基本组成部分,包括 IoC 和依赖注入功能。
- spring-beans 模块提供 BeanFactory,工厂模式的微妙实现,它移除了编码式单例的需要,并且可以把配置和依赖从实际编码逻辑中解耦。
- context模块建立在由core和 beans 模块的基础上建立起来的,它以一种类似于JNDI注册的方式访问对象。Context模块继承自Bean模块,并且添加了国际化(比如,使用资源束)、事件传播、资源加载和透明地创建上下文(比如,通过Servelet容器)等功能。Context模块也支持Java EE的功能,比如EJB、JMX和远程调用等。ApplicationContext接口是Context模块的焦点。spring-context-support提供了对第三方库集成到Spring上下文的支持,比如缓存(EhCache, Guava, JCache)、邮件(JavaMail)、调度(CommonJ, Quartz)、模板引擎(FreeMarker, JasperReports, Velocity)等。
- spring-expression模块提供了强大的表达式语言,用于在运行时查询和操作对象图。它是JSP2.1规范中定义的统一表达式语言的扩展,支持set和get属性值、属性赋值、方法调用、访问数组集合及索引的内容、逻辑算术运算、命名变量、通过名字从Spring IoC容器检索对象,还支持列表的投影、选择以及聚合等。
数据访问/集成
数据访问/集成层包括 JDBC,ORM,OXM,JMS 和事务处理模块,它们的细节如下:
(注:JDBC=Java Data Base Connectivity,ORM=Object Relational Mapping,OXM=Object XML Mapping,JMS=Java Message Service)
- JDBC 模块提供了JDBC抽象层,它消除了冗长的JDBC编码和对数据库供应商特定错误代码的解析。
- ORM 模块提供了对流行的对象关系映射API的集成,包括JPA、JDO和Hibernate等。通过此模块可以让这些ORM框架和spring的其它功能整合,比如前面提及的事务管理。
- OXM 模块提供了对OXM实现的支持,比如JAXB、Castor、XML Beans、JiBX、XStream等。
- JMS 模块包含生产(produce)和消费(consume)消息的功能。从Spring 4.1开始,集成了spring-messaging模块。。
- 事务模块为实现特殊接口类及所有的 POJO 支持编程式和声明式事务管理。(注:编程式事务需要自己写beginTransaction()、commit()、rollback()等事务管理方法,声明式事务是通过注解或配置由spring自动处理,编程式事务粒度更细)
Web
Web 层由 Web,Web-MVC,Web-Socket 和 Web-Portlet 组成,它们的细节如下:
- Web 模块提供面向web的基本功能和面向web的应用上下文,比如多部分(multipart)文件上传功能、使用Servlet监听器初始化IoC容器等。它还包括HTTP客户端以及Spring远程调用中与web相关的部分。。
- Web-MVC 模块为web应用提供了模型视图控制(MVC)和REST Web服务的实现。Spring的MVC框架可以使领域模型代码和web表单完全地分离,且可以与Spring框架的其它所有功能进行集成。
- Web-Socket 模块为 WebSocket-based 提供了支持,而且在 web 应用程序中提供了客户端和服务器端之间通信的两种方式。
- Web-Portlet 模块提供了用于Portlet环境的MVC实现,并反映了spring-webmvc模块的功能。
其他
还有其他一些重要的模块,像 AOP,Aspects,Instrumentation,Web 和测试模块,它们的细节如下:
- AOP 模块提供了面向方面的编程实现,允许你定义方法拦截器和切入点对代码进行干净地解耦,从而使实现功能的代码彻底的解耦出来。使用源码级的元数据,可以用类似于.Net属性的方式合并行为信息到代码中。
- Aspects 模块提供了与 AspectJ 的集成,这是一个功能强大且成熟的面向切面编程(AOP)框架。
- Instrumentation 模块在一定的应用服务器中提供了类 instrumentation 的支持和类加载器的实现。
1.3spring-framework下载
官方网址:https://spring.io/projects/spring-framework
下载地址:https://repo.spring.io/libs-release-local/org/springframework/spring/5.0.2.RELEASE/
解压之后如下图

docs 文件为说明文档
libs 为依赖jar包
schema 为spring约束
2.spring核心容器
Spring容器会负责控制程序之间的关系,而不是由程序代码直接控制。Spring为我们提供了两种核心容器,分别为BeanFactory和ApplicationContext
BeanFactory
通过子类XmlBeanFactory 去读取配置 文件:
创建BeanFactory实例时,需要提供Spring所管理容器的详细配置信息,这些信息通常采用XML文件形式来管理,其加载配置信息的语法如下:
BeanFactory beanFactory =
new XmlBeanFactory(new FileSystemResource("F: /applicationContext.xml"));
ApplicationContext
继承了BeanFactory,不但实现了的功能,并且能够增加国际化,资源访问,事件传播等,创建ApplicationContext接口实例,通常采用两种方法。
常用实现类
ClassPathXmlApplicationContext 主要用户获取 resource 路径下的配置
// 大多数使用用第二种
// 通过ApplicationContext 子类ClassPathXmlApplicationContext 去读取配置文件
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:bean1.xml");
FileSystemXmlApplicationContext 主要获取绝对路径的配置
// 通过FileSystemXmlApplicationContext 加载 bean 配置文件 容器按照配置文件 初始化对象
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\Users\\wgz\\Desktop\\qianfeng\\2006\\spring\\spring01\\代码\\java2006_day65_spring01\\src\\main\\resources\\bean1.xml");
官方:
1通过ClassPathXmlApplicationContext创建
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(String configLocation);
ClassPathXmlApplicationContext会从类路径classPath中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作
2.通过FileSystemXmlApplicationContext创建
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(String configLocation);
FileSystemXmlApplicationContext会从指定的文件系统路径(绝对路径)中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。
3.获取Bean实例
创建Spring容器后,就可以获取Spring容器中的Bean。Spring获取Bean的实例通常采用以下两种方法:
- Object getBean(String name);
根据容器中Bean的id或name来获取指定的Bean,获取之后需要进行强制类型转换。
- T getBean(Class requiredType);
根据类的类型来获取Bean的实例。由于此方法为泛型方法,因此在获取Bean之后不需要进行强制类型转换。
3.工程搭建
1.创建java工程,引入依赖
<properties>
<!--在当前pom 或者父类pom 中声明属性 -->
<spirng.version>5.0.16.RELEASE</spirng.version>
</properties>
<dependencies>
<!-- 引入spring 核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spirng.version}</version>
</dependency>
</dependencies>
2.创建再resource创建spring配置文件
<?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">
<!--
再配置文件 告诉容器 需要创建拿下对象 <bean>就是一个对象
-->
<!-- 一个bean 就是一个对象 就是让容器创建一个对象-->
<bean id="student" class="com.qfedu.entity.Student">
<!-- 设置属性值-->
<property name="id" value="1000"></property>
<property name="name" value="小明"></property>
</bean>
</beans>
3.测试
public class BeanFactoryTest {
public static void main(String[] args) {
// 容器加载 配置文件
// BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("C:\\Users\\wgz\\Desktop\\qianfeng\\2006\\spring\\spring01\\代码\\java2006_day65_spring01\\src\\main\\resources\\bean1.xml"))
// Student student = beanFactory.getBean(Student.class);
// 大多数使用用第二种
// 通过ApplicationContext 子类ClassPathXmlApplicationContext 去读取配置文件
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:bean1.xml");
// 通过FileSystemXmlApplicationContext 加载 bean 配置文件 容器按照配置文件 初始化对象
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\Users\\wgz\\Desktop\\qianfeng\\2006\\spring\\spring01\\代码\\java2006_day65_spring01\\src\\main\\resources\\bean1.xml");
// 按照Student.class 去获取一个 Student 对象
Student student = applicationContext.getBean(Student.class);
System.out.println("student:"+student);
}
}
4.控制反转与依赖注入
控制反转 IOC ( Inversion of Control)
将创建对象的权力交给容器,我们只需要告诉容器你需要常见那些对象就可以了,并且容器会一直持有这个对象,管理对象的声明周期,通过**getBean()**获取对象
自建创建
// 自己手动创建对象
Student student = new Student();
容器创建
// spring容器获取对象步骤:
// 1.bean.xml 声明要创建的对象
// 2.使用BeanFactory 的子类 去加载 bean.xml 配置 ,按照配置文件创建 bean对象,并且放置在容器中
// 3. getBean(Student.class); 使用getBean 根据类型获取
依赖注入
DI的全称是Dependency Injection,中文称之为依赖注入
依赖注入主要用来控制容器中对象之间的依赖关系
<!-- 声明创建一个 IStudentDao 类型的对象
class="com.qfedu.dao.impl.IStudentDaoImpl" 根据类创建对象
id="iStudentDao" 就是创建的对象 再容器中的唯一表示 我们可以根据 id 从容器中获取IStudentDaoImpl 示例
-->
<bean id="iStudentDao" class="com.qfedu.dao.impl.IStudentDaoImpl">
</bean>
<!-- 声明在容器中创建一个StudentServiceImpl 示例 -->
<bean id="studentService" class="com.qfedu.service.impl.StudentServiceImpl">
<!-- 通过ref="iStudentDao" 容器中idiStudentDao 的对象 设置到StudentServiceImpl 对应studentDao 属性内 -->
<!-- -->
<property name="studentDao" ref="iStudentDao"></property>
</bean>
测试
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:dibean.xml");
// 根据id 获取 容器中的实例 IStudentDaoImpl
IStudentDao iStudentDao = (IStudentDao) applicationContext.getBean("iStudentDao");
StudentServiceImpl studentService = (StudentServiceImpl) applicationContext.getBean("studentService");
// 不需要手动控制依赖 使用容器控制依赖关系
// studentService.setStudentDao(iStudentDao);
Student student = studentService.findStudentById(100);
System.out.println("student:"+student);
}
5.Bean
什么bean?
baen就是java对象,只不过是由容器创建,并且由容器管理对象生命周期
官方
如果把Spring看做一个大型工厂,则Spring容器中的Bean就是该工厂的产品。要想使用这个工厂生产和管理Bean,就需要在配置文件中告诉它需要哪些Bean,以及需要使用何种方式将这些Bean装配到一起。
Bean的本质就是Java中的对象,而Spring中的Bean其实就是对实体类的引用,来生产Java类对象,从而实现生产和管理Bean 。
<bean id="student" class="com.qfedu.entity.Student">
<!-- 设置属性值-->
<property name="id" value="1000"></property>
<property name="name" value="小明"></property>
</bean>
1.bean属性
id : bean 在容器的唯一标识
name: bean 在容器中一个标识name ,namek可以有多个使用,或者; 分割
注意:name 不可以和其他的id 和名字重复
class:bean 创建时要按照那个类类进行创建
<!--
再配置文件 告诉容器 需要创建拿下对象 <bean>就是一个对象
-->
<!-- 一个bean 就是一个对象 就是让容器创建一个对象
id 容器创建对象的唯一标识
name 也为对象取一个标识 name name可以有多个 使用,或者; 分割
-->
<bean id="student" name="xiaoming,ming" class="com.qfedu.entity.Student">
<!-- 设置属性值-->
<property name="id" value="1000"></property>
<property name="name" value="小明"></property>
</bean>
<!--
再容器中在床一个对象 id student2
name="xiaoming" 每一个bean的名字可以有多个 但是不能和其他bean重名 也不能重复id对应字符串
-->
<bean id="student2" name="xiaoheng" class="com.qfedu.entity.Student">
<property name="id" value="1001"></property>
<property name="name" value="xiaoheng"></property>
</bean>
测试
// 通过FileSystemXmlApplicationContext 加载 bean 配置文件 容器按照配置文件 初始化对象
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\Users\\wgz\\Desktop\\qianfeng\\2006\\spring\\spring01\\代码\\java2006_day65_spring01\\src\\main\\resources\\bean1.xml");
// 按照Student.class 去获取一个 Student 对象
// Student student = applicationContext.getBean(Student.class);
// getBean("student"); 根据id 获取 实例
// getBean("xiaoming") 根据名字获取
//
Student student = (Student) applicationContext.getBean("xiaoming");
System.out.println("student:"+student);
2.bean实例化
三种方式
1.默认无参构造方法
容器调用无参构造方法创建对象
2.静态工厂实例化
好处就是:可以自定创建的对象,并且将对象交给容器管理
实现
a创建静态工厂
/**
* 静态工厂
*/
public class StaticStudentFactory {
/**
* 自定义 Student 对象的创建
*
* // 获取了创建对象的权力
* @return
*/
public static Student createStudent(){
Student student = new Student();
// 比如name 去网络查询 数据库查询
student.setName("xiaoming");
student.setId(1000);
return student;
}
}
b 声明使用工厂创建的对象
<?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">
<!--
bean 像容器声明创建对象 此时创建对象是由静态工厂创建 而不是容器直接创建
class="com.qfedu.factory.StaticStudentFactory" 创建对象的静态工厂
factory-method="createStudent" 制定创建对象的静态工厂方法
-->
<bean id="student" class="com.qfedu.factory.StaticStudentFactory" factory-method="createStudent">
</bean>
</beans>
c 获取对象
public class StaticFactoryTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:factory/staticfactory_bean.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println("student:"+student);
}
}
3.实例工厂实例化
作用和静态工厂实例化一致
a 创建实例工厂
/**
* 实例工厂实例化
*/
public class StudentFactory {
public Student creatStudent(){
Student student = new Student();
// 自定义初始化 student
student.setName("xiaoming");
student.setId(1000);
return student;
}
}
b
<?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">
<!--
声明实例工厂 创建实例工厂对象
-->
<bean id="studentFactory" class="com.qfedu.factory.StudentFactory"></bean>
<!--
通过实例工厂创建对象
factory-bean="studentFactory" 引用实例工厂
factory-method="creatStudent" 调用实例工厂的 creatStudent 创建自定义的Student对象 并放置到容器中
-->
<bean id="student" factory-bean="studentFactory" factory-method="creatStudent">
</bean>
</beans>
c 测试
public class StudentFactoryTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:factory/factorybean.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println("student:"+student);
}
}
**总结:**静态工厂实例化和是工厂实例化
作用是更具自己定义需求创建对象,主要用于dao层连接的初始(解决MySQL密码加密问题)