Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

@ComponentScan with arguments

 
Greenhorn
Posts: 28
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: 71699
312
  • 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: 12616
273
  • 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
Greenhorn
Posts: 28
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: 12616
273
  • 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: 23011
156
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: 2008
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: 22055
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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