正方形类
Rectangle
,实现接口
Shape
的方法
double area(double x, double y)
,其中
x
表
示正方形的长,
y
表示正方形的宽。用
Spring
的
AOP
技术编写程序,采取环绕通知方式输
出计算长方形面积的日志。
![](https://img-blog.csdnimg.cn/c6337f459f9549149fbf150e52194ca5.png)
代码比较简单
直接上代码
Rectangle.java
package com.chen.aop;
import org.springframework.stereotype.Service;
@Service("Rectanglearea")
public class Rectangle implements Shape {
@Override
public double area(double x,double y) throws IllegalArgumentException {
if (x<=0||y<=0)
throw new IllegalArgumentException("半径值非法...");
double s = x*y;
System.out.println("正方形面积是:"+s);
return s;
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.chen.aop"/>
</beans>
Shape.java
package com.chen.aop;
public interface Shape {
double area(double x,double y);
}
MyAspect.java
package com.chen.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
@EnableAspectJAutoProxy(exposeProxy=true)
@Component
@Aspect
public class MyAspect {
//public final Logger LOGGER = LogManager.getLogger(this.getClass().getName());
/*
* execution 表达式第一个*表示匹配任意的方法返回值,里有个 空格 !;
* (两个点)表示零个或多个,第一个..表示 module 包及其子包;第二个*表示所有类,
* 第三个*表示所有方法;第二个..表示方法的任意参数个数
*/
@Pointcut("execution(* com.chen.aop..*.*(..))")
public void pointCut(){}
@Around(value = "pointCut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint)
{
Object[] args = proceedingJoinPoint.getArgs();
Object result=null;
try {
//前置通知@Before
System.out.println("环绕前置通知");
//目标方法执行
result = proceedingJoinPoint.proceed(args);
//环绕返回通知@AfterReturning
System.out.println("环绕返回通知");
} catch (Throwable throwable) {
//环绕异常通知@AfterThrowing
System.out.println("环绕异常通知");
throw new RuntimeException(throwable);
} finally {
//最终通知@After
System.out.println("环绕最终通知");
}
return result;
}
}
TestAop.java
import com.chen.aop.Shape;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ComponentScan("com.chen.aop.*")
@EnableAspectJAutoProxy()
public class TestAop {
public static void main(String[] args){
ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");
Shape circle = (Shape) actx.getBean("Rectanglearea");
double d = circle.area(5.0,8.0);
}
}
pom.xml
使用的是jdk8,spring5.2.0
<?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>Work18</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
运行结果