亲宝软件园·资讯

展开

Java SpringAOP技术之注解方式

C'z x 人气:0

1.配置xml扫描注解 

<?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/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.qcby"></context:component-scan>
</beans>

2.配置注解

package com.qcby;
import org.springframework.stereotype.Component;
@Component(value = "user")
public class User {
    //连接点/切入点
    public void add(){
        System.out.println("add......");
    }
}

给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明

@Component
@Aspect  //生成代理对象
public class UserProxy {
}

3.配置文件中开启自动代理

<?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/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.qcby"></context:component-scan>
    <!--开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

4.通知类型注解

@Before -- 前置通知

@AfterReturing -- 后置通知

@Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行)

@After -- 最终通知

@AfterThrowing -- 异常抛出通知

package com.qcby;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect  //生成代理对象
public class UserProxy {
    //增强/通知  ---》前置通知
    @Before(value = "execution(public void com.qcby.User.add())")
    public void before(){
        System.out.println("前置通知.............");
    }
    // 环绕通知
    @Around(value = "execution(public void com.qcby.User.add())")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前置.............");
        //  执行被增强的方法
        proceedingJoinPoint.proceed();
        System.out.println("环绕后置.............");
    }
    // 最终通知
    @After(value = "execution(public void com.qcby.User.add())")
    public void after() {
        System.out.println("最终通知.............");
    }
    //后置通知
    @AfterReturning(value = "execution(public void com.qcby.User.add())")
    public void afterReturning() {
        System.out.println("后置通知.............");
    }
    //异常通知
    @AfterThrowing(value = "execution(public void com.qcby.User.add())")
    public void afterThrowing() {
        System.out.println("出错了.............");
    }
}

5.测试类

package com.qcby.test;
import com.qcby.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
    @Test
    public void aopTest1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringConfig.xml");
        User user = (User) applicationContext.getBean("user");
        user.add();
    }
}

6.结果 

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!   

加载全部内容

相关教程
猜你喜欢
用户评论