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
38
votes
11 answers

Spring 3.0: Unable to locate Spring NamespaceHandler for XML schema namespace

My setup is fairly simple: I have a web front-end, back-end is spring-wired. I am using AOP to add a layer of security on my rpc services. It's all good, except for the fact that the web app aborts on launch: [java] SEVERE: Context initialization…
Nick Hristov
  • 905
  • 1
  • 8
  • 17
38
votes
1 answer

how to retransform a class at runtime

I am tring modify class which already loaded in a jvm. The solution which I found is: 1st Attach an agent to a jvm specified by pid. (e.g. 8191)(Codes: AttachTest) 2nd Find the class which you want modified from those that have already been loaded…
Nick Dong
  • 3,638
  • 8
  • 47
  • 84
38
votes
5 answers

Spring AOP - why do i need aspectjweaver?

i wrote a very simple Aspect with Spring AOP. It works, but i have some problems understanding what is really going on. I don't understand why i have to add the aspectjweaver.jar? The Spring-AOP documentation clearly states that i don't need aspectj…
Mario B
  • 2,102
  • 2
  • 29
  • 41
37
votes
1 answer

Logging, Aspect Oriented Programming, and Dependency Injection - Trying to make sense of it all

I know that logging is a prime use case for AOP. Additionally logging wrappers are also exemplified as cases when you want to use DI so that classes aren't coupled with a specific logging implementation. However, some consider logging wrappers an…
Matt
  • 14,353
  • 5
  • 53
  • 65
36
votes
4 answers

Is AOP a type of decorator pattern?

I got asked this question in a interview. I clearly know what a decorator pattern is and how it can be used. But I was not able to think through this question in the interview. This is the actual question asked. Is AOP a variation of decorator…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
36
votes
1 answer

Aspect oriented programming - what is 'cflow'?

I have referred to the AspectJ reference here it states that the 'cflow' is cflow(Pointcut) - every join point in the control flow of each join point P picked out by Pointcut, including P itself This isn't entirely lucid for me and I was…
Joeblackdev
  • 7,217
  • 24
  • 69
  • 106
35
votes
2 answers

Aspect oriented programming (AOP) in Python

Possible Duplicate: Any AOP support library for Python? I am familiar with the AspectJ extension for the Java language. I want to know if there is such a thing for Python. Don't get me wrong, I do not mean a library but a language extension like…
coredump
  • 3,017
  • 6
  • 35
  • 53
34
votes
6 answers

How to use AOP with AspectJ for logging?

I would like to add "trace" messages to all my public methods as follows: public void foo(s:String, n:int) { // log is a log4j logger or any other library log.trace(String.format("Enter foo with s: %s, n: %d", s, n)) ... log.trace("Exit foo")…
Michael
  • 10,185
  • 12
  • 59
  • 110
33
votes
3 answers

@AspectJ pointcut for all methods inside package

I have this working code for a specific package, but i want to configure it for all controllers, service and dao…
Mohan Seth
  • 761
  • 2
  • 11
  • 20
33
votes
3 answers

Is using Spring AOP for logging a good idea?

I'm reading up on Spring at the moment and one of the examples used for a use of AOP is logging the start and end of method calls. I've also read that using AOP can impact performance. Is using Spring AOP a good idea for this type of logging? My…
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
33
votes
2 answers

AspectJ "around" and "proceed" with "before / after"

Let's say you have three advices: around, before and after. 1) Are before/after called when proceed is called in the around advice, or are they called before/after the around advice as a whole? 2) If my around advice does not call proceed, will the…
Gérald Croës
  • 3,799
  • 2
  • 18
  • 20
33
votes
8 answers

Intercept the call to an async method using DynamicProxy

Below is the code from the Intercept method on a custom type that implements IInterceptor of the Castle Dynamic Proxy library. This snippet is from an AOP based logging proof-of-concept console app that is posted here. public void…
Hari Pachuveetil
  • 10,294
  • 3
  • 45
  • 68
32
votes
11 answers

What's your "best practice" for the first Java EE Spring project?

I'm currently trying to get into the Java EE development with the Spring framework. As I'm new to Spring, it is hard to imaging how a good running project should start off. Do you have any best practices, tipps or major DO NOTs for a starter? How…
cringe
  • 13,401
  • 15
  • 69
  • 102
32
votes
9 answers

Fixing BeanNotOfRequiredTypeException on Spring proxy cast on a non-singleton bean?

I'm having an issue with pulling a Spring bean from an application context. When I try; InnerThread instance = (InnerThread) SpringContextFactory.getApplicationContext().getBean("innerThread", InnerThread.class); I…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
32
votes
2 answers

Can I do Aspect Oriented Programming in Scala?

I'm not talking about mimicking AOP features in Scala (i.e. using Traits instead of Aspects), I'm wondering if it is possible to do true AOP in Scala (i.e. advices, aspects, joint points, weaving etc ...)
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68