Spring AOP is configured in two ways

The first method: annotation configuration AOP annotation configuration AOP (implemented using AspectJ Class Library) is roughly divided into three steps: 1. Use the annotation @ aspect to define a facet, and define the pointcut (@ pointcut) and notification type (@ before, @ afterreturning, @ after, @ afterthrowing, @ around) in the facet 2. Develop classes that need to be intercepted. 3. Configure the aspect into XML. Of course, we can also use the method of automatically scanning beans. In this case, it will be managed by the spring AOP container.

In addition, you need to reference the jar package of AspectJ: aspectjweaver jar aspectjrt. jar

example:

Implementation interface:

Operation class:

Join AOP

configuration file

Test class:

result:

be careful:

@Aspect: it means that this class is an aspect class @ componet: because it needs to be managed by spring as an aspect class, it needs to be initialized into spring management during initialization@ Befoe: advice execution of pointcuts...: pointcut syntax

The second is to configure AOP with XML

The example is the same as above: only the configuration files are different

The following < beans > is the spring configuration tag. There are several important attributes in beans:

xmlns:

Is the default XML document parsing format, that is, spring beans. the address is http://www.springframework.org/schema/beans 。

By setting this attribute, all attributes declared in beans can be used directly through < >, such as < bean >, etc.

xmlns:xsi:

It is the specification that XML needs to comply with. It can be seen from the URL that it is the unified specification of W3. Later, all parsing files are located through XSI: schemalocation.

xmlns:aop:

This is the key point. It is some semantic specifications we need to use here, which is related to aspect oriented AOP.

xmlns:tx:

Transaction related configuration content in spring.

An XML file can only declare one default semantic parsing specification.

For example, only beans in the above XML is the default, and others need to be used through specific tags. For example, AOP has many attributes. If you want to use it, you must add AOP: XXX in front. For example, AOP: config above.

Similarly, if the default xmlns is configured with the semantic parsing specification related to AOP, the config tag can be written directly in XML.

The above are the two spring AOP configuration methods. Do you understand? After that, there are more articles about spring AOP configuration methods to share with you. You must continue to pay attention

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>