文章目录
- pom.xml
- LogAspect.java
- StudentServiceImpl.java
- StudentService.java
- applicationContext.xml
- StudentServiceImplTest.java
pom.xml
<?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><parent><groupId>com.aistart</groupId><artifactId>Spring_demo</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>Aspect_demo</artifactId><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.springframework</groupId><artifactId>spring-aop</artifactId><version>5.3.30</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.6</version></dependency></dependencies></project>
LogAspect.java
package com.aistart.aspect;public class LogAspect {public void LogInfoAfter(){System.out.println("后置日志");}public void LogInfoBefore(){System.out.println("前置日志");}
}
StudentServiceImpl.java
package com.aistart.service.impl;import com.aistart.service.StudentService;public class StudentServiceImpl implements StudentService {@Overridepublic void insertStudent() {System.out.println("加入一个学生的业务");}
}
StudentService.java
package com.aistart.service;public interface StudentService {void insertStudent();
}
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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="studentService" class="com.aistart.service.impl.StudentServiceImpl"/><bean id="logAspect" class="com.aistart.aspect.LogAspect"/><aop:config><aop:aspect ref="logAspect"><aop:pointcut id="studentServiceCut" expression="execution(* com.aistart.service.impl.StudentServiceImpl.*(..))"/><aop:pointcut id="studentServiceCut1" expression="execution(* com.aistart.service.impl.StudentServiceImpl.*(..))"/><aop:before method="LogInfoBefore" pointcut-ref="studentServiceCut"></aop:before><aop:after method="LogInfoAfter" pointcut-ref="studentServiceCut"></aop:after></aop:aspect></aop:config></beans>
StudentServiceImplTest.java
package com.aistart.service.impl;import com.aistart.service.StudentService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;import static org.junit.Assert.*;public class StudentServiceImplTest {@Testpublic void insertStudent() {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");StudentService studentService = context.getBean("studentService", StudentService.class);studentService.insertStudent();}
}