- An application consists of following layers
- Web Layer
- Business Layer
- Used to handle Business Services
- Data Layer
- Used to handle Data Services
- Database
- An Application has following components basically
- Logging
- Security
- Authentication
- Sanitize the data
- Transaction
- Business Logic
- Out of these Logging,Security,Transaction are common mostly for all methods.
- We can club these in one aspect so they are automatically called.
- We can create a new Logging Aspect.
- We can create a new Security Aspect.
- We can create a new Transaction Aspect.
- We define an Aspect configuration which defines which objects will be applied with which projects.
- We can wrap aspects around our methods.
- If we need to log every-time a business method is called then we need AOP
- We may require to log following information
- What method was called?
- What are the input parameters that are there?
- What would be the value returned back?
- How much time it took to execute that particular method?
- To Automatically perform the above tasks we need AOP.
- Following Steps need to be followed while creating an AOP
- Write aspects.
- Configure where these aspects apply.
- Advice is a part of aspect.
- Aspect oriented programming gives us the flexibility to remove the Cutting concerns from our application And separate them out from main business logic.
- We use aspectj Library With spring framework.
- "@Aspect" annotation is used to mark class as an aspect.
- An approach that helps in separating concerns in a program
- Aspects take care of various common tasks that need to happen in other places. We focus on the problem.
- Example logging, security checks, Managing transactions.
- We can define these once in An aspect and then specify when, and where this code should be applied across applications.
- Keep code cleaner.
- Following Jars along with Spring OOP are required
- aopalliance,jar
- aspectjrt.jar
- aspectjweaver.jar
- Terminologies in AOP
- Aspect
- Logging. Aspect is the concern that we are trying to implement generically. Ex: logging, transaction management.
- Pointcut
- An expression which determines what are the methods that the Advice should be applied on.
- What point we need to call it.
- Joinpoint
- a point during the execution of a program, such as the execution of a method or the handling of an exception.
- Weaving
- linking aspects with other application types or objects to create an advised object.
- This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.
- Advice
- action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice.
- Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point.
- What should be called when we call a method.
- By default AOP is not enabled in our system.
- We need to enable it using "@EnableAspectJAutoProxy" annotation.
- We need to create a bean of the Aspect class as well so that it is read by Spring IOC.
LoggingAspect.java
package com.springimplant.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(public String getName())")
public void LoggingAdvice()
{
System.out.println("Advice Run.Get Method Called");
}
}
Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean name="triangle" class="com.springimplant.entities.Triangle">
<property name="name" value="Triangle Name" ></property>
</bean>
<bean name="circle" class="com.springimplant.entities.Circle">
<property name="name" value="Circle Name" ></property>
</bean>
<bean name="shapeService" class="com.springimplant.service.ShapeService" autowire="byName"/>
<bean name="loggingAspect" class="com.springimplant.aspect.LoggingAspect"/>
</beans>
Example:
No comments:
Post a Comment