Sal*_*zmi 6 java spring spring-aop
我创建了一个名为BaseCron的bean,它有一个executeBefore()在spring的下面配置中配置的方法,用于拦截Crons类的所有方法调用并在它们之前执行.
该executeBefore()方法有一些验证.我之前验证了某些条件,如果它们是假的,我就是抛出异常.抛出异常会导致方法失败,因此Crons类中的方法不会执行.
它工作正常.
你可以建议一些其他的方法,我可以停止执行Crons类而不抛出异常.我试过回来但是没用.
<bean id="baseCronBean" class="com.myapp.cron.Jkl">
</bean>
<aop:config>
<aop:aspect id="cron" ref="baseCronBean">
<aop:pointcut id="runBefore" expression="execution(* com.myapp.cron.Abc.*.*(..)) " />
<aop:before pointcut-ref="runBefore" method="executeBefore" />
</aop:aspect>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
Abc课程:
public class Abc {
public void checkCronExecution() {
log.info("Test Executed");
log.info("Test Executed");
}
}
Run Code Online (Sandbox Code Playgroud)
Jkl课程:
public class Jkl {
public void executeBefore() {
//certain validations
}
}
Run Code Online (Sandbox Code Playgroud)
干净的方法是使用Around建议而不是Before.
将方面(和相关配置)更新为如下所示
public class Jkl{
public void executeAround(ProceedingJoinPoint pjp) {
//certain validations
if(isValid){
// optionally enclose within try .. catch
pjp.proceed(); // this line will invoke your advised method
} else {
// log something or do nothing
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4875 次 |
| 最近记录: |