Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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 Spring AOP AsjectJ注释:为多个方法定义切入点_Java_Spring_Aop_Aspectj_Spring Aop - Fatal编程技术网

Java Spring AOP AsjectJ注释:为多个方法定义切入点

Java Spring AOP AsjectJ注释:为多个方法定义切入点,java,spring,aop,aspectj,spring-aop,Java,Spring,Aop,Aspectj,Spring Aop,我已通过advise定义了两种方法的执行: SampleBusinessLogicImpl.UpdateSample示例和 SampleBusinessLogicImpl.createSampleSample示例 但是我的建议只针对第一个更新方法执行。 我做错了什么 @Aspect public class SampleDynamicValidationAspect { private static final Logger logger = LoggerFactory.getLogg

我已通过advise定义了两种方法的执行:

SampleBusinessLogicImpl.UpdateSample示例和 SampleBusinessLogicImpl.createSampleSample示例

但是我的建议只针对第一个更新方法执行。 我做错了什么

@Aspect
public class SampleDynamicValidationAspect {

    private static final Logger logger = LoggerFactory.getLogger(SampleDynamicValidationAspect.class); 

    /**
    private SampleTblDAO dao; //DAOs can be used inside dynamic validations
    **/


    @Before("execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.updateSample(com.rakuten.gep.sample.entity.common.Sample,..)) && args(sample,..) throws *Exception"
            +"|| execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(com.rakuten.gep.sample.entity.common.Sample,..)) && args(sample,..) throws *Exception")
    public void validate(Sample sample) throws SampleException {
        //Dynamic validation here.
        //If some validation is failed, wrapped the appropiate exception in SampleException
        System.out.println("Involking Dynamic Validator");
    }
}
我的SampleBusinessLogicImpl类如下所示:

@Service
public class SampleBusinessLogicImpl implements SampleBusinessLogic {

    @Autowired
    @Qualifier(value="proxySampleTblDao")
    private SampleTblDao sampleTblDao;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public Sample createSample(Sample sample) throws SampleException {
        //..
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public Sample updateSample(Sample sample) throws SampleException {
      //..

    }
}

当您在切入点中定义匹配的方法表达式时,它应该与您希望提供建议的实际方法相匹配

这就是切入点表达式应该是什么

@Before("execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.updateSample(com.rakuten.gep.sample.entity.common.Sample,..) throws *Exception) && args(sample,..)"
            +"|| execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(com.rakuten.gep.sample.entity.common.Sample,..) throws *Exception) && args(sample,..)")
原因:

在您的例子中,方法签名是

public Sample createSample(Sample sample) throws SampleException {
public Sample updateSample(Sample sample) throws SampleException {
但是,你的切入点是

@Before("execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.updateSample(com.rakuten.gep.sample.entity.common.Sample,..)) && args(sample,..) throws *Exception"
            +"|| execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(com.rakuten.gep.sample.entity.common.Sample,..)) && args(sample,..) throws *Exception")
请注意,throws*异常位于切入点表达式中的方法声明之外。这就是切入点表达式与方法声明不匹配的原因。 在执行中移动throws*异常,它就会工作。我刚试过

仅澄清一个方法的调用

是的,实际上。。您不需要指定throws*异常。仅使用该方法就足够了,因此,如果您完全删除它,它将在两种方法执行时都能很好地工作。现在,更新之所以有效是因为它首先在切入点表达式中声明。因此,切入点匹配updateSample的方法声明,然后遇到不合适的*异常,| |应用于*异常,而*异常实际上什么都不是

现在,如果您只是翻转切入点表达式的声明方式,则会出现错误。只有createSample可以工作,因为这是切入点表达式中唯一匹配的。 请记住,在执行表达式中也会关闭

另外,来自SpringAOP文档

executionmodifiers模式?ret类型模式声明类型模式?名称模式参数模式 投掷模式

除了上面代码段中返回的类型模式ret type模式以外的所有部分,名称模式, 和模式参数是可选的


希望,我能够帮助解决这个疑问

当您在切入点中定义匹配的方法表达式时,它应该与您希望提供建议的实际方法相匹配

这就是切入点表达式应该是什么

@Before("execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.updateSample(com.rakuten.gep.sample.entity.common.Sample,..) throws *Exception) && args(sample,..)"
            +"|| execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(com.rakuten.gep.sample.entity.common.Sample,..) throws *Exception) && args(sample,..)")
原因:

在您的例子中,方法签名是

public Sample createSample(Sample sample) throws SampleException {
public Sample updateSample(Sample sample) throws SampleException {
但是,你的切入点是

@Before("execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.updateSample(com.rakuten.gep.sample.entity.common.Sample,..)) && args(sample,..) throws *Exception"
            +"|| execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(com.rakuten.gep.sample.entity.common.Sample,..)) && args(sample,..) throws *Exception")
请注意,throws*异常位于切入点表达式中的方法声明之外。这就是切入点表达式与方法声明不匹配的原因。 在执行中移动throws*异常,它就会工作。我刚试过

仅澄清一个方法的调用

是的,实际上。。您不需要指定throws*异常。仅使用该方法就足够了,因此,如果您完全删除它,它将在两种方法执行时都能很好地工作。现在,更新之所以有效是因为它首先在切入点表达式中声明。因此,切入点匹配updateSample的方法声明,然后遇到不合适的*异常,| |应用于*异常,而*异常实际上什么都不是

现在,如果您只是翻转切入点表达式的声明方式,则会出现错误。只有createSample可以工作,因为这是切入点表达式中唯一匹配的。 请记住,在执行表达式中也会关闭

另外,来自SpringAOP文档

executionmodifiers模式?ret类型模式声明类型模式?名称模式参数模式 投掷模式

除了上面代码段中返回的类型模式ret type模式以外的所有部分,名称模式, 和模式参数是可选的


希望,我能够帮助解决这个疑问

你可以这样定义

@Before(value="( execution(* com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.updateSample(..))"
    + "|| execution(* com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(..)))"
    + " && args(sample)")

你可以这样定义

@Before(value="( execution(* com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.updateSample(..))"
    + "|| execution(* com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(..)))"
    + " && args(sample)")

我们可以看到你的SampleBusinessLogicImpl类吗?@SotiriosDelimanolis我发布了代码。我发现使用自定义注释标记我希望切入点应用的位置更容易。我们可以看到你的SampleBusinessLogicImpl类吗?@SotiriosDelimanolis我发布了代码。我发现使用自定义注释标记我希望切入点的位置更容易已应用。你能解释一下我的建议仅针对第一个更新方法执行吗?@SotiriosDelimanolis。无法在此处键入所有内容:因此,更新了答案,试图澄清疑问。毫无例外,它将起作用。但是,如果从通知中引发异常,它将被包装在未声明的HrowableException异常中。有了*Exception if,它将引发正确的SampleException。正确,这是您的要求,因此我没有建议删除*异常,而只是将其拉入执行表达式中。您能否解释我的建议仅针对第一个更新方法执行?@SotiriosDelimanolis。无法在此处全部键入:因此,更新了答案,试图澄清疑问。毫无例外,如果从通知中抛出异常,它将工作
它将被包装在未声明的HrowableException exception中。With*exception if将抛出正确的SampleException。正确,这是您的要求,因此我没有建议删除*exception,而只是将其拉入执行表达式中