• 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

How do I access beans from Spring container if I use @ComponentScan

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far, I have used XML file for component-scan of my entire application. Then, to access bean(s) from Spring Container, I would do something like:

But then I learned that I can have class annotated with @ComponentScan that serves the same purpose.

So, instead of writing this in XML file:
, I can do this instead:


But I am stuck now. How do I access beans from Spring Container now? Because component-scan from XML is replaced with @ComponentScan, meaning I can't anymore do:


 
Saloon Keeper
Posts: 14688
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main method is not the proper place to get beans from the application context. It's only there to set up the application context.

When a request hits your application, Spring will create controllers and inject beans automatically. You just need to annotate your classes with annotations that indicate that those instances are also managed by Spring. Examples include @Component, @Controller, etc.
 
Stefan Jankovic
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I know how to do that. But for the sake of this example, how do I test those beans using main() method?

I am following one book and I would like to do everything from scratch without making actual requests to my app.
 
Saloon Keeper
Posts: 26544
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If what you want to do is test beans, use a test framework. Such as Junit.

JUnit is an external testing system. It does not require modifications to application code to use - and modifications can alter the reliability and security of the application, so it's better to avoid them. You don't need a Main() method, because the Main() for JUnit testing is within JUnit itself.

If you're using a build system such as Maven, the build system can run unit testing as part of the build process and in many cases even generate reports. Many of the "big ticket" open-source projects are built and tested by Maven.
 
Ranch Hand
Posts: 61
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use another implementation of ApplicationContext - AnnotationConfigApplicationContext. So write just like that:



@ComponentScan means IoC container(your context) finds and creates all the beans itself. So if you annotate your classes as @Component, then your configuration class scans all these annotated classes and creates beans.
reply
    Bookmark Topic Watch Topic
  • New Topic