Skip to content

Commit c45230d

Browse files
authored
Merge pull request #397 from Yashgabani845/yash-work4
spring tutorial added
2 parents 22b4d70 + 157a862 commit c45230d

File tree

7 files changed

+1310
-0
lines changed

7 files changed

+1310
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Spring and Spring-boot",
3+
"position": 10,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "In this section, you will learn about very famous frameworks of core jaava spring and springboot"
7+
}
8+
}
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
---
2+
id: aspect oriented programming
3+
title: Aspect oriented programming(AOP)
4+
sidebar_label: Spring AOP Tutorial
5+
sidebar_position: 4
6+
tags: [java, spring,core-java, programming, java core, java spring, java web, AOP, aspect oriented]
7+
description: in thi tutorial you will learn about aspect oriented programming and how aspect6s are working and when to use it and how to use it.
8+
---
9+
### Spring AOP Tutorial
10+
11+
**Aspect-Oriented Programming (AOP)** complements Object-Oriented Programming (OOP) by providing modularity, with aspects as the key unit of modularity rather than classes.
12+
13+
AOP breaks the program logic into distinct parts called concerns, increasing modularity by addressing cross-cutting concerns.
14+
15+
### Why Use AOP?
16+
17+
AOP offers a pluggable way to dynamically add additional concerns before, after, or around the actual logic. For instance, consider a class `A` with multiple methods:
18+
19+
```java
20+
class A {
21+
public void m1() { ... }
22+
public void m2() { ... }
23+
public void m3() { ... }
24+
public void m4() { ... }
25+
public void m5() { ... }
26+
public void n1() { ... }
27+
public void n2() { ... }
28+
public void p1() { ... }
29+
public void p2() { ... }
30+
public void p3() { ... }
31+
}
32+
```
33+
34+
To maintain logs and send notifications after calling methods starting with `m`, you would traditionally have to write this logic in each method. However, if the client decides to change the notification requirement, you would need to update all methods, leading to maintenance problems.
35+
36+
With AOP, you define additional concerns like logging and notification separately and manage them via configuration. This makes maintenance easier, as changes can be made in a single place.
37+
38+
### Where to Use AOP?
39+
40+
AOP is mainly used for:
41+
42+
1. Declarative enterprise services such as transaction management.
43+
2. Implementing custom aspects.
44+
45+
### AOP Concepts and Terminology
46+
47+
**Join Point**: A point in the program such as method execution, exception handling, etc. Spring supports only method execution join points.
48+
49+
**Advice**: An action taken by an aspect at a join point. Types of advice include:
50+
- **Before Advice**: Executes before a join point.
51+
- **After Returning Advice**: Executes after a join point completes normally.
52+
- **After Throwing Advice**: Executes if a method exits by throwing an exception.
53+
- **After (finally) Advice**: Executes after a join point regardless of its exit.
54+
- **Around Advice**: Executes before and after a join point.
55+
56+
**Pointcut**: An expression language of AOP that matches join points.
57+
58+
**Introduction**: Introduction of additional methods and fields for a type, allowing new interfaces for advised objects.
59+
60+
**Target Object**: The object being advised by one or more aspects, also known as the proxied object.
61+
62+
**Aspect**: A class containing advices, join points, etc.
63+
64+
**Interceptor**: An aspect containing only one advice.
65+
66+
**AOP Proxy**: Implements aspect contracts, created by the AOP framework. In Spring, it can be a JDK dynamic proxy or a CGLIB proxy.
67+
68+
**Weaving**: The process of linking aspects with other application types to create an advised object, done at compile time, load time, or runtime. Spring AOP performs weaving at runtime.
69+
70+
### AOP Implementations
71+
72+
1. **AspectJ**
73+
2. **Spring AOP**
74+
3. **JBoss AOP**
75+
76+
### Spring AOP Usage
77+
78+
Spring AOP can be used in three ways, with the most common being the Spring AspectJ Annotation Style:
79+
80+
1. By Spring 1.2 Old style (DTD based)
81+
2. By AspectJ annotation style
82+
3. By Spring XML configuration style
83+
84+
### Spring AOP AspectJ Annotation Example
85+
86+
**Annotations:**
87+
- `@Aspect`: Declares the class as an aspect.
88+
- `@Pointcut`: Declares the pointcut expression.
89+
- **Advice Annotations:**
90+
- `@Before`: Applied before the actual method.
91+
- `@After`: Applied after the actual method and before returning the result.
92+
- `@AfterReturning`: Applied after the method completes, allows access to the return value.
93+
- `@Around`: Applied before and after the actual method.
94+
- `@AfterThrowing`: Applied if the method throws an exception.
95+
96+
**Example:**
97+
98+
```java title="Operation.java"
99+
package com.javatpoint;
100+
101+
public class Operation {
102+
public void msg() { System.out.println("msg method invoked"); }
103+
public int m() { System.out.println("m method invoked"); return 2; }
104+
public int k() { System.out.println("k method invoked"); return 3; }
105+
}
106+
```
107+
108+
(Aspect with Before Advice)
109+
```java title="TrackOperation.java"
110+
package com.javatpoint;
111+
112+
import org.aspectj.lang.JoinPoint;
113+
import org.aspectj.lang.annotation.Aspect;
114+
import org.aspectj.lang.annotation.Before;
115+
import org.aspectj.lang.annotation.Pointcut;
116+
117+
@Aspect
118+
public class TrackOperation {
119+
@Pointcut("execution(* Operation.*(..))")
120+
public void k() {} // Pointcut name
121+
122+
@Before("k()") // Applying pointcut on before advice
123+
public void myadvice(JoinPoint jp) { // This is advice
124+
System.out.println("additional concern");
125+
// System.out.println("Method Signature: " + jp.getSignature());
126+
}
127+
}
128+
```
129+
130+
(Spring Configuration)
131+
```xml title="applicationContext.xml"
132+
<?xml version="1.0" encoding="UTF-8"?>
133+
<beans xmlns="http://www.springframework.org/schema/beans"
134+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
135+
xmlns:aop="http://www.springframework.org/schema/aop"
136+
xsi:schemaLocation="http://www.springframework.org/schema/beans
137+
http://www.springframework.org/schema/beans/spring-beans.xsd
138+
http://www.springframework.org/schema/aop
139+
http://www.springframework.org/schema/aop/spring-aop.xsd">
140+
141+
<bean id="opBean" class="com.javatpoint.Operation"></bean>
142+
<bean id="trackMyBean" class="com.javatpoint.TrackOperation"></bean>
143+
144+
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
145+
</beans>
146+
```
147+
148+
(Main Class)
149+
```java title="Test.java"
150+
package com.javatpoint;
151+
152+
import org.springframework.context.ApplicationContext;
153+
import org.springframework.context.support.ClassPathXmlApplicationContext;
154+
155+
public class Test {
156+
public static void main(String[] args) {
157+
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
158+
Operation e = (Operation) context.getBean("opBean");
159+
System.out.println("calling msg...");
160+
e.msg();
161+
System.out.println("calling m...");
162+
e.m();
163+
System.out.println("calling k...");
164+
e.k();
165+
}
166+
}
167+
```
168+
169+
**Output:**
170+
```
171+
calling msg...
172+
additional concern
173+
msg() method invoked
174+
calling m...
175+
additional concern
176+
m() method invoked
177+
calling k...
178+
additional concern
179+
k() method invoked
180+
```
181+
182+
The additional concern is printed before each method invocation.
183+
184+
### Other Examples
185+
186+
#### @After Example
187+
188+
```java title="TrackOperation.java"
189+
@After("k()") // Applying pointcut on after advice
190+
public void myadvice(JoinPoint jp) {
191+
System.out.println("additional concern");
192+
// System.out.println("Method Signature: " + jp.getSignature());
193+
}
194+
```
195+
196+
#### @AfterReturning Example
197+
```java title="TrackOperation.java"
198+
@AfterReturning(pointcut = "execution(* Operation.*(..))", returning = "result")
199+
public void myadvice(JoinPoint jp, Object result) {
200+
System.out.println("additional concern");
201+
System.out.println("Method Signature: " + jp.getSignature());
202+
System.out.println("Result in advice: " + result);
203+
System.out.println("end of after returning advice...");
204+
}
205+
```
206+
207+
#### aop:before Example (XML Configuration)
208+
```java title="TrackOperation.java"
209+
package com.javatpoint;
210+
import org.aspectj.lang.JoinPoint;
211+
public class TrackOperation {
212+
public void myadvice(JoinPoint jp) { // It is advice
213+
System.out.println("additional concern");
214+
// System.out.println("Method Signature: " + jp.getSignature());
215+
}
216+
}
217+
```
218+
219+
220+
```xml title="applicationContext.xml"
221+
<aop:config>
222+
<aop:aspect id="myaspect" ref="trackAspect">
223+
<!-- @Before -->
224+
<aop:pointcut id="pointCutBefore" expression="execution(* com.javatpoint.Operation.*(..))" />
225+
<aop:before method="myadvice" pointcut-ref="pointCutBefore" />
226+
</aop:aspect>
227+
</aop:config>
228+
```
229+
230+
#### aop:after Example (XML Configuration)
231+
```xml
232+
<aop:config>
233+
<aop:aspect id="myaspect" ref="trackAspect">
234+
<!-- @After -->
235+
<aop:pointcut id="pointCutAfter" expression="execution(* com.javatpoint.Operation.*(..))" />
236+
<aop:after method="myadvice" pointcut-ref="pointCutAfter" />
237+
</aop:aspect>
238+
</aop:config>
239+
```
240+
241+
#### aop:after-returning Example (XML Configuration)
242+
```xml
243+
<aop:config>
244+
<aop:aspect id="myaspect" ref="trackAspect">
245+
<!-- @AfterReturning -->
246+
<aop:pointcut id="pointCutAfterReturning" expression="execution(* com.javatpoint.Operation.*(..))" />
247+
<aop:after-returning method="myadvice" returning="result" pointcut-ref="pointCutAfterReturning" />
248+
</aop:aspect>
249+
</aop:config>
250+
```
251+
252+
### Summary
253+
Spring AOP helps in separating cross-cutting concerns like logging, transaction management, etc., from the business logic. This modular approach makes maintenance and evolution of the codebase easier and more manageable.

0 commit comments

Comments
 (0)