首页 > 代码库 > spring4 aop annotation
spring4 aop annotation
package com.zrd.aop.annotation; /** * 第一步:定义一个接口 * * @author ZRD * */ public interface IMyService { int add(int i, int j); }
package com.zrd.aop.annotation; import org.springframework.stereotype.Component; /** * 第二步:定义接口的实现类 * * @author ZRD * */ @Component public class MyServiceImpl implements IMyService { @Override public int add(int i, int j) { int result = i +j; System.out.println("add 正在执行... " + result); return result; } }
package com.zrd.aop.annotation; 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; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * 第三步:声明一个切面 * * 记录日记的切面 * * @author ZRD * */ @Aspect @Component public class LogAspect { /** * 切入点:表示在哪个类的哪个方法进行切入。配置有切入点表达式 */ @Pointcut("execution(* com.zrd.aop.annotation.*.*(..))") public void pointcutExpression() { } /** * 1 前置通知 * @param joinPoint */ @Before("pointcutExpression()") public void beforeMethod(JoinPoint joinPoint) { System.out.println("前置通知执行了"); } /** * 2 后置通知 */ @After("pointcutExpression()") // 在方法执行之后执行的代码. 无论该方法是否出现异常 public void afterMethod(JoinPoint joinPoint) { System.out.println("后置通知执行了,有异常也会执行"); } /** * 3 返回通知 * * 在方法法正常结束受执行的代码 * 返回通知是可以访问到方法的返回值的! * * @param joinPoint * @param returnValue * */ @AfterReturning(value = http://www.mamicode.com/"pointcutExpression()", returning = "returnValue")><!-- 第四步:spring配置文件:配置IOC自动扫描和AOP切面自己生产目标代理对象 --> <?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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 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-4.0.xsd"> <context:component-scan base-package="com.zrd.aop.*"></context:component-scan> <!-- 使用注解配置切面 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 使用XML配置切面 --> <!-- <aop:config> <aop:pointcut expression="" id=""/> <aop:aspect> <aop:after method=""/> </aop:aspect> </aop:config> --> </beans> package com.zrd.aop.annotation; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 第五步:测试 * * @author ZRD * */ public class Main { /** * @param args */ public static void main(String[] args) { ApplicationContext ax = new ClassPathXmlApplicationContext("applicationContext.xml"); /** 使用切面时,一定要这样写,不然无法注入bean **/ //IMyService service = ax.getBean(MyServiceImpl.class); IMyService service = (IMyService) ax.getBean("myServiceImpl"); service.add(2, 8); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。