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
18
votes
2 answers

Mixing JDK and CGLIB proxies within Spring

I have an application running with Spring, and I'm using AOP in some places. Since I want to use the @Transactional annotation at interface level, I have to allow Spring to create JDK proxies. So, I don't set the proxy-target-class property to true.…
Thiago Coraini
  • 311
  • 2
  • 7
18
votes
6 answers

How to test an aspect with SpringBootTest?

I created a simple aspect in Spring using Spring Boot 2.1.6.RELEASE. It basically logs the total time spent on a method. @Aspect @Component public class TimeLoggerAspect { private static final Logger log =…
Natan Cox
  • 1,495
  • 2
  • 15
  • 28
18
votes
4 answers

Aspect-Oriented Programming in Clojure

How to achieve Aspect-Oriented Programming in Clojure? Do we need AOP in Clojure? Let's say we want plain vanilla Clojure solution (no AspectJ).
Chiron
  • 20,081
  • 17
  • 81
  • 133
18
votes
4 answers

Exception java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect

I recently started working on AspectJ and wrote a simple Aspect for Logging. I have defined below dependencies into my pom file: org.aspectj aspectjrt
Atul
  • 1,694
  • 4
  • 21
  • 30
18
votes
5 answers

how to detect a function was called with javascript

I have a javascript function. How to check: if function was called ( in section have this function), then not to call the function if function was not called ( in section haven't this function), then call the…
Chameron
  • 2,734
  • 8
  • 34
  • 46
18
votes
4 answers

Traditional logging vs AOP logging

I'm starting this new project and we are thrashing out our logging/debugging approach and I wanted to put the question to the rest of you on SO, given private final static Logger logger = LoggerFactory.getLogger(getClass()); ... public void…
non sequitor
  • 18,296
  • 9
  • 45
  • 64
18
votes
4 answers

Basic implementation of AOP like attribute using standard .NET Framework

Possible Duplicate: C# wrap method via attributes I'd like to achieve such functionality: [Atomic] public void Foo() { /* foo logic */ } Where [Atomic] attribute is an attribute, which wraps function logic within a transaction…
jwaliszko
  • 16,942
  • 22
  • 92
  • 158
18
votes
2 answers

AspectJ pointcut expression match parameter annotations at any position

I'm trying to define a pointcut expression to match methods which contain a parameter annotated with a specific annotation, regardless of what position the parameter is in. In my case I'm looking for the @Constraint annotation. For example: Matching…
driangle
  • 11,601
  • 5
  • 47
  • 54
17
votes
5 answers

Help and Information about Aspect Oriented Programming

I'm a newcomer to the idea of aspect-oriented programming but I would like to explore the idea of using it on my project for handling logging, reporting, etc. To this end I have some questions: Should I bother exploring this path of AOP for these…
Jeffrey Cameron
  • 9,975
  • 10
  • 45
  • 77
17
votes
5 answers

Aspectj doesn't work with kotlin

i want to use aspectj aop in kotlin,here is my code: my annotation in annotation.lazy_list: Kotlin: package anotation @Retention(AnnotationRetention.RUNTIME) @Target(AnnotationTarget.FUNCTION) annotation class lazy_list my aspectj aop…
junk
  • 917
  • 3
  • 11
  • 29
17
votes
1 answer

Run-time AOP vs Compile-time AOP

What's the advantage and disadvantage of these two type of AOP framework? I'm using Unity as my aop framework, but I guess the compile-time aop framework such as postsharp may have better performance than run-time aop framework? It looks like…
Allen4Tech
  • 2,094
  • 3
  • 26
  • 66
17
votes
1 answer

Aspect-oriented programming in android

What would be the best way to implement Aspect-oriented programming (AOP) in an Android application? Would it be efficient with the mobile battery?
Javi
  • 19,387
  • 30
  • 102
  • 135
17
votes
1 answer

Pointcut matching methods with annotated parameters

I need to create an aspect with a pointcut matching a method if: Is public Its class is annotated with @Controller (Finally does not) One of its parameters (can have many) is annotated with @MyParamAnnotation. I think the first two conditions are…
sinuhepop
  • 20,010
  • 17
  • 72
  • 107
17
votes
2 answers

WCF service attribute to log method calls and exceptions

I have a requirement to log each method call in a WCF service, and any exceptions thrown. This has led to a lot of redundant code, because each method needs to include boilerplate similar to this: [OperationContract] public ResultBase Add(int…
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
16
votes
1 answer

How does one manage object pooling in Spring?

It's my understanding that in Spring, all objects are treated by default as singletons. If singleton is set to false, then a new object will be served at each request. But what if I wanted to pool objects? Say set a range from a min of 1 to a…
Cuga
  • 17,668
  • 31
  • 111
  • 166