Chapter 215 Aspect-Oriented Programming Spring AOP Actual Configuration

LiuJia111222333 2022-08-06 19:02:39 阅读数:150

chapteraspect-orientedaspectorientedprogramming

1集 基于SpringAOPFast implementation⽤⽇志打印《上》

简介:实战SpringAOP配置⽇Chi printing basic preparation 

需求分析:针对Videoservice接⼝实现⽇志打印

三个核⼼包:

  • spring-aopAOP核⼼功能,例如代理⼯⼚ 
  • aspectjweaver:简单理解,⽀持切⼊点表达式
  • aspectjrt:简单理解,⽀持aop相关注解 

定义service接⼝和实现类 

定义横切关注点

 

引⼊相关包:支持相关aop注解

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>

 MavenThe warehouse is switched to Alibaba Cloud:

 <!--The warehouse is switched to Alibaba Cloud-->
<repositories>
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>

2集 基于SpringAOPFast implementation⽤⽇志打印《下》

  •  在bean文件上添加schema

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"
xmlns:aop="http://www.springframework.org/schema/aop"
  • 配置beanaop 
<!--给VideoServiceThe additions, deletions and modifications of the add log function-->
<bean id="VideoService" class="xj.com.springDemo.service.impl.VideoServiceImpl"></bean>
<!--通知类-->
<bean id="timeHandler" class="xj.com.springDemo.aop.TimeHandler"></bean>
<!--配置切面-->
<aop:config>
<!--横切关注点-->
<aop:aspect id="timeAspect" ref="timeHandler">
<!--定义切入点表达式,Which method to cut into,Cut to the front assavthe way to start-->
<!--<aop:pointcut id="AllMethodLogPintCut" expression="execution(* xj.com.springDemo.service.VideoService.sav*(..))"/>-->
<aop:pointcut id="AllMethodLogPintCut" expression="execution(* xj.com.springDemo.service.VideoService.*(..))"/>
<aop:pointcut id="AllTimeLogPointCut" expression="execution(* xj.com.springDemo.service.VideoService.del*(..))"/>
<!--配置前置通知和后置通知,如果id相同,It references the one pointcut that matches the most-->
<aop:before method="Before" pointcut-ref="AllMethodLogPintCut"></aop:before>
<aop:after method="After" pointcut-ref="AllMethodLogPintCut"/>
<!--配置环绕通知-->
<!--<aop:around method="around" pointcut-ref="AllTimeLogPointCut"/>-->
</aop:aspect>
</aop:config>

接口的实现类:

public class VideoServiceImpl implements VideoService {
public int addOrder(VideoOrder videoOrder) {
System.out.println("使用addOrder方法");
return 0;
}
public int delOrder(VideoOrder videoOrder) {
System.out.println("使用delOrder方法");
return 0;
}
public List<VideoOrder> findOrderById() {
System.out.println("使用findOrderById方法");
return null;
}
public int updateOrder(VideoOrder videoOrder) {
System.out.println("使用updateOrder方法");
return 0;
}
public int save(Video video) {
System.out.println("Save a video");
return 0;
}
}

通知类: 

/**
* AOP通知
*/
public class TimeHandler {
public void Before(){
System.out.println("Pre-enhancement processing");
}
public void After(){
System.out.println("后置增强处理");
}
}

测试类:

 //测试Aop打印日志
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
VideoService videoService = context.getBean(VideoService.class);
videoService.addOrder(new VideoOrder());
videoService.save(new Video());
}

执行结果:

 

When there is a return value in the notification class,使用AopThe pointcut will map the return value to the pointcut and the return value type of the pointcut needs to be the same as the return value in the advice class:

        

del实现类方法:

 public Long delOrder(VideoOrder videoOrder) {
System.out.println("使用delOrder方法");
return null;
}

通知类;Returns a usage time

 //Set up a wraparound notification
public Long around(){
Long start = System.currentTimeMillis();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Long end = System.currentTimeMillis();
return end - start;
}

bean和aop配置: Advice can only reference one pointcut in an aspect!        

 <!--给VideoServiceThe additions, deletions and modifications of the add log function-->
<bean id="VideoService" class="xj.com.springDemo.service.impl.VideoServiceImpl"></bean>
<!--通知类-->
<bean id="timeHandler" class="xj.com.springDemo.aop.TimeHandler"></bean>
<!--配置切面-->
<aop:config>
<!--横切关注点-->
<aop:aspect id="timeAspect" ref="timeHandler">
<!--配置切入点-->
<aop:pointcut id="AllTimeLogPointCut" expression="execution(* xj.com.springDemo.service.VideoService.del*(..))"/>
<!--配置环绕通知-->
<aop:around method="around" pointcut-ref="AllTimeLogPointCut"/>
</aop:aspect>
</aop:config>

测试类:


@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
VideoService videoService = context.getBean(VideoService.class);
// videoService.addOrder(new VideoOrder());
//
// videoService.save(new Video());
//执行删除语句
System.out.println(videoService.delOrder(new VideoOrder()));
}

运行结果:

 

copyright:author[LiuJia111222333],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/218/202208061859100989.html