Register   Login   About   Study   Enterprise   Share
Internet / AI Technology University (ITU/AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 1. Java Introduction >> 1.3. Critical Thinking and Software Development Process >> 1.3.2. SDLC-SOA-AOP >> 1.3.2.3. OOP versus Aspect-Oriented and Functional Programming
Current Topic: 1.3.2.3.1. AOP Samples
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Aspect Oriented Programming is another way of working on program structure.

It does not focus on class like OOP but it focuses on aspects. It allows modular programming where code is divided into separate sub programs to avoid scattering of codes. AOP is not replacing OOP; it is just another great way of having organizing your programs and to increase productivity.

If there is a code that is asking users to enter a number and determine if it is prime, for a number that is high it would take a longer time to get the result. Using the concept of AOP would help to rectify the problem of taking a longer time to process the code.

AOP can accelerate a method execution.

AOP can be used to support auditing, exception handling, and simplify application in a process of programming.

The key unit of modularity in OOP is the class but in AOP it is the aspect.

The 7 terms in AOP are:
Aspect, Join Point, Advice, Pointcut, Introduction, Target Object and Weaving.



// @Aspect and @Around are AOP annotations in the Code Sample below

@Aspect
public class EmployeeAroundAspect {

@Around("execution(* com.journaldev.spring.model.Employee.getName())")
public Object employeeAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
System.out.println("Before invoking getName() method");
Object value = null;
try {
value = proceedingJoinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
}
}


using AspectJ (See full code in MethodLogger.java: https://github.com/jcabi/jcabi-aspects/blob/jcabi-0.15.2/src/main/java/com/jcabi/aspects/aj/MethodLogger.java )

@Aspect
public class MethodLogger {
@Around("execution(* *(..)) && @annotation(Loggable)")
public Object around(ProceedingJoinPoint point) {
long start = System.currentTimeMillis();
Object result = point.proceed();
Logger.info(
"#%s(%s): %s in %[msec]s",
MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
point.getArgs(),
result,
System.currentTimeMillis() - start
);
return result;
}
}
Was it clear so far?


This is an aspect with a single around() advice inside. The aspect is annotated with @Aspect and advice is annotated with @Around. These annotations are markers in .class files. They provide meta-information for runtime.

Annotation @Around has one parameter, which?in this case?says that the advice should be applied to a method if:

its visibility modifier is * (public, protected or private);

its name is name * (any name);

its arguments are .. (any arguments); and

it is annotated with @Loggable

When a call to an annotated method is to be intercepted, method around() executes before executing the actual method. When a call to method power() is to be intercepted, method around() receives an instance of class UsingLoggable and must return an object, which will be used as a result of method power().

In order to call the original method, power(), the advice has to call proceed() of the join point object.

Here is the sample of using the @Loggable annotation.


public class UsingLoggable {
@Loggable
public int mathSample(int x, int y) {
return Math.pow(x, y);
}
}

These samples allows to minimize the repeated code through a process known as weaving.

Assignments:
1. Provide one or more complete samples of AOP code with good comments describing the purpose of the code.

2. Create a QnA related to the material and email to dean@ituniversity.us

More recommended reading:
http://www.javaworld.com/article/2073918/core-java/i-want-my-aop---part-1.html?page=2
http://javabeat.net/introduction-to-springs-aspect-oriented-programmingaop/
http://www.informit.com/articles/article.aspx?p=174533&seqNum=2
http://www.christianschenk.org/blog/aop-with-aspectj/
https://docs.jboss.org/aop/1.0/aspect-framework/userguide/en/html/what.html
https://www.pluralsight.com/courses/aspect-oriented-programming-spring-aspectj
https://www.tutorialspoint.com/spring/aop_with_spring.htm
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>// @Aspect and @Around are AOP annotations in the Code Sample below
<br/>
<br/>@Aspect
<br/>public class EmployeeAroundAspect {
<br/>
<br/>	@Around("execution(* com.journaldev.spring.model.Employee.getName())")
<br/>	public Object employeeAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
<br/>		System.out.println("Before invoking getName() method");
<br/>		Object value = null;
<br/>		try {
<br/>			value = proceedingJoinPoint.proceed();
<br/>		} catch (Throwable e) {
<br/>			e.printStackTrace();
<br/>                }
<br/>         }
<br/>}
<br/>


using AspectJ (See full code in MethodLogger.java: https://github.com/jcabi/jcabi-aspects/blob/jcabi-0.15.2/src/main/java/com/jcabi/aspects/aj/MethodLogger.java )
<br/>@Aspect
<br/>public class MethodLogger {
<br/>  @Around("execution(* *(..)) && @annotation(Loggable)")
<br/>  public Object around(ProceedingJoinPoint point) {
<br/>    long start = System.currentTimeMillis();
<br/>    Object result = point.proceed();
<br/>    Logger.info(
<br/>      "#%s(%s): %s in %[msec]s",
<br/>      MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
<br/>      point.getArgs(),
<br/>      result,
<br/>      System.currentTimeMillis() - start
<br/>    );
<br/>    return result;
<br/>  }
<br/>}
<br/>






Was it clear so far?



This is an aspect with a single around() advice inside. The aspect is annotated with @Aspect and advice is annotated with @Around. These annotations are markers in .class files. They provide meta-information for runtime.

Annotation @Around has one parameter, which?in this case?says that the advice should be applied to a method if:

its visibility modifier is * (public, protected or private);

its name is name * (any name);

its arguments are .. (any arguments); and

it is annotated with @Loggable

When a call to an annotated method is to be intercepted, method around() executes before executing the actual method. When a call to method power() is to be intercepted, method around() receives an instance of class UsingLoggable and must return an object, which will be used as a result of method power().

In order to call the original method, power(), the advice has to call proceed() of the join point object.

Here is the sample of using the @Loggable annotation.

<br/>public class UsingLoggable {
<br/>  @Loggable
<br/>  public int mathSample(int x, int y) {
<br/>    return Math.pow(x, y);
<br/>  }
<br/>}
<br/>

These samples allows to minimize the repeated code through a process known as weaving.

Assignments:
1. Provide one or more complete samples of AOP code with good comments describing the purpose of the code.

2. Create a QnA related to the material and email to dean@ituniversity.us

More recommended reading:
http://www.javaworld.com/article/2073918/core-java/i-want-my-aop---part-1.html?page=2
http://javabeat.net/introduction-to-springs-aspect-oriented-programmingaop/
http://www.informit.com/articles/article.aspx?p=174533&seqNum=2
http://www.christianschenk.org/blog/aop-with-aspectj/
https://docs.jboss.org/aop/1.0/aspect-framework/userguide/en/html/what.html
https://www.pluralsight.com/courses/aspect-oriented-programming-spring-aspectj
https://www.tutorialspoint.com/spring/aop_with_spring.htm

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy