
一、引言
1.1 原生web开发中存在哪些问题?
二、Spring框架
2.1 概念
2.2 访问与下载
三、Spring架构组成
Spring架构组成 |

|
|
GroupId |
ArtifactId |
说明 |
org.springframework |
spring-beans |
Beans 支持,包含 Groovy |
org.springframework |
spring-aop |
基于代理的AOP支持 |
org.springframework |
spring-aspects |
基于AspectJ 的切面 |
org.springframework |
spring-context |
应用上下文运行时,包括调度和远程抽象 |
org.springframework |
spring-context-support |
支持将常见的第三方类库集成到 Spring 应用上下文 |
org.springframework |
spring-core |
其他模块所依赖的核心模块 |
org.springframework |
spring-expression |
Spring 表达式语言,SpEL |
org.springframework |
spring-instrument |
JVM 引导的仪表(监测器)代理 |
org.springframework |
spring-instrument-tomcat |
Tomcat 的仪表(监测器)代理 |
org.springframework |
spring-jdbc |
支持包括数据源设置和 JDBC 访问支持 |
org.springframework |
spring-jms |
支持包括发送/接收JMS消息的助手类 |
org.springframework |
spring-messaging |
对消息架构和协议的支持 |
org.springframework |
spring-orm |
对象/关系映射,包括对 JPA 和 Hibernate 的支持 |
org.springframework |
spring-oxm |
对象/XML 映射(Object/XML Mapping,OXM) |
org.springframework |
spring-test |
单元测试和集成测试支持组件 |
org.springframework |
spring-tx |
事务基础组件,包括对 DAO 的支持及 JCA 的集成 |
org.springframework |
spring-web |
web支持包,包括客户端及web远程调用 |
org.springframework |
spring-webmvc |
REST web 服务及 web 应用的 MVC 实现 |
org.springframework |
spring-webmvc-portlet |
用于 Portlet 环境的MVC实现 |
org.springframework |
spring-websocket |
WebSocket 和 SockJS 实现,包括对 STOMP 的支持 |
org.springframework |
spring-jcl |
Jakarta Commons Logging 日志系统 |
四、Spring入门
4.1 导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
4.2 在\src\main\resources目录下创建applicationContext.xml
<?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="userDao" class="dao.UserDaoImpl"></bean>
</beans>
4.3 测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = context.getBean(UserDao.class);
System.out.println(userDao);
}
4.4BeanFactory与ApplicationContext区别
ApplicationContext:它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。
BeanFactory:它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。
ApplicationContext对BeanFactory提供了扩展:
国际化处理
事件传递
Bean自动装配
各种不同应用层的Context实现
早期开发使用BeanFactory.
五、Spring对bean的管理细节
5.1创建bean的三种方式
第一种方式:使用默认构造函数创建。
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
<bean id="userDao" class="com.qf.dao.UserDaoImpl"></bean>
第二种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
<bean id="userFactory" class="com.qf.factory.UserFactory"></bean>
<bean id="userDao" factory-bean="userFactory" factory-method="getUserDao"></bean>
第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
<bean id="userDao" class="com.qf.factory.UserStaticFactory" factory-method="getUserDao"></bean>
5.2 bean对象的作用范围
bean标签的scope属性:
作用:用于指定bean的作用范围
取值: 常用的就是单例的和多例的
singleton:单例的(默认值)
prototype:多例的
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
<bean id="userDao" class="com.qf.dao.impl.UserDaoImpl" scope="prototype"></bean>
5.3 bean对象的生命周期
生命周期属性
配置一个方法作为生命周期初始化方法. spring会在对象创建之后立即调用. init-method
配置一个方法作为生命周期的销毁方法. spring容器在关闭并销毁所有容器中的对象之前调用. destory-method
<bean id="userDao" class="com.qf.dao.impl.UserDaoImpl" scope="singleton"
init-method="init" destroy-method="destroy"></bean>
六、spring中的依赖注入
6.1 Spring中的依赖注入
依赖注入:Dependency Injection
IOC的作用:降低程序间的耦合(依赖关系)
依赖关系的管理:以后都交给spring来维护,在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明
依赖关系的维护:就称之为依赖注入。
依赖注入:
能注入的数据:
基本类型和String
其他bean类型(在配置文件中或者注解配置过的bean)
复杂类型/集合类型
注入的方式:
第一种:使用构造函数提供
第二种:使用set方法提供
第三种:使用注解提供
6.2 构造函数注入
使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性:
type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始
name:用于指定给构造函数中指定名称的参数赋值
value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象
优势:
在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:
改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。
6.3 set方法注入
涉及的标签:property
出现的位置:bean标签的内部
标签的属性:
name:用于指定注入时所调用的set方法名称
value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象
优势:
创建对象时没有明确的限制,可以直接使用默认构造函数
弊端:
如果有某个成员必须有值,则获取对象是有可能set方法没有执行。
6.4 复杂类型的注入/集合类型的注入
用于给List结构集合注入的标签:
list array set
用于个Map结构集合注入的标签:
map props
6.5 注解方法注入(需要在applicationContext.xml中的添加context约束)
用于创建对象的
他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的
Component:用于把当前类对象存入spring容器中
value属性:用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母改小写。
Controller:一般用在表现层
Service:一般用在业务层
Repository:一般用在持久层
以上三个注解的作用和属性与Component相同,是spring框架为我们提供明确的三层使用的注解
用于注入数据的
他们的作用就和在xml配置文件中的bean标签中写一个<property>标签的作用是一样的
Autowired:自动按照类型注入
Qualifier:在按照类中注入的基础之上再按照名称注入,value属性:用于指定注入bean的id
Resource:直接按照bean的id注入。它可以独立使用,name属性:用于指定bean的id
注意:以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现,另外,集合类型的注入只能通过XML来实现。因为Resource注解是J2EE的,而不是Spring本身的,所以在使用时需要在pom.xml中导入依赖:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
Value:用于注入基本类型和String类型的数据
用于改变作用范围的
作用和在bean标签中使用scope属性实现的功能是一样的
Scope:用于指定bean的作用范围
value:指定范围的取值。常用取值:singleton prototype
和生命周期相关
作用和在bean标签中使用init-method和destroy-methode的作用是一样的
PreDestroy:用于指定销毁方法
PostConstruct:用于指定初始化方法
在当前applicationContext.xml配置文件中引入其他配置文件
<import resource="bean.xml"></import>