-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default condition to avoid field Injection and haveConstructor Injection #288
Comments
In my own library of ArchUnit rules I added a similar rule to prevent field injection: public static final ArchRule DO_NOT_USE_FIELD_INJECTION = noFields()
.should(beAnnotatedWith("org.springframework.beans.factory.annotation.Autowired"))
.orShould(beAnnotatedWith("org.springframework.beans.factory.annotation.Value"))
.because("field injection is evil, see http://olivergierke.de/2013/11/why-field-injection-is-evil/"); Maybe we could add this to |
Resolves TNG#288 Signed-off-by: Roland Weisleder <roland.weisleder@googlemail.com>
Thanks for including the fieldInjectioncheck Ensuring CDI
|
I was just trying add another condition like say if we have a field with value initialised ,then we will have filter it out as it dosent has to be a part of CDI List classFields = javaClass.getAllFields().stream().collect(Collectors.toList()); Regards, |
@smfaizalkhan, if you want to ensure that your developers use constructor injection when using CDI, I suggest making all fields final. This has two main advantages:
Or is there a reason I'm missing why classes that are created by the CDI container should not contain final fields? |
Thanks ,we have used it this way
Interface
|
@rweisleder
but i don't get the below highlighted class
Is it part of different package if so ,what is it? Regards, |
However, when using ArchUnit with JUnit5†, you don't have to use this low-level import on your own, but can use ArchUnit's JUnit support. † By the way: 1. you don't need to declare the |
Thanks for the response,BTW scope tag was a mistake ,we are scoping it as test .Dont how it popped here :) |
My take on They have meanwhile created a bunch of generally available rules, maybe it would make sense to add some to ArchUnit itself and reuse them from ArchUnit Maven and Gradle Plugin... Should discuss this at some point, how they would feel about it. With the field injection rule I just have some stomach pain because it's super dependent on your framework. Do you use Spring, CDI, Guice, xyz, every time there are different framework specific annotations. Maybe it would work to write a generic rule that simply flags all fields as illegal that don't have a single "set access" 😉 (because those are neither initialized in the constructor nor have any setter, thus it must be some reflection magic at work or they are useless) |
Resolves TNG#288 Signed-off-by: Roland Weisleder <roland.weisleder@googlemail.com>
Resolves TNG#288 Signed-off-by: Roland Weisleder <roland.weisleder@googlemail.com>
Resolves #288 Signed-off-by: Roland Weisleder <roland.weisleder@googlemail.com>
I'm fascinated by the API and have started using it our project.Lately i discovered that few
people in our project were using fieldInjection and not constructor injections
So we thought of using ArchRule and was expecting a default rule as below .
But by default it was not available ,so we wrote a conditions to it,
Avoiding Field Injection
Ensuring CDI
Would like to know if it can be added as default behaviour in the API.
Regards,
Faizal
The text was updated successfully, but these errors were encountered: