基于注解开发AspectJ

1.pom.xml,添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>edu.njxz.demo</groupId>
    <artifactId>springTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.8</version>
        </dependency>
    </dependencies>


</project>

2.创建目标类接口

public interface TestDao {
    void save();
    void delete();
}

3.实现接口类

@Repository("testDao")
public class TestDaoImpl  implements TestDao{
    public void save() {
        System.out.println("保存");
    }

    public void delete() {
        System.out.println("删除");
    }
}

4.创建切面类,并进行注解

@Aspect  //对应<aop:aspect ref="myAspect">
@Component  //对应<bean id="myAspect" class="">
public class MyAspect {

    /**
     * 定义切入点  @Pointcut 相当于  <aop:pointcut id="myPointCut" expression="execution(* edu.njxz.demo.dynamicProxy.*.*(..))"></aop:pointcut>
     */
    @Pointcut("execution(* edu.njxz.demo.dynamicProxy.*.*(..))")
    private void myPointCut(){

    }
    /**
     * 前置通知,使用JoinPoint接口作为参数获得目标对象信息
     * @param jp
     */
    @Before("myPointCut()")
    public void before(JoinPoint jp){
        System.out.print("前置通知:模拟权限控制");
        System.out.println(",目标对象:"+jp.getTarget()+" ,被增强处理的方法:"+jp.getSignature().getName());
    }

    /**
     * 后置返回通知
     * @param jp
     */
    @AfterReturning("myPointCut()")
    public void afterReturning(JoinPoint jp){
        System.out.print("后置返回通知:模拟删除临时文件");
        System.out.println(" ,被增强处理的方法:"+jp.getSignature().getName());
    }

    /**
     * 环绕通知
     * @param pjp
     * @return
     * @throws Throwable
     * 必须throws Throwable
     */
    @Around("myPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        //开始
        System.out.println("环绕开始:执行目标方法前,模拟开启事务");
        //执行当前目标方法
        Object obj=pjp.proceed();
        //结束
        System.out.println("环绕结束:执行目标方法后,模拟关闭事务");

        return obj;
    }

    /**
     * 异常通知
     *
     */

    @AfterThrowing(value = "myPointCut()",throwing = "e")
    public void except(Throwable e){
        System.out.println("异常通知:"+"程序执行异常"+e.getMessage());
    }

    /**
     * 后置通知,(最终通知)
     */
    @After("myPointCut()")
    public void after(){
        System.out.println("最终通知:模拟释放资源");
    }
}

5.创建配置文件

<?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="edu.njxz.demo.aspectAnnotation"></context:component-scan>
    <context:component-scan base-package="edu.njxz.demo.dynamicProxy"></context:component-scan>
    <!-- 启动基于注解的AspectJ支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

6.测试运行

public class Test {

    public static void main(String[] args) {
        //把beans.xml的类加载到容器
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("app2.xml");

        //从容器中获取增强后的目标对象
        TestDao testDaoAdvice =(TestDao) applicationContext.getBean("testDao");

        //执行方法
        testDaoAdvice.save();
    }

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AspectJ注解和事务注解是在Spring框架中用于声明式事务管理的两种方式。 AspectJ注解是一种通过注解方式来实现面向切面编程的技术。例如,@annotation(org.springframework.transaction.annotation.Transactional)可以用来标注在方法上,表示该方法使用了@Transactional注解来启用事务管理功能。 事务注解是一种用于定义事务相关信息的注解,可以用于指定事务的隔离级别、超时信息、传播行为、是否只读等。同时,事务注解还可以与事务状态(TransactionStatus)一起使用,用于控制事务的提交或回滚等操作。 在Spring框架中,可以通过配置文件或注解方式来实现声明式事务管理。对于AOP XML方式的声明式事务管理,需要配置事务管理器和增强功能,然后在业务层通过配置或注解来启用事务。而对于注解方式的声明式事务管理,只需要配置事务管理器,并在业务层添加相应的注解即可。 总结来说,AspectJ注解和事务注解都是在Spring框架中用于实现声明式事务管理的方式,前者通过注解方式实现面向切面编程,后者用于定义事务相关信息并与事务状态一起使用。可以根据具体需求选择适合的方式来管理事务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [spring切面的使用@AspectJ注解的3种配置](https://blog.csdn.net/u010010600/article/details/114116528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [AOP基于AspectJ注解开发和事务JDBC模板](https://blog.csdn.net/qq_42216575/article/details/100809416)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值