AspectJ 使用注解替代xml

@Service("userService")
public class UserServiceImpl implements IUserService {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public void addUser() {
        System.out.println("add user...." );
    }

    @Override
    public void deleteUser() {
        System.out.println("delete user...." );
    }


}
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns xml namespace:xml命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p ="http://www.springframework.org/schema/p"
       xmlns:context ="http://www.springframework.org/schema/context"
       xmlns:aop ="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="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.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!--配置扫描注解位置-->
    <context:component-scan base-package="com.gyf"/>

    <!--配置aop注解生效-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!--aop配置-->
    <aop:config>
        <aop:aspect ref="myAspect2"></aop:aspect>
    </aop:config>
</beans>

 

 


@Component
@Aspect
public class MyAspect2 implements MethodInterceptor {
    //•环绕通知 org.aopalliance.intercept.MethodInterceptor
    //	•在目标方法执行前后实施增强

    //    声明一个公共切入点
    @Pointcut("execution(* com.gyf.service.*.*(..))")
    public void myPointcut() {

    }

    //    配置方法一:有点麻烦
    //    @Before("execution(* com.gyf.service.*.*(..))")
    //    解决办法:
    //    声明公共切入点;
    @Before("myPointcut()")
    public void myBefore(JoinPoint jp) {
        System.out.println("前置通知   " + jp.getSignature().getName());
    }


    @AfterReturning(pointcut = "myPointcut()", returning = "retValue")
    public void myAfter(JoinPoint jp, Object retValue) {
        System.out.println("后置通知   " + jp.getSignature().getName());
        System.out.println("返回值   " + retValue);
    }



    @Around("myPointcut()")
    public Object invoke(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        System.out.println("环绕通知——--开启事务"+proceedingJoinPoint.getSignature().getName());
        Object obj = proceedingJoinPoint.proceed();
        System.out.println("环绕通知——--关闭事务"+proceedingJoinPoint.getSignature().getName());
        return obj;
    }

    @AfterThrowing(value = "myPointcut()",throwing = "e")
    public void myAfterThrowing(JoinPoint jp,Throwable e){
        System.out.println("异常通知——--开启事务"+jp.getSignature().getName()+"-----"+e.getMessage());
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        System.out.println("前。");
        Object obj = methodInvocation.proceed();
        System.out.println("后。");
        return obj;
    }
}

 

   @Test
    public void Test(){

        ApplicationContext context = new ClassPathXmlApplicationContext("beans10.xml");
        IUserService userService = (IUserService)context.getBean("userService");
        userService.addUser();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值