What is Spring AOP – Let’s explore

Spring AOP

Spring Series – Learn Spring AOP by Example

AOP Basics

In today’s complex application development the following points are always kept as a focus area.

  • Application should be developed as a combination of modular components.
  • All the components should be loosely coupled.
  • Components should be reusable.

The advantage of this approach is

  • Easy maintenance and enhancement of the application.
  • Components can be reused in the existing or new application.
  • Reduces the cost of maintenance and enhancement.

Now to address these concerns spring has developed the concepts like IOC and AOP. We have already discussed IOC so now we will concentrate on AOP.

Spring AOP

AOP targets the application functionalities (like logging, transaction etc) which are used throughout the application in different areas. Using AOP these cross-cutting functionalities are developed as a reusable component and injected into the application where ever required. And again the entire configuration is done using xml file.

Implementation of AOP

Following diagram shows a travel management system. ‘Ticket Booking’, ‘Hotel Booking’ and ‘Travel Planning’ are different functionalities. Now all of these functionalities require services like logging, transaction and security. So these are cross cutting concerns which come across all the modules. These concerns are developed based on the principle of AOP.

All the common concerns are developed as a plug-in, so it can be used in future modules as well. You just need to configure it so that the service is also available in your new modules. This is the power of aspect oriented programming (AOP).

AOP is also applied in enterprise level applications. In enterprise application, transaction, logging, security and many more features are there as cross cutting concerns. So all of these are developed as aspects and configured in xml file.

The biggest advantage is loose coupling. As all the concerns are self-managed and independent, so you can change and enhance them as per your requirement without touching the application code.







Travel Management System

Now we will describe the AOP implementation by showing an example.

To start with, first we will define an interface and its implementation class. This is a simple application to compare two integer values and return the result.

Java is the interface definition to compare two integer values

package com.techalpine;

public interface CompareInt{

public String compare(int a,int b);

}

Java is the implementation class. Tā salīdzina divus veselus skaitļus un atgriež rezultātu,,en,valsts klases CompareIntImpl īsteno CompareInt,,en,atgriezt "ir lielāks nekā" b,,en,b ir lielāks nekā,,en,Tagad mēs uzrakstīt vienkāršu padomus saukt pirms zvanot salīdzināt,,en,Konsultācijas tiks apspriesti informāciju šādās nodaļās,,en,Tagad jūs varat iedomāties, padomu, kā uzdevums, kas tiks pieslēgts pieteikumu caur xml konfigurācijas,,en,Un tas tiks saukts pirms metode tiek saukta,,en,Bet tur nebūs nekādu tiešu kodēšana iekšpusē īstenošanas klasē,,en,Mēs veiksim tikai pirms padomu īstenošanas klasei,,en,imports java.lang.reflect.Method,,en,imports org.springframework.aop.MethodBeforeAdvice,,en,valsts klases LogBefore īsteno MethodBeforeAdvice,,en,valsts spēkā pirms,,en,Objekta mērķis,,en,Pirms Calling salīdzināt metode,,en

package com.techalpine;

public class CompareIntImpl implements CompareInt{

public String compare(int a, int b){

ja (a>b)

{

return a ” is greater than” b;

}

else

{

return “b is greater than a”;

}

}

}

Now we will write a simple advice to be called before calling the compare (int, int) metode. The advice will be discussed in details in the following chapters. Now you can imagine advice as a task which will be plugged into the application through xml configuration. And this will be called before the method is called. But there will not have any direct coding inside the implementation class. We will only take before advice for the implementation class.







package com.techalpine;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class LogBefore implements MethodBeforeAdvice{

public void before(Method method, Object[] args, Object target) {

Println(“Before Calling the compare Method”);

}

}

Tagad mēs diskutēt par konfigurācijas failu,,en,Test-AOP.xml,,en,Fails ir trīs sadaļas,,en,Pirmais posms ir padoms pupiņas definīcija,,en,Otrajā nodaļā nosaka ieviešanas klasi,,en,Trešā sadaļa definē saistošo konsultācijas ar īstenošanas klase,,en,www.springframework.org/schema/beans/spring-beans-2.0.xsd,,en,padomi,,en,pupiņu id =,,en,beforeCall,,en,class =,,en,com.techalpine.LogBefore,,pt,īstenošana klase,,en,compareImpl,,en,com.techalpine.CompareIntImpl,,en,Proxy īstenošana klase,,en,salīdzināt,,en,org.springframework.aop.framework.ProxyFactoryBean,,en,property name =,,en,proxyInterfaces,,en,com.techalpine.CompareInt,,en,interceptorNames,,en,ref pupu =,,en,Tagad pēdējā daļa ir testa klasi CompareTest.java,,en,Tā ielādē pupiņu definīciju, izmantojot XMLBeanFactory klasi, un tad sauc salīdzināt,,en,imports org.springframework.beans.factory.BeanFactory,,en,imports org.springframework.beans.factory.xml.XmlBeanFactory,,en (Test-AOP.xml)

The file has three sections

  • The first section is the advice bean definition
  • The second section defines the implementation class
  • The third section defines the binding of the advice with the implementation class

<beans xmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd”>

<!– Advices –>

<bean id = “beforeCall”

class = “com.techalpine.LogBefore” />

<!– Implementation Class –>

<bean id = “compareImpl”

class = “com.techalpine.CompareIntImpl ” />

<!– Proxy Implementation Class –>

<bean id = “compare”

class = “org.springframework.aop.framework.ProxyFactoryBean”>

<property name = “proxyInterfaces”>

<vērtība>com.techalpine.CompareInt</vērtība>

</īpašums>

<property name = “interceptorNames”>

<list>

<vērtība>beforeCall</vērtība>

</list>

</īpašums>

<property name = “target”>

<ref bean = “compareImpl”/>

</īpašums>

</bean>

</beans>

Now the last part is the test class CompareTest.java. It loads the bean definition by using XMLBeanFactory class and then called the compare () metode.

package com.techalpine;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.*;

public class CompareTest {

valsts statisko spēku galvenās(String args[]){

Resource resource = new FileSystemResource(“./src/Test-AOP.xml”);

BeanFactory factory = new XmlBeanFactory(resource);

CompareInt com = (Adder)factory.getBean(“compare”);

String result = com.compare(12,10);

Println(“Result = ” + result);

}

}

The output of the compare () method call would be as follows

Before calling the compare Method

12 is greater than 10







Secinājums:

Spring AOP is very helpful when we try to separate business logic from cross cutting concerns. It is widely used in different forms to cover various aspects. AOP can also be implemented in other programming languages.

 

Tagged on: ,
============================================= ============================================== Pērciet labākās Techalpine grāmatas vietnē Amazon,en,Elektriķa CT kastaņu valodas,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share