What is Spring AOP – Let’s explore

Spring AOP

გაზაფხულის სერია,,en,ვისწავლოთ Spring AOP by მაგალითი,,en,AOP საფუძვლები,,en,დღევანდელ კომპლექსი განაცხადის განვითარების შემდეგ რაოდენობა ყოველთვის ინახება როგორც აქცენტი ტერიტორიაზე,,en,უპირატესობა ამ მიდგომის,,en,მარტივი შენარჩუნება და გაფართოება განცხადება,,en,კომპონენტები შეიძლება გამოყენებული არსებული ან ახალი განცხადება,,en,ამცირებს ღირებულება შენარჩუნება და გაფართოება,,en,ახლა ამ პრობლემების გაზაფხულზე შეიმუშავა ცნებები, როგორიცაა საერთაშორისო ოლიმპიური კომიტეტის და AOP,,en,ჩვენ უკვე განვიხილეთ საერთაშორისო ოლიმპიური კომიტეტის ასე რომ, ახლა ჩვენ კონცენტრირება AOP,,en,AOP მიზნად განაცხადის ფუნქციები,,en,როგორიცაა ხე,,en,გარიგების ა.შ.,,en,რომლებიც გამოიყენება მთელი პროგრამა სხვადასხვა სფეროში,,en,გამოყენება AOP ეს,,en,ჯვარი ჭრის,,en,ფუნქციები არიან შემუშავებული, როგორც მრავალჯერადი კომპონენტი და გაუკეთეს შევიდა განცხადება, სადაც ოდესმე საჭირო,,en – 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, გარიგების, 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. It compares two integer values and returns the result

package com.techalpine;

საჯარო კლასის CompareIntImpl ახორციელებს CompareInt,,en,დაბრუნდეს "მეტია" b,,en,ბ მეტია,,en,ახლა ჩვენ უნდა დაწეროს მარტივი რჩევა ეწოდოს ადრე მოუწოდებს შედარების,,en,რჩევა იქნება დეტალურად განიხილეს შემდეგი თავები,,en,ახლა თქვენ წარმოიდგინეთ რჩევა, როგორც ამოცანა, რომელიც plugged შევიდა განაცხადის XML კონფიგურაციის,,en,და ეს იქნება ე.წ. ადრე მეთოდი ეწოდება,,en,მაგრამ იქ არ ჰქონდეს პირდაპირი კოდირების შიგნით განხორციელების კლასის,,en,ჩვენ მხოლოდ მიიღოს, სანამ რჩევა განხორციელების კლასის,,en,იმპორტი java.lang.reflect.Method,,en,იმპორტი org.springframework.aop.MethodBeforeAdvice,,en,საჯარო კლასის LogBefore ახორციელებს MethodBeforeAdvice,,en,საჯარო ბათილად ადრე,,en,ობიექტის სამიზნე,,en,ადრე მოუწოდებს შედარების მეთოდი,,en,ახლა ჩვენ განვიხილავთ შესახებ კონფიგურაციის ფაილი,,en,ტესტი AOP.xml,,en{

public String compare(int a, int b){

თუ (a>ბ)

{

return a ” is greater than” b;

}

სხვა

{

return “b is greater than a”;

}

}

}

Now we will write a simple advice to be called before calling the compare (int, int) მეთოდი. 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, ობიექტის[] args, Object target) {

System.out.println(“Before Calling the compare Method”);

}

}

Now we will discuss about the configuration file (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

<ლობიო 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”>

<ღირებულება>com.techalpine.CompareInt</ღირებულება>

</ქონების>

<property name = “interceptorNames”>

<list>

<ღირებულება>beforeCall</ღირებულება>

</list>

</ქონების>

<property name = “target”>

<ref bean = “compareImpl”/>

</ქონების>

</bean>

</ლობიო>

Now the last part is the test class CompareTest.java. It loads the bean definition by using XMLBeanFactory class and then called the compare () მეთოდი.

package com.techalpine;

import org.springframework.beans.factory.BeanFactory;

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

import org.springframework.core.io.*;

public class CompareTest {

საჯარო სტატიკური ბათილად მთავარი(სიმებიანი 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);

System.out.println(“Result = ” + result);

}

}

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

Before calling the compare Method

12 is greater than 10







დასკვნა:

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: ,
============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share