我已经为这两种方法定义了要执行的通知:
( SampleBusinessLogicImpl.updateSample(Sample样本)和SampleBusinessLogicImpl.createSample(Sample样本()
但是,我的建议只对第一个update()方法执行。我在这里做错什么了?
@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 {
//..
}
}发布于 2014-02-03 07:41:03
当您在Pointcut中定义匹配的方法表达式时,它应该与您希望对其进行advice的实际方法相匹配。
这就是切入点表达式应该是什么。
@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 *Exception在pointcut表达式中的方法声明之外。这就是为什么切入点表达式与方法声明不匹配的原因。在throws *Exception中移动execution(,它就能工作了。我刚试过了。
只说明了一个方法的调用.
是的,实际上..。您不需要指定throws *Exception。只要使用该方法就足够了,因此,如果您完全删除它,它将非常好地工作,这两种方法都会执行。现在,更新工作的原因是,它首先在Pointcut表达式中声明。因此,切入点与updateSample()的方法声明相匹配,然后它遇到了不合适的*Exception,并将它应用于*Exception,这实际上是没有意义的。
现在,如果您只是按声明的方式翻转切入点表达式,错误的表达式就是。只有createSample才能工作,因为这是切入点表达式中唯一匹配的。记住,)也在execution expression中关闭。
@Before("execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.createSample(com.rakuten.gep.sample.entity.common.Sample,..) throws *Exception) && args(sample,..)"
+"|| execution(public * com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl.updateSample(com.rakuten.gep.sample.entity.common.Sample,..) throws *Exception) && args(sample,..)") 另外,来自Spring文档
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
除了返回类型模式(上面片段中的ret- type模式)、name模式和参数模式之外,其他所有部分都是可选的。
希望,我能帮你解决疑团。
https://stackoverflow.com/questions/21518697
复制相似问题