_Spring技术–AOP–案例分析
-
思路分析:
- 导入坐标(pom.xml)
- 制作连接点方法(原始操作,Dao接口与实现类)
- 制作共性功能(通知类与通知)
- 定义切入点
- 绑定切入点与通知的关系(切面)
-
整体架构如图所示:
-
首先在pom.xml文件夹中导入aspectjweaver包:
-
首先来看业务层的实现类,之中共有3个方法,注意:在updatePoint()方法中是没有打印系统时间这个方法体的:
我们需要做的就是在不修改PersonServiceImpl的情况下在运行updatePoint() 的时候打印系统的时间
-
在aop包下创建AOP的配置类:
-
在SpringConfig中做spring的配置:
-
最后在test文件中做方法测试:
-
-
package com.Alvis.servicetest; import com.Alvis.config.SpringConfig; import com.Alvis.service.PersonService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; // 设定类运行器 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfig.class) public class ServiceTest { @Test public void App_01() { ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class); PersonService personService = apx.getBean(PersonService.class); personService.run(); } @Test public void App_02() { ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class); PersonService personService = apx.getBean(PersonService.class); personService.updatePoint(); } } ================================================ 8月 24, 2022 12:24:35 上午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames 信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener] 8月 24, 2022 12:24:35 上午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners 信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@7714e963, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@20ce78ec, org.springframework.test.context.support.DirtiesContextTestExecutionListener@393671df, org.springframework.test.context.event.EventPublishingTestExecutionListener@56620197] 1661271876399 update Point...and add currentTime 进程已结束,退出代码0
-