1.spring最重要的两个概念
IOC:控制反转,把对象或相关资源的创建与管理交由IOC容器,实现解耦。
AOP:面向切面编程,把多次用到的模板代码作为切面,代码中某个需要切面的具体位置作为连接点,而切点负责联系切面与连接点,在连接点织入切面。
2.所依赖的jar包
spring-beans:spring核心包,spring中一切资源皆为bean
spring-core:spring核心包,包含一系列bean管理工具
spring-context:spring核心包,应用上下文:可认为是IOC容器,IOC功能实现必导包
spring-aop:AOP功能实现必导包
aspectjweaver:提供完整的aop支持
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SpringProjectTest</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- spring万物皆是bean,提供bean实现--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.7.RELEASE</version> </dependency> <!-- 提供bean管理--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!-- 提供aop支持--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.7.RELEASE</version> </dependency> <!-- spring应用上下文,提供IOC支持--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency> <!-- 提供完整的aop支持--> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency> </dependencies> </project>
3.测试
- 在已经搭建好的Maven工程中,往pom文件导入上述jar包
- 创建相关类
person类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32package com.demo.pojo; /** * @Author YMJ * @Description TODO * @DATE 2019-12-03 20:16 * @Version 1.0 */ public class Person { String name; public Person(){} public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name='" + name + ''' + '}'; } }
personService类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.demo.service; import com.demo.pojo.Person; /** * @Author YMJ * @Description TODO * @DATE 2019-12-04 10:41 * @Version 1.0 */ public class PersonService { public Person show2(Person person){ return person; } }
切面类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50package com.demo.advice; import org.aopalliance.intercept.Joinpoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; /** * @Author YMJ * @Description TODO * @DATE 2019-12-03 22:02 * @Version 1.0 */ public class PersonAdvice { public void before(){ System.out.println("前置通知"); } public void after(){ System.out.println("后置通知"); } public void afterReturning(){ System.out.println("返回通知"); } public void afterThrowing(){ System.out.println("异常通知"); } /** * 环绕通知:可代替上面四个 * @param proceedingJoinPoint */ public void around(ProceedingJoinPoint proceedingJoinPoint){ String methodName = proceedingJoinPoint.getSignature().getName(); System.out.println("环绕通知-前置通知:"+methodName); try { // 表示调用真实的方法 Object obj = proceedingJoinPoint.proceed(); System.out.println("环绕通知-返回通知:"+methodName+" 返回结果:" + obj.toString()); } catch (Throwable throwable) { throwable.printStackTrace(); System.out.println("环绕通知-异常通知:"+methodName); } finally { System.out.println("环绕通知-后置通知:"+methodName); } } }
- 创建spring的配置文件springApplicationContext.xm
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--配置person类交由IOC容器管理--> <bean id="person" class="com.demo.pojo.Person"> <property name="name" value="蔡徐坤"></property> </bean> <bean id="personService" class="com.demo.service.PersonService"></bean> <bean id="personAdvice" class="com.demo.advice.PersonAdvice"></bean> <!--aop配置--> <aop:config> <!--配置切面(表示将会调用切面里的,方法,切点对应的方法交连接点)--> <aop:aspect ref="personAdvice"> <aop:pointcut id="cut" expression=" execution(* com.demo.service.*.*(..))" ></aop:pointcut> <aop:before method="before" pointcut-ref="cut"/> <aop:after method="after" pointcut-ref="cut"/> <aop:after-throwing method="afterThrowing" pointcut-ref="cut"/> <aop:after-returning method="afterReturning" pointcut-ref="cut"/> <aop:around method="around" pointcut-ref="cut"></aop:around> </aop:aspect> </aop:config> </beans>
- 创建测试类
测试IOC功能
测试AOP功能
温馨提示:菜鸡出道,如有错误,请指出,不胜感激。
最后
以上就是着急水池最近收集整理的关于Spring框架学习---IDEA创建最简单spring demo(实现最基本的IOC与AOP)的全部内容,更多相关Spring框架学习---IDEA创建最简单spring内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复