Questions tagged [aop]

AOP stands for Aspect-Oriented Programming. Use this tag if your question is about aspect-oriented concepts and techniques, or for programming problems using AOP extensions in any language. AOP increases modularity by allowing the separation of "cross-cutting concerns" into aspects. Click learn more... to find out what it's all about.

AOP stands for aspect-oriented programming. Questions about aspect-oriented concepts, techniques, and programming problems using AOP extensions in any language should have this tag.

AOP exists because there are two types of requirements in software engineering: functional requirements (concerns) describe specific behaviors; non-functional requirements (cross-cutting concerns) describe general qualities or services. In OOP, concerns are implemented in a very modular way -- all of the code for a concern is kept together, usually in a class. This is a good thing because modular code increases software quality.

However, cross-cutting concerns cannot be modularized in OOP (that's why they're called "cross-cutting" because they 'cut across' the functional concerns). Code for a cross-cutting concern ends up being spread out (tangled) over many, or even all, of the modules in an OOP program. AOP fixes this problem by collecting that spread-out code into one module called an aspect.

Logging, caching, security, and transaction management are examples of cross-cutting concerns. AOP makes it straightforward to retrofit existing applications with any of these services. The original code is not modified. Instead, an aspect is created with advice and pointcuts. Advice is like a class method - it contains code with the new functionality to be added. A pointcut is code that selects one or more join points. Join points are specific places in the existing program where the advice will be applied. The new program is created during a process called weaving, when the original code and the aspect code are integrated with each other. Weaving can be done at compile-time or load-time, so you can add aspects to programs even when you don't have the source code (there are some restrictions, see here for example).

The most widely used AOP language is AspectJ, which is an aspect-oriented enhancement to Java. Using AOP in the Spring framework is also popular here on SO. AOP is accessible in a number of ways including command-line, Eclipse, Spring, and IntelliJ Ultimate. AOP is also available for several other languages: Python, JavaScript, Ruby, Lua, and Smalltalk.

AOP was first used in 1997 at Xerox PARC to solve the problem of tangled cross-cutting concerns in object-oriented programs.

AOP-related SO Tags:

3657 questions
1
vote
1 answer

ILWeaving help - [ValidSystemPath] attribute

Problem I'm using Mono.Cecil to IL Weave string property getters that have my custom [ValidSystemPath] attribute on them. The purpose of the attribute is to ensure the property only ever returns valid system characters for file names and paths etc.…
Dom
  • 213
  • 3
  • 10
1
vote
1 answer

AspectJ pointcut not working with external classes and LTW

I am trying to use AspectJ (which until yesterday I didn’t know) with LTW in order to understand how an existing framework works. In brief, I am interested at how framework's input XML files get parsed. I have written the following aspect: import…
Federico
  • 561
  • 2
  • 11
  • 32
1
vote
1 answer

Curious Spring AOP Exception

I'm trying to get my first-ever Spring AOP-based MethodInterceptors working, and am getting some bizarre exceptions with my ultra-simple XML configuration. I have a class named Food which has a method called eat(ConsumptionRate rate) that takes an…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
1
vote
2 answers

How can I use an Aspect on a SpringBootTest

I have an aspect that I have created that does not work when utilised directly on a Test method, but does work when added to a helper component. How can I get this working when I use it directly. So in the code below testAspect fails, but…
James Hutchinson
  • 841
  • 2
  • 13
  • 27
1
vote
1 answer

Aspect not getting called on non-spring maven project

I'm using Java 8, testng, maven and selenium for UI automation frame work and trying to use aspectj to execute aspects before and after for switching iframes(entry and exit) when any method in an object is called. Aspect never gets called when i…
gamelover
  • 15
  • 4
1
vote
2 answers

Why doesn't Spring support the order of annotations advice at the method level?

@Component @Aspect @Slf4j(topic = "e") public class NotVeryUsefulAspect{ @Pointcut("within(com.lc.aop.for_source.service.impl.AAopServiceImpl)") public void pointCutWithinAAopService(){ } …
1
vote
1 answer

Using Aspects in a java application - when javac/ajc?

I think about using AspectJ in an existing project. I have several pure Java Eclipse projects and I like to create an AOP project. I'm not quite sure about when ajc is needed and when optional. We use Ant (with javac) as our main build and I would…
Emerson
  • 1,327
  • 2
  • 15
  • 24
1
vote
2 answers

Spring AOP pointcut execution not working

I'm working on a Spring Boot project that uses Spring Cloud (io.awspring.cloud:spring-cloud-aws-dependencies:2.4.2) to produce and consume AWS SQS messages. I have several message producers and several message consumers, and all is working fine from…
Nathan Russell
  • 3,428
  • 5
  • 30
  • 51
1
vote
2 answers

Which version of AspectJ is suitable for Java 1.4.2?

I can seem to find an guide for 1.4.2 compatibility. Can someone who is good in AOP give me a heads up. http://www.eclipse.org/aspectj/downloads.php
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
1
vote
1 answer

When does a Spring @PostFilter execute?

I am using spring's @Around annotations to provide caching of DAO calls and also using spring security's @PostFilter to restrict the viewing of certain records. However I am concerned that the cache could bypass the PostFilter and present a security…
chotchki
  • 4,258
  • 5
  • 34
  • 55
1
vote
1 answer

AfterReturning advice not executed

@Component Class A { public String methodA() { return methodB(); } @Publish public String methodB() { return "abc"; } } @Component @Aspect public class PublishEvents { …
javaguy
  • 927
  • 2
  • 16
  • 37
1
vote
1 answer

AspectJ compatibility

We are using an external library that has been compile-time weaved with AspectJ 1.9.6. We would like to use Java 17 and thus need at least AspectJ 1.9.8 to compile-time weave our own code. During runtime we have a shared classpath (WEB-INF/lib) for…
fliX
  • 773
  • 8
  • 24
1
vote
1 answer

How to use aspect-oriented programming (AOP) in Go?

I am a Go rookie. I want to use the Prometheus monitoring function level and try to be as generic as possible, like spring transactional. Using aspect oriented programming. I tried the following: type BizFunc func() func DoAndMonitor(bizFunc…
Feng Kai
  • 27
  • 2
1
vote
2 answers

Spring AOP different advice execution order?

I used spring-boot-starter-parent 2.7.4 to test the execution sequence of @Around @Before @After @AfterReturning,here is what I got: (The method of my point cut is to print "hello, world") If I do not use @Around advice: before ...... hello,…
Name Null
  • 390
  • 2
  • 11
1
vote
1 answer

How to handle exception in service layer

Springboot provides @ControllerAdvice to handle exceptions in controllers. But in service layer, there is no such annotations. I've learned about spring-aop uses @AfterThrowing track exception, but @AfterThrowing cannot catch exceptions. Another…
buzz
  • 11
  • 1