Spring three slices

Source: Internet
Author: User
Tags aop

In addition to supporting schema-configurable AOP, Spring also supports annotations: Use @aspectj-style slice declarations. 1 Enabling support for @aspectj

Spring does not support @aspectj-style slice declarations by default, in order to support the need to use the following configuration:

Java code: <aop:aspectj-autoproxy/>

So spring can discover @aspectj-style slices and apply the slices to the target object. 2 statement slices

The @AspectJ-style declaration section is very simple and is declared using the @aspect annotation:

Java code: @Aspect () public class aspect{...}

When the slice is declared as a bean in the configuration file, Spring automatically recognizes and makes AOP configuration:

Java code: <bean id= "aspect" class= ... Aspect "/>

The slice is a pojo, where you can make a pointcut and a notification definition, and then look down. 3 statement pointcuts

@AspectJ-style named Pointcuts are implemented using the @pointcut+ method under the Org.aspectj.lang.annotation package (the method must be a return void type).

Java code: @Pointcut (value= "pointcut expression", Argnames = "List of parameter names") public void Pointcutname (...) {}

Value: Specifies a pointcut expression;

Argnames: Specifies named Pointcut method parameter list parameter names, which can be separated by multiple ",", which are passed to parameters with the same name as the notification method, while the pointcut expression "args (param)" Assigns the matching parameter type to the parameter type specified by the same parameter as the named Pointcut method.

Pointcutname: The name of the pointcut that you can use to refer to the pointcut expression.

Java code: View Copy to clipboard print @Pointcut (value= "execution) (* Cn.javass ... *.sayadvisorbefore (..)) && args (param) ", argnames =" param ") public void Beforepointcut (String param) {}

Defines a pointcut, named "Beforepointcut", that matches the first parameter type of the target method to the parameter type named "param" in the notification method implementation. 4 announcement notices

5 notification types are also supported for @AspectJ-style declaration notifications:

First, the front notice: Use the @before annotation statement under the Org.aspectj.lang.annotation package;

Java code: @Before (value = "Pointcut expression or named Pointcut", argnames = "argument list parameter name")

Value: Specifies a pointcut expression or named pointcut;

Argnames: Synonymous with schema-style configuration.

Let's take a look at the following example:

1, define the interface and implementation, here we will use the definition of schema style;

2, define the plane:

Java code: View Copy to clipboard print package CN.JAVASS.SPRING.CHAPTER6.AOP;   Import Org.aspectj.lang.annotation.Aspect; @Aspect public class HelloWorldAspect2 {}

3, define the entry point:

Java code: @Pointcut (value= "Execution" (* cn.javass). *.sayadvisorbefore (..)) && args (param) ", argnames =" param ") public void Beforepointcut (String param) {}

4, the definition of notice:

Java code:

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.