欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用前置通知进行访问控制:通过方法参数决定是否可以访问该方法,分别使用基于XML的声明式AspectJ和基于注解的声明式AspectJ来实现。

程序员文章站 2022-06-13 16:57:39
...

1,基于XML的声明式AspectJ来实现
TestDao.java:

package dynamic.jdk;public interface TestDao {
 public void save(String t);
}
TestDaoImpl.java:package dynamic.jdk;public class TestDaoImpl implements TestDao {
 @Override
 public void save(String t) {
  System.out.println("你好");
 }}

MyAspect.java:

package aspectj.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
 public void before(JoinPoint jp) {
  Object[] args=jp.getArgs();
  if(args[0]=="agree") {
   System.out.println("访问成功");
  }else {
   
   System.out.println("访问失败");
   System.exit(-1);
      
   }

  }}

applicationContext.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: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/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 定义目标对象 -->
    <bean id="testDao" class="dynamic.jdk.TestDaoImpl"/>
    <!-- 定义切面 -->
    <bean id="myAspect" class="aspectj.xml.MyAspect"/>
    <!-- AOP配置 -->
    <aop:config>
    <!-- 配置切面 -->
    <aop:aspect ref="myAspect">
    <!-- 配置切入点,通知增强哪些方法 -->
    <aop:pointcut expression="execution(* dynamic.jdk.*.*(..))" id="myPointCut"/>
    <!-- 将通知与切入点关联 -->
    <!-- 关联前置通知 -->
    <aop:before method="before" pointcut-ref="myPointCut"/>
    </aop:aspect>
    </aop:config>
    </beans> 

XMLAspectJTest:

package aspectj.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dynamic.jdk.TestDao;
public class XMLAspectJTest {
 public static void main(String[] args) {
  ApplicationContext appCon=new ClassPathXmlApplicationContext("/aspectj/xml/applicationContext.xml");
  //从容器中获取增强后的目标对象
  TestDao testDaoAdvice=(TestDao)appCon.getBean("testDao");
  //执行方法
  testDaoAdvice.save("agree");
  testDaoAdvice.save("disagree");
 }}

**2,基于注解声明式AspectJ来实现

AspectJ来实现MyAspect.java:

package aspectj.annotation;**
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
  @Pointcut("execution(* dao.*.*(..))")
     private void myPointCut() {
         
     }
  @Before("myPointCut()")
  public void before(JoinPoint jp) {
   Object[] args=jp.getArgs();
   if(args[0]=="agree") {
    System.out.println("访问成功");
   }else {    
    System.out.println("访问失败");
    System.exit(-1);
       
    }
  }
}

applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 指定要需要扫描的包,使注解生效 -->
    <context:component-scan base-package="aspectj.annotation"/>
    <context:component-scan base-package="dynamic.jdk"/>
    <!-- 启动基于注解的AspectJ支持 -->
    <aop:aspectj-autoproxy/>
    </beans>

Aspectjannotationtest.java:

package aspectj.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dynamic.jdk.TestDao;
public class Aspectjannotationtest {
 public static void main(String[] args) {
  ApplicationContext appCon=new ClassPathXmlApplicationContext("/aspectj/xml/applicationContext.xml");
  //从容器中获取增强后的目标对象
  TestDao testDaoAdvice=(TestDao)appCon.getBean("testDao");
  //执行方法
  testDaoAdvice.save("agree");
  testDaoAdvice.save("disagree");
 }
}
相关标签: web开发