AOP two in spring based on AOP namespaces (declaring a slice, pointcut, and notification)

Source: Internet
Author: User
Tags aop logical operators

2. Declare a slice

In spring AOP, which is based on an AOP namespace, to declare a slice, you need to use the child label <aop:aspect> of <aop:config/>. The <aop:aspect> label has a ref attribute that must be assigned to specify the managed bean (backing bean) associated with that section, and we will use the backing bean to address the bean later. As the following example shows, the Java class for the Bean is a normal Java class in which the notification method for the slice is defined. Additionally, the,<aop:aspect> label also has two optional order and ID attributes, which are used to specify the load sequence of the section, and the id attribute is used to identify the slice. Examples are as follows:

Code

<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
<bean id="MyAspect" class="aop.test.MyAspect" />
<aop:config proxy-target-class="true">
<aop:aspect ref="MyAspect" order="1" id="TestAspectName">
……切面其他配置
</aop:aspect>
</aop:config>
……其他配置
</beans>

3. Declare an entry point

To declare a pointcut, you can use the <aop:pointcut> of <aop:aspect>, and in Spring2.5 it has two property IDs and expression, which are used to mark the Pointcut and set the pointcut expression. For example:

Code

<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
<bean id="MyAspect" class="aop.test.MyAspect"/>
<aop:config proxy-target-class="true">
<aop:aspect ref="MyAspect" order="1" id=”TestAspectName”>
<aop:pointcut id="test"
expression="execution(* aop.test.TestBean.*(..))"/>
<aop:before pointcut="aop.test.MyAspect.Pointcut1()"
method="beforeAdvice" />
</aop:aspect>
</aop:config>
……其他配置
</beans>

The expression property of the Aop:pointcut> label uses the pointcut expression language described earlier, which means that the ASPECTJ pointcut expression is supported. But because the XML to "&&", "| |", "!" Such logical operators are unfriendly, and the logical operators that are used in @AspectJ pointcut expression languages need to be replaced with "and", "or" and "not" in the XML configuration, respectively.

Sometimes we also need to use the pointcut of the @pointcut annotation declaration in XML, so what? As you may recall, we can refer to another pointcut in the pointcut expression. Right, right here, we use this feature to accomplish this task as follows:

Code

<aop:pointcut id= "test" expression= "aop.test.MyAspect.Pointcut1 ()"/>

Note: Here we must use the full path to mark the referenced pointcut.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.