spring框架中基于XML和注解的声明式Aspectj的常规操

2019-09-29  本文已影响0人  梦捷者

一、基于XML的声明式AspectJ的操作

1、导入相关的jar包

2、在com.itheima.aspectj.xml包下创建MyAspect.java切面类

package com.itheima.aspectj.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
    //前置通知֪
    public void mybefore(JoinPoint joinpoint){
        System.out.print("前置通知:模拟执行权限检查...");
        System.out.print("\n");
        System.out.print("ִ目标类是:"+joinpoint.getTarget());
        System.out.print("\n");
        System.out.print("执行的相应的切入点是(即执行的方法):"+ joinpoint.getSignature().getName());
        System.out.print("\n");
        
    }
    
    //后置通知֪
    public void myafterreturning(JoinPoint joinpoint){
        System.out.print("后置通知:模拟记录日志");
        System.out.print("\n");
        System.out.print("执行相应的目标方法是(即切入点):"+ joinpoint.getSignature().getName());
        System.out.print("\n");
        
    }
    
    //环绕通知֪
    public Object myaround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.print("环绕开始:执行方法之前,模拟开启事务....");
        System.out.print("\n");
        //ִ执行当前的目标方法
         Object obj=proceedingJoinPoint.proceed();
         System.out.print("环绕结束:执行目标方法之后,模拟关闭事务....");
         System.out.print("\n");
        return obj;
        
    }
}

3、在com.demo包下编写业务逻辑层的Dao层,即UserDao.java接口类和相关的UserDaoImpl.java实现类

package com.demo;

public interface UserDao {
    public void adduser();
    public void deleteuser();
}

package com.demo;

import org.springframework.stereotype.Repository;


public class UserDaoImpl implements UserDao {

    public void adduser() {
        // TODO Auto-generated method stub
        System.out.print("增加用户");
        System.out.print("\n");
    }

    public void deleteuser() {
        // TODO Auto-generated method stub
        System.out.print("删除用户");
        System.out.print("\n");
    }

}

4、在src目录下创建ApplicationContext.xml文件,编写相关的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">
    <bean id="userdao" class="com.demo.UserDaoImpl"></bean>
    <bean id="myaspect" class="com.itheima.aspectj.xml.MyAspect"></bean>
    
    <aop:config>
    
    <aop:aspect ref="myaspect">
    <aop:pointcut expression="execution(* com.demo.*.*(..))" id="mypoint"/>
    <aop:before method="mybefore" pointcut-ref="mypoint"/>
    <aop:after-returning method="myafterreturning" pointcut-ref="mypoint" returning="returnval"/>
    <aop:around method="myaround" pointcut-ref="mypoint"/>    
    </aop:aspect>
    </aop:config>

</beans>

5、在com.test中编写test1.java,进行测试相关的面向接口编程(运行结果成功,代表基于XML的声明式AspectJ已经实现了AOP开发)

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.demo.UserDao;

public class test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ApplicationContext app2=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserDao userdao=(UserDao)app2.getBean("userdao");
        userdao.adduser();
        userdao.deleteuser();


        
        
    }

}

二、基于annotation(注解)的声明式AspectJ的操作

1、导入相关的jar包(同上)
2、在src.com.itheima.aspectj.xml包下创建MyAspect.java切面类

package src.com.itheima.aspectj.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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;

@Aspect
@Component
public class MyAspect {
    
    
    @Pointcut("execution(* src.com.demo.*.*(..))")
    private void mypoint(){}
    
    //前置通知֪
    @Before("mypoint()")
    public void mybefore(JoinPoint joinpoint){
        System.out.print("前置通知:模拟执行权限检查...");
        System.out.print("\n");
        System.out.print("目标类是:"+joinpoint.getTarget());
        System.out.print("\n");
        System.out.print("执行的相应的切入点是(即执行的方法):"+ joinpoint.getSignature().getName());
        System.out.print("\n");
        
    }
    
    //后置通知֪
    @AfterReturning("mypoint()")
    public void myafterreturning(JoinPoint joinpoint){
        System.out.print("后置通知:模拟记录日志");
        System.out.print("\n");
        System.out.print("执行相应的目标方法是(即切入点):"+ joinpoint.getSignature().getName());
        System.out.print("\n");
        
    }
    
    //环绕通知֪
    @Around("mypoint()")
    public Object myaround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.print("环绕开始:执行方法之前,模拟开启事务....");
        System.out.print("\n");
        //ִ执行相关联的方法
         Object obj=proceedingJoinPoint.proceed();
         System.out.print("\n");
         System.out.print("环绕结束:执行目标方法之后,模拟关闭事务....");
         System.out.print("\n");
        return obj;
        
    }
    

}

3、在src.com.demo包下创建UserDao.java的接口类和UserDaoImpl.java的实现类

package src.com.demo;

public interface UserDao {
    public void adduser();
    public void deleteuser();
}

package src.com.demo;

import org.springframework.stereotype.Repository;

@Repository("userdao")
public class UserDaoImpl implements UserDao {

    public void adduser() {
        // TODO Auto-generated method stub
        System.out.print("增加用户");
        System.out.print("\n");
    }

    public void deleteuser() {
        // TODO Auto-generated method stub
        System.out.print("删除用户");
        System.out.print("\n");
    }

}

4、在src目录下创建ApplicationContext.xml文件,编写相关的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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="src.com"></context:component-scan>
    
  <aop:aspectj-autoproxy/><!--启动基于注解的声明式AspectJ的支持 -->
  
</beans>

5、在com.test中编写test1.java,进行测试相关的面向接口编程(运行结果成功,代表基于注解的声明式AspectJ已经实现了AOP开发)

package src.com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import src.com.demo.UserDaoImpl;

import src.com.demo.UserDao;

public class test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ApplicationContext app2=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserDao userdao=(UserDao)app2.getBean("userdao");
        userdao.adduser();
        
    }

}

上一篇下一篇

猜你喜欢

热点阅读