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 기능은 재사용 부품으로 개발 토록 요구되는 어플리케이션에 주입,,en,그리고 다시 전체 구성은 XML 파일을 사용하여 수행됩니다,,en,AOP 구현,,en,다이어그램을 따르는 것은 여행 관리 시스템을 보여줍니다,,en,티켓 예약 ',,en,호텔 예약',,en,여행 계획,,en,다른 기능은 '있다,,en,이제 이러한 기능을 모두 로깅 등의 서비스를 필요로,,en,거래 및 보안,,en,그래서 이러한 모든 모듈 건너 교차 절단 우려가 있습니다,,en,이 문제는 AOP의 원리를 기반으로 개발된다,,en,모든 일반적인 문제는 플러그인으로 개발,,en,그래서 그것은뿐만 아니라 미래의 모듈을 사용할 수 있습니다,,en,이 서비스는 또한 새로운 모듈에서 사용할 수 있도록 당신은 그것을 구성해야,,en,이 관점 지향 프로그래밍의 힘,,en,AOP,,en. 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는 엔터프라이즈 급 애플리케이션에 적용된다,,en,엔터프라이즈 응용 프로그램에서,,en,벌채 반출,,en,보안 및 더 많은 기능 크로스 커팅 문제로있다,,en,그래서 이들 모두 측면으로 개발 및 XML 파일에서 구성됩니다,,en,가장 큰 장점은 느슨한 커플 링,,en,모든 문제는 자기 관리와 독립적으로,,en,그래서 당신은 변경하고 응용 프로그램 코드를 건드리지 않고 귀하의 요구 사항에 따라 그것들을 향상시킬 수 있습니다,,en,여행 관리 시스템,,en,이제 우리는 예를 보여줌으로써 AOP 구현을 설명합니다,,en,첫째 우리는 인터페이스와 구현 클래스를 정의합니다,,en,이 두 개의 정수 값을 비교하고 결과를 반환하는 간단한 응용 프로그램입니다,,en,자바는 두 개의 정수 값을 비교하는 인터페이스 정의입니다,,en,패키지 com.techalpine,,en,공용 인터페이스의 CompareInt,,en,공공 문자열 비교,,en,자바 구현 클래스입니다,,en. 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. 그것은 두 개의 정수 값을 비교하여 결과를 반환,,en,공용 클래스 CompareIntImpl는 CompareInt를 구현,,en,돌려 "보다 크다" B,,en,B는보다 큰,,en,이제 우리는 비교 호출하기 전에 호출 할 수있는 간단한 조언을 작성합니다,,en,조언은 다음 장에서 상세하게 논의 될 것이다,,en,이제 XML 구성을 통해 응용 프로그램에 연결해야하는 작업으로 조언을 상상할 수,,en,그리고이 메소드를 호출하기 전에 호출됩니다,,en,그러나 구현 클래스 내부에 직접 코딩이 필요가 없습니다,,en,우리는 단지 구현 클래스에 대한 조언을하기 전에 취할 것,,en,수입 java.lang.reflect.Method의,,en,수입 org.springframework.aop.MethodBeforeAdvice,,en,공용 클래스 LogBefore는 MethodBeforeAdvice를 구현,,en,이전 공공 무효,,en,개체 대상,,en,비교] 메서드를 호출하기 전에,,en

package com.techalpine;

public class CompareIntImpl implements CompareInt{

public String compare(int a, int b){

면 (에이>B)

{

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, Object[] 인수, Object target) {

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

}

}

이제 우리는 구성 파일에 대해 논의 할 것이다,,en,테스트 - AOP.xml,,en,이 파일은 세 개의 섹션으로 구성되어 있습니다,,en,첫 번째 섹션은 조언 bean 정의이다,,en,두 번째 섹션은 구현 클래스를 정의,,en,세 번째 섹션은 구현 클래스와 조언의 바인딩을 정의,,en,www.springframework.org/schema/beans/spring-beans-2.0.xsd,,en,조언,,en,콩 ID =,,es,beforeCall,,en,클래스 =,,en,com.techalpine.LogBefore,,pt,구현 클래스,,en,compareImpl,,en,com.techalpine.CompareIntImpl,,pt,프록시 구현 클래스,,en,비교,,en,인 org.springframework.aop.framework.ProxyFactoryBean,,en,속성 이름 =,,en,proxyInterfaces,,en,com.techalpine.CompareInt,,pt,인터셉터,,en,심판 콩 =,,es,이제 마지막 부분은 테스트 클래스 CompareTest.java입니다,,en,그것은은 XmlBeanFactory 클래스를 사용하여 bean 정의를로드 한 후라는 비교,,en,수입에게 org.springframework.beans.factory.BeanFactory,,en,수입 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”>

<가치>com.techalpine.CompareInt</가치>

</재산>

<property name = “interceptorNames”>

<명부>

<가치>beforeCall</가치>

</명부>

</재산>

<property name = “target”>

<ref bean = “compareImpl”/>

</재산>

</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 () 방법.

package com.techalpine;

import org.springframework.beans.factory.BeanFactory;

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

org.springframework.core.io 가져올 수 있습니다. *,,en,공용 클래스 CompareTest,,en,자원 자원 = 새로운 FileSystemResource,,en,SRC / 시험 - AOP.xml,,en,BeanFactory에 공장 = 새로운은 XmlBeanFactory,,en,의지,,en,CompareInt =으로,,pt,가산기,,en,factory.getBean,,en,문자열 결과 = com.compare,,en,결과 =,,en,결과,,en,비교]의 출력,,en,다음과 같이 메서드 호출 될 것이다,,en,비교] 메서드를 호출하기 전에,,en,보다 큰,,en,우리는 크로스 커팅 문제에서 비즈니스 로직을 분리 할 때 스프링 AOP는 매우 도움이된다,,en,널리 다양한 측면을 덮도록 다양한 형태로 사용되는,,en,AOP는 다른 프로그래밍 언어에서 구현 될 수있다,,en;

public class CompareTest {

공공 정적 무효 메인(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);

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: ,
============================================= ============================================== 아마존에서 최고의 Techalpine 책을 구입하십시오,en,전기 기술자 CT 밤나무 전기,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share