Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configurable vs Component with Spring and AspectJ

When using AspectJ, why use @Component over @Configurable.

I've got Spring and AspectJ setup for @Transactional support, aspects on self-invocation, and injection into JPA entities. This works great.

I'm using @Component for most classes that need injection, and thus either having them injected into their dependencies. Or, when I can't, injecting the ApplicationContext and then using getBean() as a last resort. And I'm reserving @Configurable for only JPA entities (Hibernate) that need injection. I've also started using @Configurable for jUnit tests, to make writing tests easy. This also works great.

But I'm curious. If AspectJ is now auto-injecting (beanifying) anything with the @Configurable annotation, regardless of how its constructed; getBean(), new(), @Autowired. Why wouldn't I just switch to using @Configurable for all my beans? Then I can do away with the application context and getBean() altogether, and just new() any classes I can't inject.

I realize that I made no mention of XML bean configuration. I don't shy away from that, but this project doesn't happen to require any. I just constructor or setter inject the dependencies when testing. very easy.

like image 327
DragonFax Avatar asked May 20 '09 04:05

DragonFax


People also ask

Does spring use AspectJ?

The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

What is the use of @aspect annotation in spring?

Spring AspectJ AOP implementation has many annotations. They are explained below: @Aspect: It declares the class as an aspect. @Pointcut: It declares the pointcut expression.

What type of Aspect weaving is supported by Spring AOP?

Weaving can be done at compile time, at load time, or at runtime. There are two ways in which classes and aspects can be woven: static or dynamic. Spring AOP does dynamic weaving of aspects by creating proxy of the target objects.

What is @aspect annotation in spring boot?

@AspectJ refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations. The @AspectJ style was introduced by the AspectJ project as part of the AspectJ 5 release.


2 Answers

@Component is a Spring marker interface which can give Spring clues when it comes to auto-detection of beans.

@Configurable is a marker used by the AOP load-time-weaving stuff.

The two don't really have much to do with each other.

like image 189
skaffman Avatar answered Sep 19 '22 16:09

skaffman


@Component is for classes which will be instantiated by Spring itself, while @Configurable is for classes which will be instantiated by your own code, or by another framework — entities by Hibernate or Servlets by the servlet container, for instance.

like image 31
Daniel Serodio Avatar answered Sep 22 '22 16:09

Daniel Serodio