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
<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 )
Was it clear so far?
onclick="window.location.href='/BASE/jsp/demo.jsp?checkFlavor=itsp&issueID=403&intro=general&group=aitu&ur=f'">
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