编写过程
添加依赖
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.urfread</groupId>
<artifactId>untitled1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>LearnAOP</artifactId>
<name>Archetype - LearnAOP</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.1.5</version>
</dependency>
</dependencies>
</project>
Spring配置文件
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启组件扫描-->
<context:component-scan base-package="com.urfread.learnaop"/>
<!-- 开启aspectj自动代理,为目标对象生成代理-->
<aop:aspectj-autoproxy/>
</beans>
接口类和实现类
package com.urfread.learnaop;
public interface Calculator {
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
int div(int a, int b);
}
package com.urfread.learnaop;
import org.springframework.stereotype.Component;
@Component("calculator")
public class CalculatorImpl implements Calculator{
@Override
public int add(int a, int b) {
int result = a + b;
System.out.println(a+" + "+b+" = "+result);
return result;
}
@Override
public int sub(int a, int b) {
int result = a - b;
System.out.println(a+" - "+b+" = "+result);
return result;
}
@Override
public int mul(int a, int b) {
int result = a * b;
System.out.println(a+" * "+b+" = "+result);
return result;
}
@Override
public int div(int a, int b) {
int result = a / b;
System.out.println(a+" / "+b+" = "+result);
return result;
}
}
切面类
package com.urfread.learnaop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect//切面类
@Component//ioc容器
public class LogAspect {
//设置切入点和通知类型
//切入点表达式:execution(访问修饰符 返回值类型 全类名.方法名(参数列表))
//设置通知类型
// 前置 @Before(value="切入点表达式配置切入点")
@Before("execution(* com.urfread.learnaop.CalculatorImpl.*(..))")
public void beforeMethod(){
System.out.println("前置通知");
}
// 返回 @AfterReturning
// 异常 @AfterThrowing
// 后置 @After
// 环绕 @Around
}
测试类
import com.urfread.learnaop.Calculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Calculator calculator = context.getBean("calculator", Calculator.class);
calculator.add(1,2);
}
}
文章来源地址https://www.toymoban.com/news/detail-847467.html
文章来源:https://www.toymoban.com/news/detail-847467.html
到了这里,关于AOP 面向切面编程 入门练习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!