AOP in Spring

Source: Internet
Author: User

AOP in Spring

AOP

In software development, the function of Multi-processing in an application is called a cross-cutting concern. Generally, the cross-cutting concern is conceptually separated from the application logic, separating the cross-cutting concerns from the business logic is a problem to be solved by Aspect-oriented programming (AOP.

Dependency injection facilitates decoupling between objects, while AOP can decouple the cross-cutting concerns and the objects they affect. Cross-cutting concerns are often applied to logs, transaction management, and cache.

In Aspect-oriented programming, a cross-cutting point is a common function, which can also be called a plane. This function can be used anywhere without modifying the affected class. Benefits of doing so: 1. Focus on one place, rather than decentralization2. Simplified service modules

Terminology in AOP

Aspect: a common feature in applications

Notification (advice): it can be understood as a specific implementation of the aspect. It is usually written in Java. Common notifications include before and after), surround notification, after-returning, and after-throwing)

Pointcut: the position where the cut surface or notification exists. This position is the cut point.

Joinpoint: place a notification at the point of cut, for example, before or after a method.

Weaving: applies a cut surface to the process of creating a new proxy object for the target object.

Introduction: allows us to add new attributes or methods to existing classes. It is mainly implemented by creating notifications.

In Spring, you need to use AspectJ's cut point expression to write cut points.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + xvejrNTatMu7 + bShyc/BqrrPxuTL/LXE1rjKvsb3wLTP3raox9C146GjPC9wPgo8cD7A/Signature + signature + Signature AXRoaW4oYW9wMS4qKTwvcD4KPHA + release/M/release + export/g06a1xM2o1qqhozwvcD4KPHA + woamiziwmj1_o7xeu7nt0kgx "| ","!" Symbol.

There is also a bean indicator in Spring. bean contains an ID to limit the bean. Example: execution (* aop. People. getName (..))AndBean (p)

AOP declaration in XML

Instance:

PackageAop;

PublicclassAdvice {

PublicvoidDoSomething ()

{

System.Out. Println ("hello .......");

}

}

PackageAop;

PublicclassPeople {

PrivateStringname;

PublicString getName (){

ReturnName;

}

PublicvoidSetName (String name ){

This. Name = name;

}

PublicString greet ()

{

System.Out. Println (name + ".............");

ReturnName;

}

}

"P"Class = "Aop. People">

"Name"Value = "Zhangsan">

"Method"Class = "Aop. Advice"/>

"Method">

"Execution (* aop. People. greet () and bean (p )"Id ="Greet"/>

"DoSomething"Pointcut-ref ="Greet"/>

ApplicationContextac =NewClassPathXmlApplicationContext ("aop/bean. xml ");

People p = ac. getBean ("p", People.Class);

P. greet ();

Result: hello .......

Zhangsan .............

Send parameters to notifications

Instance:

PublicclassAdvice {

Privateint Id;

PublicvoidDoSomething (IntId)

{

System.Out. Println ("hello..." + id );

}

}

PublicclassPeople {

PrivateStringname;

PublicString getName (){

ReturnName;

}

PublicvoidSetName (String name ){

This. Name = name;

}

PublicvoidGreet (IntId)

{

System.Out. Println (name + ".............");

}

}

"P"Class = "Aop. People">

"Name"Value = "Zhangsan">

"Method"Class = "Aop. Advice"/>

"Method">

"Execution (* aop. People. greet (Int) AndArgs (id)"Id ="Greet"/>

"DoSomething"Pointcut-ref ="Greet"Arg-names ="Id"/>

ApplicationContextac =NewClassPathXmlApplicationContext ("aop/bean. xml ");

People p = ac. getBean ("p", People.Class);

P. greet (1 );

Result: hello... 1

Zhangsan .............

The result shows that the parameter id in the target method is sent to the notification.

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.