Spring AOP throwsadvice

xiaoxiao2021-03-06  18

The current project has been contacted some of the Spring AOP, such as declarative transaction management. After understanding AOP's implementation of Spring, this new programming idea does find us to provide us with a new idea to solve problems. In order to record this learning process, the notes of learning are organized.

1. Several important concepts (see Spring online documentation)

· PointCut: a set of JointPoint. In Spring We can define those JointPoint to form a PointCut we need through some regular expressions, making our Advice can be compiled.

· Introduction: Introduction can increase the status and method in the existing classes without modifying this class, thereby increasing its status and action;

• Target: A class that meets the conditions defined by PointCut, we can use the Advice for this class. Most spring AOP is implemented by dynamic agents, this target is the object of the agent;

PROXY: In order to apply an Advice to another class, such as implementing Around Advice, is the other code before and after execution, then the actual implementation must first execute a paragraph of advice, then execute the method of Target After that, then the code is executed again, that is, when the client executes a class, actually executing is a proxy, passed by the agent to the Target again.

Weaving: With Target and Advice, what time is weaving these two modules together? The method you can selectively includes compilation (so we need a special compiler), when loading classes (so we need a special classloader) and running (AOP easy to create a proxy to call the call The agent is passed to the Target class).

2. throws advice

There is such a requirement in the current project. If some exceptions are thrown at some process, you need to record these exceptions, saved in the database or email the developer. We don't say this requirement with what conflicts with TDD, first look at how to implement it.

According to the above concept, we should pay attention to three concepts: Target, Advice and Proxy.

2.1 Target implementation

Target is the business process class above, and we can write code according to normal development, there is no special requirements. Such as:

Public interface ibizprocess

{

Void doonthing ();

Void DoanotherTherth ();

}

Public Class BizProcessImpl Implements IbizProcess

{

Public void doonthing ()

{

}

Public void doanotherthing ()

{

Throw new runtimeException ("boom");

}

}

2.2 Advice

In order to implement Advice when the business process throws an exception, we need to define an ADVICE class to implement the throwsAdvice interface. There is no way to define this interface, and we ask our classes to implement the method of AfTerthrows, as follows:

Public Void AfThrows ([Method Method,] [Object ARGS,] [Object Target,] throwable throwable; the front three parameters of this method are optional. We define multiple versions of this method in the same class, such as:

Public void atThrowing (MyException1 EX) {}

Public void atThrowing (MyException2 EX) {}

The specific method is called, determined by the specific Exception, which is easy to automatically identify by AOP.

2.3 Proxy (agent)

A simple implementation in Spring is with its org.springframework.aop.framework.proxyFactoryBean. This bean contains many properties, three of which we need to set: target, proxyinterfaces, and interceptornames, as follows:

com.company.biz.ibizprocessImpl

throwsadvice

By the above configuration, Spring woven Target and Advice together. It should be noted that proxyinterfaces and interceptorNames can be multiple, if you are multiple, you need to use list to define. The order of InterceptorNames determines the order of these Advice execution.

3. Simplify configuration

As can be seen from the above example, if there are multiple BizProcess objects require a proxy, we configure a proxy for each bean in the Spring configuration, then the maintenance of the configuration file will become troublesome. To this end, Spring provides a relatively convenient way to solve this problem, such as BeanNameAutoproxycreator, DefaultAdviceAutoproxycreator, and metadata autoproxying. We used BeanNameAutoproxycreator because he was more intuitive and simple.

The configuration is as follows:

转载请注明原文地址:https://www.9cbs.com/read-44730.html

New Post(0)