Spring是分层的JavaSE/EE应用full-stack轻量级开源框架。
分层:针对三层架构设计: Controller层(表现层)-> Service层(业务层)-> 数据层(DAO)
full-stack(全栈):Spring功能非常强大,能够服务于Java开发过程中的各个层面
轻量级:资源消耗较低,运行速度较快
Spring的体系结构
Spring的两大核心
IOC:控制反转
AOP:面向切面编程
(1) IOC
概念:
-
IoC(Inversion Of Control)控制反转,Spring反向控制应用程序所需要使用的外部资源。
-
Spring控制的资源将全部放置在Spring容器中,该容器称为IoC容器
-
控制反转:将产生对象的控制权由咱们自己交给了Spring;反转:产生对象的控制权
-
IoC是面向对象编程的一种设计原则,可以用来降低代码之间的耦合度
入门案例
步骤:
1.导入spring坐标->pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
2.编写业务层接口与实现类
public interface UserService {
//业务方法
void save();
}
public class UserServiceImpl implements UserService {
public void save() {
System.out.println("user service running...");
}
}
3.建立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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
4.配置所需资源(Service)为spring控制的资源
<!-- 1.创建spring控制的资源-->
<bean id="userService" class="com.demo.service.impl.UserServiceImpl"/>
5.表现层(App)通过spring获取资源(Service实例)
public class UserApp {
public static void main(String[] args) {
//2.加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//3.获取资源
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
}
}
(2)AOP
概念:
-
AOP(Aspect Oriented Programing)面向切面编程,是一种编程范式,隶属于软件工程范畴。
-
AOP基于OOP基础之上进行横向开发,是对 OOP 编程方式的一种补充,并非是取而代之。
-
AOP不是由Spring提出的,最佳开源实现是AspectJ;Spring集成了AspectJ 实现AOP
核心概念:一张图初步了解
-
Joinpoint(连接点):就是方法
-
Pointcut(切入点):就是挖掉共性功能的方法
-
Advice(通知):就是共性功能,最终以一个方法的形式呈现
-
Aspect(切面):就是共性功能与挖的位置的对应关系
-
Target(目标对象):就是挖掉功能的方法对应的类产生的对象,这种对象是无法直接完成最终工作的
-
Weaving(织入):就是将挖掉的功能回填的动态过程
-
Proxy(代理):目标对象无法直接完成工作,需要对其进行功能回填,通过创建原始对象的代理对象实现
开发思想:
解决问题:将共性功能提取出来
入门案例:
1.导入aspectj的坐标
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
2.确认要抽取的功能:并将其制作成方法保存到专用的类中,最后删除原始业务中对应的功能
//1.制作通知类,在类中定义一个方法用于完成共性功能
public class AOPAdvice {
public void function(){
System.out.println("共性功能 run time: " + LocalDateTime.now());
}
3.引入aop命名空间,然后将所有进行AOP操作的资源加载到IoC容器中
-
引入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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--3.开启AOP命名空间-->
-
在applicationContextx.xml中配置userService和通知类AOPAdvice
<bean id="userService" class="com.demo.service.impl.UserServiceImpl"/> <!--2.配置共性功能成为spring控制的资源--> <bean id="myAdvice" class="com.demo.aop.AOPAdvice"/>
-
对比之前新增的改动
4.使用配置的方式描述被抽取功能的位置,并描述被抽取功能与对应位置的关系
<!--4.配置AOP-->
<aop:config>
<!--5.配置切入点: 切入点就是方法的描述-->
<!-- 代表当前项目中所有类的所有方法,都添加共性功能 -->
<aop:pointcut id="pt" expression="execution(* *..*(..))"/>
<!--6.配置切面(切入点与通知的关系)-->
<aop:aspect ref="myAdvice">
<!--7.配置具体的切入点对应通知中那个操作方法-->
<aop:before method="function" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>
5.运行App类
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.save();