Hello Misagh,

I've recently come across similar discussions while working on a Java-based application and I'm getting curious about the topic you've just raised. Although I'm still attached to the field injections (@Autowired) way, I'd be happy to discover new paths about that! From my side the best advantages might show up on the "test context and component scanning" side, since sometimes it's kind of a pain to deal with that!

So, thank you for proposing (highly possible) better alternatives and I'm gonna carefully follow the discussion!

Regards,
Matteo


On 19/02/21 11:41, Misagh Moayyed wrote:
Hello all,

I want to discuss and propose a design change in the way Syncope components, 
specially *Logic classes are constructed. For a concrete example, this 
component [1] might be a good baseline.

Components such as [1] do two things that seem less than ideal:

1) The class is directly annotated with a @Component
2) It uses field-injections by annotating fields with @Autowire and such

I submit that this approach generally proves challenging, specially when it comes to 
constructing a context for integration tests and dealing with classpath scanning. There 
is lot of literature on why this (field injections) might not be an ideal approach; The 
"test context and component scanning" is one practical example that I myself 
ran into; Purists might also argue that business-level components and logic classes 
should not be tied to the upper framework per se (though I don't actually find myself in 
this camp all too often!).

A better alternative perhaps would be:

- Avoid field-injections and use ctor-level injections (this is the general 
recommendation from Spring)
- Do not use autowire/component/etc directly in business-level classes
- ...which means do not rely as much (if ever) on classpath/context scanning
- ...and instead, create and instantiate the bean directory in @Configuration 
classes, conditionally and with direct control
- ...or use a middle-ground for now, with something like this [2]

The work feels largely cosmetic perhaps; I think it will pay off in the future 
specially if it's something that is advocated by Spring and family.

WDYT?

--Misagh

[1] 
https://github.com/apache/syncope/blob/master/core/am/logic/src/main/java/org/apache/syncope/core/logic/OIDCJWKSLogic.java
[2] 
https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-spring-beans-and-dependency-injection.html

Reply via email to