正方形类 Rectangle,实现接口 Shape 的方法 double area(double x, double y),其中 x 表示正方形的长,y 表示正方形的宽。

正方形类 Rectangle ,实现接口 Shape 的方法 double area(double x, double y) ,其中 x
示正方形的长, y 表示正方形的宽。用 Spring AOP 技术编写程序,采取环绕通知方式输
出计算长方形面积的日志。

代码比较简单
直接上代码
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>

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值