1.spring-IOC
org.springframework
spring-context
5.2.13.RELEASE
2.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">
<!-- 对于一个xml文件如果作为框架的配置文件,需要遵守框架的配置规则 -->
<!-- 通常一个框架为了让开发者能够正确的配置,都会提供xml的规范文件(dtd\xsd) -->
</beans>
3.在spring中配置实体类
<!--通过bean将实体类配置给Spring进行管理,id表示实体类的唯一表示-->
<bean id="stu" class="com.qfedu.ioc.bean.Student">
<property name="stuNum" value="10002"/>
<property name="stuName" value="李斯"/>
<property name="stuGender" value="女"/>
<property name="stuAge" value="20"/>
</bean>
4.初始化Spring工厂,获取对象
//1.初始化Spring容器,加载Spring配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.通过Spring容器获取Student对象
Student student2 = (Student) context.getBean("stu");
5.依赖注入
依赖注入的三种方式
spring加载配置后,通过反射创建对象,并给属性赋值
- set注入
- 构造器注入
- 接口注入
set方法注入,在bean标签中通过配置property标签来给属性赋值,实际上通过反射调用set方法,完成属性的注入。
<!--通过bean将实体类配置给Spring进行管理,id表示实体类的唯一表示-->
<bean id="stu" class="com.qfedu.ioc.bean.Student" autowire="byName">
<!-- 简单类型 -->
<property name="stuNum" value="10001"/>
<property name="stuAge" value="12"/>
<!-- 字符串类型-->
<property name="weight" value="62.3"/>
</bean>
<bean id="date" class="java.util.Date"></bean>
<bean id="stu" class="com.qfedu.ioc.bean.Student" >
<!-- 日期类型-->
<property name="enterenceTime" ref="date"/>
</bean>
<bean id="stu" class="com.qfedu.ioc.bean.Student" >
<!-- 日期类型-->
<property name="enterenceTime">
<bean class="java.util.Date"/>
</property>
</bean>
6.Spring IOC的工作原理
7.SpringIOC
因为Spring容器初始化时,只会加载applicationContext.xml文件,那么我们在实体类中添加的注解就不会被Spring扫描,所以我们需要在applicationContext.xml声明Spring的扫描范围
,以达到Spring初始化时扫描带有注解的实体类并完成初始化工作
<!-- 声明使用注解配置 -->
<context:annotation-config/>
<!-- 声明Spring工厂注解的扫描范围 -->
<context:component-scan base-package="com.qfedu.beans"/>
8.注解
- @Controller 注解主要声明将控制器类配置给Spring管理,例如Servlet
- @Service 注解主要声明业务处理类配置Spring管理,Service接口的实现类
- @Repository 直接主要声明持久化类配置给Spring管理,DAO接口
- @Component 除了控制器、servcie和DAO之外的类一律使用此注解声明
@Scope
类注解,用于声明当前类单例模式还是 非单例模式,相当于bean标签的scope属性,@Scope(“prototype”) 表示声明当前类为非单例模式(默认单例模式)
@Lazy`- 类注解,用于声明一个单例模式的Bean是否为懒汉模式- @Lazy(true) 表示声明为懒汉模式,默认为饿汉模式
@PostConstruct- 方法注解,声明一个方法为当前类的初始化方法(在构造器之后执行),相当于bean标签的init-method属性 @PreDestroy
- 方法注解,声明一个方法为当前类的销毁方法(在对象从容器中释放之前执行),相当于bean标签的destory-method属性
@Autowired`- 属性注解、方法注解(set方法),声明当前属性自动装配,默认byType - @Autowired(required = false) 通过requried属性设置当前自动装配是否为必须(默认必须——如果没有找到类型与属性类型匹配的bean则抛出异常)
- byType
- ref引用
@Resource`- 属性注解,也用于声明属性自动装配
- 默认装配方式为byName,如果根据byName没有找到对应的bean,则继续根据byType寻找对应的bean,根据byType如果依然没有找到Bean或者找到不止一个类型匹配的bean,则抛出异常。
9.代理模式
静态代理
动态代理:JDK动态代理,CGLIb动态代理
10.Spring AOP
Aspect Oriented Programming 面向切面编程,是一种利用“横切”的技术(底层实现就是动态代理),对原有的业务逻辑进行拦截,并且可以在这个拦截的横切面上添加特定的业务逻辑,对原有的业务进行增强。
基于动态代理实现在不改变原有业务的情况下对业务逻辑进行增强
11.SpringAOP依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
12.创建spring-aop的命名空间
spring-aop的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="bookDAO" class="com.qfedu.dao.BookDAOImpl"></bean>
<bean id="studentDAO" class="com.qfedu.dao.StudentDAOImpl"></bean>
<!---->
<bean id="txManager" class="com.qfedu.utils.TxManager"></bean>
<aop:config>
<!--声明切入点-->
<aop:pointcut id="book_all" expression="execution(* com.qfedu.dao.*.*(..))"/>
<!--声明txManager为切面类-->
<aop:aspect ref="txManager">
<!--通知-->
<aop:before method="begin" pointcut-ref="book_all"/>
<aop:after method="commit" pointcut-ref="book_all"/>
</aop:aspect>
</aop:config>
</beans>
13.AOP开发步骤
AOP开发步骤`:
1.创建切面类,在切面类定义切点方法
2.将切面类配置给Spring容器
3.声明切入点
4.配置AOP的通知策略
14.AOP注解配置
<!-- 基于注解配置的aop代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
15.注解配置
`注意`:注解使用虽然方便,但是只能在源码上添加注解,因此我们的自定义类提倡使用注解配置;但如果如果使用到第三方提供的类则需要通过xml配置形式完成配置。
16.spring-mybatis
IoC : 控制反转,Spring容器可以完成对象的创建、属性注入、对象管理等工作
AOP : 面向切面,在不修改原有业务逻辑的情况下,实现原有业务的增强
-
IoC支持 SpringIoC 可以为MyBatis完成DataSource、SqlSessionFactory、SqlSession以及DAO对象的创建
-
AOP支持使用Spring提供的事务管理切面类完成对MyBatis数据库操作中的事务管理
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>