• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

@ComponentScan with arguments

 
Ranch Hand
Posts: 39
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I just added spring to my app and Im not sure how to use @ComponentScan proper. I know that if I will leave it no-args it will just scan current package and sub-packages. I can add args-and pick specific package but how it will ends if I would use @ComponentScan("java") like this :


It will scan my whole app? Or it wont scan anything?
Thanks in advance for an answer!
 
Marshal
Posts: 77208
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to our Spring forum.
 
Saloon Keeper
Posts: 14687
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What this will do is register all beans in the "java" package and sub-packages and make them available for you to inject in your application.

I hypothesize that it will make your application a lot slower and it will also be completely pointless because there are no services in the packages I mentioned that use annotations to inject the discovered beans.

Why would you want to do this?
 
Jan Kaczmarek
Ranch Hand
Posts: 39
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What this will do is register all beans in the "java" package and sub-packages and make them available for you to inject in your application.

I hypothesize that it will make your application a lot slower and it will also be completely pointless because there are no services in the packages I mentioned that use annotations to inject the discovered beans.

Why would you want to do this?



Im looking for good place to put this Config class with @ComponentScan annotation. So my solution is to move all packages (service, data, model, presentation) to new, big package like "student-management-system" and put @ComponentScan("student-management-system") ? Because I do not see any other solution for the scan to work.
 
Stephan van Hulst
Saloon Keeper
Posts: 14687
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, that's better than using java as your base package.

But still, why don't you just put your Config class in the student-management-system package?
 
Saloon Keeper
Posts: 26541
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole purpose of ComponentScan is to limit what packages are scanned for the presence of eligible components rather than simply enumerating - at immense cost - every class in the application classpath. You should always strive to limit components to narrow sets of package trees so that the application will configure faster and waste fewer computing resources.
 
Bartender
Posts: 2265
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Jan,
Is your Runner class annotated with @SpringBootApplication?
Since your Runner class is at the root directory and I assume it is annotated with @SpringBootApplication, @ComponentScan can be omitted.

@SpringBootApplication has meta-annotated with @ComponentScan.
It will scan the current root directory and all sub packages.
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:But still, why don't you just put your Config class in the student-management-system package?


Because package names cannot contain dashes
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic