2

For all my @Component and @Service, I use

@RequiredArgsConstructor(onConstructor = @__(@Autowired)).

It makes the code much cleaner an works just fine.

But SonarQube only accepts this for components. All fields in the service classes have the critical issue:

"Annotate this member with "@Autowired", "@Resource", "@Inject", or "@Value", or remove it."

Is there a solution to fix it or a workaround? I don't want to disable the rule, because it helps sometimes.

My code:

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class fooService {

private final FooDAO fooDAO; // Annotate this member with "@Aurowired"...

// rest of the class
}
Raduan Santos
  • 1,023
  • 1
  • 21
  • 45
Genussgift
  • 21
  • 3
  • Have you tried the solution of [this question](https://stackoverflow.com/questions/46362965/sonarqube-lombok-false-positives)? It seems like a similar problem. – Jan Rieke Jan 18 '19 at 10:10

2 Answers2

0

To answer your question, you can simply disable the rules you don't want in SonarQube, it's completely customizable to your needs.

And as of v 4.3 of Spring, you don't even need to put the @Autowired annotation if you have only one constructor, it is implicit, see Spring doc. So you only need this :

@Service
@RequiredArgsConstructor
public class FooService {
  private final FooDAO fooDAO;
}
Matt
  • 3,422
  • 1
  • 23
  • 28
  • I may misunderstand your answer, but your first snippet is exactly what `@RequiredArgsConstructor(onConstructor = @__(@Autowired))` produces. – Jan Rieke Jan 18 '19 at 10:05
  • Yes, sorry I didn't see it was a Lombok annotation. So what I'm saying is you don't need the autowired, I'll update the answer – Matt Jan 18 '19 at 11:35
0

This looks like its fixed in the 6.3 version of SonarJava here:

https://jira.sonarsource.com/browse/SONARJAVA-3330