Spring 切面 AOP基础 之四
2015-05-10 20:49
369 查看
Spring AOP在企业应用中 通常与AspectJ结合使用,尤其是在注解AOP的时候。
话不多说,talk is cheap,i'll show you code。
引用Pom.xml 加入AspectJ的依赖
2、写service类
接口 Customerbo.java
3、要注解的切面类
LoggingAspect.java
3、配置spring文件,让注解绑定
Spring-Customer.java
4、执行测试,查看效果
结果:我们看到addCustomerAround执行了切面的内容,达到了目的。
注解类型如上四种
附:如果不用注解在LoggingAspect上,也可以在xml文件配置AOP切面。去掉@Aspect等之后,作如下配置
Spring-Customer.xml
话不多说,talk is cheap,i'll show you code。
引用Pom.xml 加入AspectJ的依赖
<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> <groupId>com.mkyong.core</groupId> <artifactId>Spring3Example</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Spring3Example</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring AOP + AspectJ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> <!-- JavaConfig need this library --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> </dependencies> </project>
2、写service类
接口 Customerbo.java
public interface CustomerBo { void addCustomer(); String addCustomerReturnValue(); void addCustomerThrowException() throws Exception; void addCustomerAround(String name); }实现类CustomerboImpl.java
public class CustomerBoImpl implements CustomerBo { public void addCustomer(){ System.out.println("addCustomer() is running "); } public String addCustomerReturnValue(){ System.out.println("addCustomerReturnValue() is running "); return "abc"; } public void addCustomerThrowException() throws Exception { System.out.println("addCustomerThrowException() is running "); throw new Exception("Generic Error"); } public void addCustomerAround(String name){ System.out.println("addCustomerAround() is running, args : " + name); } }
3、要注解的切面类
LoggingAspect.java
import java.util.Arrays; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class LoggingAspect { /*@Before("execution(* CustomerBo.addCustomer(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("logBefore() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("******"); }*/ /*@After("execution(* CustomerBo.addCustomer(..))") public void logAfter(JoinPoint joinPoint) { System.out.println("logAfter() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("******"); }*/ /*@AfterReturning( pointcut = "execution(* CustomerBo.addCustomerReturnValue(..))", returning= "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { System.out.println("logAfterReturning() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("Method returned value is : " + result); System.out.println("******"); }*/ /*@AfterThrowing( pointcut = "execution(* CustomerBo.addCustomerThrowException(..))", throwing= "error") public void logAfterThrowing(JoinPoint joinPoint, Throwable error) { System.out.println("logAfterThrowing() is running!"); System.out.println("hijacked : " + joinPoint.getSignature().getName()); System.out.println("Exception : " + error); System.out.println("******"); }*/ @Around("execution(* CustomerBo.addCustomerAround(..))") public void logAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("logAround() is running!"); System.out.println("hijacked method : " + joinPoint.getSignature().getName()); System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs())); System.out.println("Around before is running!"); Object proceed = joinPoint.proceed(); System.out.println("Around after is running!"); System.out.println("******"); } }
3、配置spring文件,让注解绑定
Spring-Customer.java
<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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy /> <bean id="customerBo" class="CustomerBoImpl" /> <!-- Aspect --> <bean id="logAspect" class="LoggingAspect" /> </beans>
4、执行测试,查看效果
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mkyong.customer.bo.CustomerBo; public class App { public static void main(String[] args) throws Exception { ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Customer.xml"); CustomerBo customer = (CustomerBo) appContext.getBean("customerBo"); //customer.addCustomer(); //customer.addCustomerReturnValue(); //customer.addCustomerThrowException(); customer.addCustomerAround("mkyong"); } }
结果:我们看到addCustomerAround执行了切面的内容,达到了目的。
注解类型如上四种
附:如果不用注解在LoggingAspect上,也可以在xml文件配置AOP切面。去掉@Aspect等之后,作如下配置
Spring-Customer.xml
<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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy /> <bean id="customerBo" class="CustomerBoImpl" /> <!-- Aspect --> <bean id="logAspect" class="LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Before --> <aop:pointcut id="pointCutBefore" expression="execution(* CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> <!-- @After --> <aop:pointcut id="pointCutAfter" expression="execution(* CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> <!-- @AfterReturning --> <aop:pointcut id="pointCutAfterReturning" expression="execution(* CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning" returning="result" pointcut-ref="pointCutAfterReturning" /> <!-- @AfterThrowing --> <aop:pointcut id="pointCutAfterThrowing" expression="execution(* CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing" throwing="error" pointcut-ref="pointCutAfterThrowing" /> <!-- @Around --> <aop:pointcut id="pointCutAround" expression="execution(* CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config> </beans>
相关文章推荐
- spring aop切面编程基础实例
- 0047 Spring的AOP入门基础--切面组件--通知--切入点
- Spring 切面 AOP基础 之二
- 【spring aop切面】基础使用教程
- Spring AOP切面基础 实现请求参数的验证
- Spring基础配置(AOP切面编程)
- 【Spring in Action】Spring的AOP基础知识及切面运用
- Spring 切面 AOP基础 之一
- Spring 切面 AOP基础 之三
- JavaEE框架——Spring入门基础(控制反转Ioc和切面技术Aop)
- Spring 4.0 学习日记(8) ---AOP切面注解实现五种通知
- Spring AOP切面日志Demo 配置文件方式
- Spring AOP(面向切面示例)
- SpringAOP-切面优先级
- 【spring-boot】spring aop 面向切面编程初接触
- Spring基础---配置文件、Ioc、AOP
- Spring的面向切面AOP
- 解释通知Spring AOP 面向切面编程
- Spring AOP 切面与通知【Spring 入门】
- 【第六章】 AOP 之 6.1 AOP基础 ——跟我学spring3