• 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
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

@ComponentScan with arguments

 
Greenhorn
Posts: 22
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • 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: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to our Spring forum.
 
Saloon Keeper
Posts: 12499
269
  • Mark post as helpful
  • send pies
  • 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: 22
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • 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: 12499
269
  • Mark post as helpful
  • send pies
  • 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: 22802
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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: 1979
13
  • Mark post as helpful
  • send pies
  • 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: 22042
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic