Spring中AOP最简单实例-@注解形式
一、项目结构
二、基于maven(引入Jar包)
在引入pom文件时,需要注意:
第一,因为使用的是结合Spring的,要引入Spring的pom。统一版本为5.1.5.RELEASE。
<properties> <spring.version>5.1.5.RELEASE</spring.version> </properties>
spring相关Jar包引入 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
第三,用到测试类,引入junit 测试包
aspect相关Jar包 <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency>
第四,用到日志,加入log4j
日志相关Jar <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
三、实体类(需要被切面切的类)--HelloWorld
package com.isoftstone.mcb; import org.apache.log4j.Logger; public class HelloWorld { private final static Logger logger = Logger.getLogger(HelloWorld.class); private String name; public String getName() { logger.info("我的名字是 "+ name); return name; } public void setName(String name) { this.name = name; } public void printException(){ logger.info("我扔出了一个IllegalArgumentException异常"); throw new IllegalArgumentException(); } }
四、切面类(切入到别人的类)--注解类--AspectAop
package com.isoftstone.mcb; import org.apache.log4j.Logger; import org.aspectj.lang.annotation.*; @Aspect public class AspectAop { private final static Logger logger = Logger.getLogger(HelloWorld.class); @Pointcut("execution(* com.isoftstone.mcb.HelloWorld.*(..))") private void aspectAll(){ } @Before("aspectAll()") public void beforeAdvice(){ logger.info("我是 beforeAdvice "); } @After("aspectAll()") public void afterAdvice(){ logger.info("我是 afterAdvice "); } @AfterReturning(pointcut = "aspectAll()", returning = "returnValue") public void afterReturning(Object returnValue) { logger.info("afterReturning,返回值"+ returnValue); } @AfterThrowing(pointcut = "aspectAll()", throwing = "ex") public void afterThrowing(Exception ex) { logger.info("我捕获了一个异常信息 "+ ex.toString()); } }
五、配置类(Spring管理)--注解类--SpringConfig
package com.isoftstone.config; import com.isoftstone.mcb.AspectAop; import com.isoftstone.mcb.HelloWorld; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @EnableAspectJAutoProxy public class SpringConfig { @Bean public HelloWorld helloWorld(){ HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("lisi"); return helloWorld; } @Bean public AspectAop aspectAop() { return new AspectAop(); } }
六、log4j配置输出到控制台-log4j.properties
# Define the root logger with appender file log4j.rootLogger = info,stdout,D,E ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout #log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
七、测试类--AopTest
package com.isoftstone; import com.isoftstone.config.SpringConfig; import com.isoftstone.mcb.HelloWorld; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class AopTest { @Test public void testAop() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class); HelloWorld helloWorld = applicationContext.getBean(HelloWorld.class); helloWorld.getName(); helloWorld.printException(); } }
八、测试结果
log4j:ERROR Could not instantiate appender named "E". 我是 beforeAdvice 我的名字是 lisi 我是 afterAdvice afterReturning,返回值lisi 我是 beforeAdvice 我扔出了一个IllegalArgumentException异常 我是 afterAdvice 我捕获了一个异常信息 java.lang.IllegalArgumentException java.lang.IllegalArgumentException