2023-09-07
<?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>com.hh</groupId> <artifactId>SpringAOP</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.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.11.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency> </dependencies> </project>
BookDao
package com.hh.dao; /** * @author hh * @version 1.0 * @DATE 2023-09-07 15:29:13 */ public interface BookDao { public void save(); public void update(); }
BookDaoImpl
package com.hh.dao.impl; import com.hh.dao.BookDao; import org.springframework.stereotype.Repository; /** * @author hh * @version 1.0 * @DATE 2023-09-07 15:29:53 */ @Repository public class BookDaoImpl implements BookDao { @Override public void save() { System.out.println(System.currentTimeMillis()); System.out.println("book dao save..."); } @Override public void update() { System.out.println("book dao update..."); } }
MyAdvice
package com.hh.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * @author hh * @version 1.0 * @DATE 2023-09-07 15:39:41 */ @Component @Aspect public class MyAdvice { @Pointcut("execution(void com.hh.dao.BookDao.update())") private void pt(){} @Before("pt()") public void method(){ System.out.println(System.currentTimeMillis()); } }
SpringConfig
package com.hh.comfig; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * @author hh * @version 1.0 * @DATE 2023-09-07 15:31:16 */ @Configuration @ComponentScan({"com.hh"}) @EnableAspectJAutoProxy(proxyTargetClass = true) public class SpringConfig { }
标签:示例,spring,void,hh,aop,org,import,com,public From: https://www.cnblogs.com/isDaHua/p/17685528.html