Aspect-oriented programming, Spring AOP

Aspect-oriented programming (AOP) is a programming paradigm which is a further development of procedural and object-oriented programming (OOP). The idea of ​​AOP is to highlight the so-called cross-cutting functionality. And so everything is in order, here I will show how to do it in Java - Spring @ AspectJ annotation style (there is also a schema-based xml style, the functionality is similar). 3r33595.  3r3604.

Allocating end-to-end functionality

3r33595.  3r3604. Up to 3r?595.  3r3604. Aspect-oriented programming, Spring AOP 3r33595.  3r3604. and after 3r?595.  3r3604. 3r33595.  3r3604. Those. There is a functionality that affects several modules, but it does not have a direct relationship to the business code, and it would be good to bring it to a separate place, this is shown in the figure above. 3r33595.  3r3604.

Join point

3r33595.  3r3604. Around 3r39898. 3r3409. 3r33595.  3r3604.
@Aspect
@Component
public class MyAspect {
3r3604. @Pointcut ("execution (public * com.example.demoAspects.MyService. * ())")
public void callAtMyServicePublic () {
}
3r3604. @Around ("callAtMyServicePublic ()")
public Object aroundCallAt (Proceeding JoinPoint call) throws Throwable {
StopWatch clock = new StopWatch (call.toString ()); 3r3604. try {3r3604. clock.start (call.toShortString ()); 3r3604. return call.proceed (); 3r3604.} finally {3r3604. clock.stop (); 3r3604. System.out.println (clock.prettyPrint ()); 3r3604.}
}
}
3r33585. 3r33586. 3r33595.  3r3604. If we run the calling code with calls to the MyService methods, then we get the time to call each method. Thus, without changing the calling code and the target, I added new functionalities: logging, profiler, and security. 3r33595.  3r3604. 3r33595.  3r3604. 3r3408. An example of using in UI forms
3r33595.  3r3604. There is a code that hides /shows fields on the form: 3r39595.  3r3604. 3r33595.  3r3604.
public class EditForm extends Form {
3r3604. @Override
public void init (Form form) {
formHelper.updateVisibility (form, settingsService.isVisible (COMP_NAME)); 3r3604. formHelper.updateVisibility (form, settingsService.isVisible (COMP_LAST_NAME)); 3r3604. formHelper.updateVisibility (form, settingsService.isVisible (COMP_BIRTH_DATE)); 3r3604. //3r3604.}
3r33585. 3r33586. 3r33595.  3r3604. You can also updateVisibility removed in the Advice type
Around 3r39898. 3r33595.  3r3604.
3r3604. @Aspect
public class MyAspect {
3r3604. @Pointcut ("execution (* com.example.demoAspects.EditForm.init () && args (form, ))") 3r3604. public void callAtInit (Form form) {}
3r3604. //3r3604. @Around ("callAtInit (form)")
public Object aroundCallAt (Proceeding JoinPoint pjp, Form form) {
formHelper.updateVisibility (form, settingsService.isVisible (COMP_NAME)); 3r3604. formHelper.updateVisibility (form, settingsService.isVisible (COMP_LAST_NAME)); 3r3604. formHelper.updateVisibility (form, settingsService.isVisible (COMP_BIRTH_DATE)); 3r3604. Object retVal = pjp.proceed (); 3r3604. return retVal; 3r3604.}
3r33585. 3r33586. 3r33595.  3r3604. 3r33595.  3r3604. etc. 3r33595.  3r3604. The structure of the project is 3r359595.  3r3604. 3r33434. 3r33595.  3r3604. 3r33595.  3r3604. 3r33434. 3r33469. pom file [/b] 3r33471.
3r33473. 3r37474. 3r3604. 3r33476. xsi: schemaLocation = "http://maven.apache.org/POM/???
http://maven.apache.org/xsd/maven-???.xsd">
3r33478. ??? 3r33434. 3r3604. 3r3604. 3r3-3568. com.example
3r3604. 3r33571. demoAspects
3r3604. 3r33510. ???-SNAPSHOT
3r3604.
jar
3r3604. 3r3604.
demoAspects
3r3604.
Demo project for Spring Boot Aspects
3r3604. 3r3604. 3r3502. 3r3604. 3r3-3568. org.springframework.boot
3r3604. 3r33571. spring-boot-starter-parent
3r3604. 3r33510. ???.RELEASE
3r3604. 3r? 3513. 3r???. 3r3604. 3r? 3516. 3r3604. 3r3604. 3r? 3519. 3r3604. 3r33521. UTF-8 3r? 3522. 3r3604. 3r33524. UTF-8 3r33535. 3r3604. 3r33535. ???r33528. 3r3604. 3r? 3530. 3r3604. 3r3604. 3r33333. 3r3604. 3r33546. 3r3604. 3r3-3568. org.springframework.boot
3r3604. 3r33571. spring-boot-starter-aop
3r3604. 3r33557. 3r3604. 3r3604. 3r33546. 3r3604. 3r3-3568. org.springframework.boot
3r3604. 3r33571. spring-boot-starter-test
3r3604. 3r33554. test
3r3604. 3r33557. 3r3604. 3r3-3559. 3r3604. 3r3604. 3r? 3562. 3r3604. 3r33535. 3r3604. 3r3566. 3r3604. 3r3-3568. org.springframework.boot
3r3604. 3r33571. spring-boot-maven-plugin
3r3604. 3r? 3574. 3r3604. 3r33576. 3r3604. 3r33578. 3r3604. 3r3604. 3r3604. 3r33582. 3r3604. 3r3604. 3r33585. 3r33586. 3r33595.  3r3604.
3r33595.  3r3604. 3r33595.  3r3604. Materials 3r33595.  3r3604. 3r? 3597. Aspect Oriented Programming with Spring
3r3604. 3r3604. 3r3602. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () ();
3r3604.
+ 0 -

Add comment