Triggering of Interceptor


AOP in JFinal is divided into control layer AOP and business layer AOP. Strictly speaking, business layer AOP is not limited to use in the business layer, because JFinal AOP can be applied anywhere else.

To control the triggering of the layer interceptor, just initiate an action request. To trigger the business layer interceptor, you need to first use the enhance method to enhance the target object, and then call the target method. The following is an example of business layer AOP usage:

// 定义需要使用AOP的业务层类
public class OrderService {
	// 配置事务拦截器
	@Before(Tx.class)
	public void payment(int orderId, int userId) {
	// service code here
	}
}
 
// 定义控制器,控制器提供了enhance系列方法可对目标进行AOP增强
public class OrderController extends Controller {
	public void payment() {
	// 使用 enhance方法对业务层进行增强,使其具有AOP能力 OrderService service = enhance(OrderService.class);
 
	// 调用payment方法时将会触发拦截器
	service.payment(getParaToInt("orderId"), getParaToInt("userId"));
	}
}


In the above code, OrderService is the business layer class, where The Tx transaction interceptor is configured on the payment method. OrderController is the controller, in which the enhance method is used to enhance the OrderSevice. Then its payment method can be called to trigger the Tx interceptor. In short, compared to the control layer, the triggering of business layer AOP only requires one more call to the enhance method, and the methods of using Interceptor, Before, and Clear are exactly the same.