Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 包中所有方法的AOP_Java_Spring_Aop - Fatal编程技术网

Java 包中所有方法的AOP

Java 包中所有方法的AOP,java,spring,aop,Java,Spring,Aop,我第一次使用AOP。 我已经编写了下面的AOP代码,当我使用它截取一个特定的方法时,它可以很好地工作 有人能指导我吗?我如何设置它来拦截某个包(com.test.model)中的所有方法 基本上是如何设置appcontext.xml 另外,在调用每个方法之前,是否需要执行如下操作来调用 AopClass aoptest = (AopClass) _applicationContext.getBean("AopClass"); aoptest.addCustomerAround("dummy");

我第一次使用AOP。 我已经编写了下面的AOP代码,当我使用它截取一个特定的方法时,它可以很好地工作

有人能指导我吗?我如何设置它来拦截某个包(com.test.model)中的所有方法

基本上是如何设置appcontext.xml

另外,在调用每个方法之前,是否需要执行如下操作来调用

AopClass aoptest = (AopClass) _applicationContext.getBean("AopClass");
aoptest.addCustomerAround("dummy");
有人能帮忙吗

如果需要更多的解释,请告诉我

下面是我的代码:

界面:

package com.test.model;

import org.springframework.beans.factory.annotation.Autowired;

public interface AopInterface {


    @Autowired
    void addCustomerAround(String name);
}
package com.test.model;

import com.test.model.AopInterface;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class AopClass implements AopInterface {

    public void addCustomerAround(String name){
        System.out.println("addCustomerAround() is running, args : " + name);
    }
}
package com.test.model;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;



@Aspect
public class TestAdvice{


     @Around("execution(* com.test.model.AopInterface.addCustomerAround(..))")
       public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("testAdvice() is running!");


       }
}
<!-- Aspect -->
    <aop:aspectj-autoproxy />
    <bean id="AopClass" class="com.test.model.AopClass" />
    <bean id="TestAdvice" class="com.test.model.TestAdvice" />
类别:

package com.test.model;

import org.springframework.beans.factory.annotation.Autowired;

public interface AopInterface {


    @Autowired
    void addCustomerAround(String name);
}
package com.test.model;

import com.test.model.AopInterface;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class AopClass implements AopInterface {

    public void addCustomerAround(String name){
        System.out.println("addCustomerAround() is running, args : " + name);
    }
}
package com.test.model;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;



@Aspect
public class TestAdvice{


     @Around("execution(* com.test.model.AopInterface.addCustomerAround(..))")
       public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("testAdvice() is running!");


       }
}
<!-- Aspect -->
    <aop:aspectj-autoproxy />
    <bean id="AopClass" class="com.test.model.AopClass" />
    <bean id="TestAdvice" class="com.test.model.TestAdvice" />
AOP:

package com.test.model;

import org.springframework.beans.factory.annotation.Autowired;

public interface AopInterface {


    @Autowired
    void addCustomerAround(String name);
}
package com.test.model;

import com.test.model.AopInterface;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class AopClass implements AopInterface {

    public void addCustomerAround(String name){
        System.out.println("addCustomerAround() is running, args : " + name);
    }
}
package com.test.model;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;



@Aspect
public class TestAdvice{


     @Around("execution(* com.test.model.AopInterface.addCustomerAround(..))")
       public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("testAdvice() is running!");


       }
}
<!-- Aspect -->
    <aop:aspectj-autoproxy />
    <bean id="AopClass" class="com.test.model.AopClass" />
    <bean id="TestAdvice" class="com.test.model.TestAdvice" />
appcontext:

package com.test.model;

import org.springframework.beans.factory.annotation.Autowired;

public interface AopInterface {


    @Autowired
    void addCustomerAround(String name);
}
package com.test.model;

import com.test.model.AopInterface;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class AopClass implements AopInterface {

    public void addCustomerAround(String name){
        System.out.println("addCustomerAround() is running, args : " + name);
    }
}
package com.test.model;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;



@Aspect
public class TestAdvice{


     @Around("execution(* com.test.model.AopInterface.addCustomerAround(..))")
       public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("testAdvice() is running!");


       }
}
<!-- Aspect -->
    <aop:aspectj-autoproxy />
    <bean id="AopClass" class="com.test.model.AopClass" />
    <bean id="TestAdvice" class="com.test.model.TestAdvice" />

只要把:

@Around("execution(* com.test.model..*.*(..))")
执行表达式的格式为:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
其中只需要
ret-type模式
name模式
param模式
,因此至少我们需要一个表达式,如:

execution(ret-type-pattern name-pattern(param-pattern))
  • ret-type模式
    与返回类型匹配,
    *
    用于任何
  • 名称模式
    与方法名称匹配,您可以使用
    *
    作为通配符,并使用
    指示子包
  • 参数模式
    匹配方法参数,
    (..)
    匹配任意数量的参数
您可以在这里找到更多信息:,这里有一些有用的信息