I did my first steps with a Spring Boot application and now, I need to extend the bootstrapped start application for my individual needs. So I need to understand a little bit more about the inside of a Spring application in order to make changes / extensions – I would like to take some notes about that here!

Wiring beans

My first finding:

“In Spring, objects aren’t responsible for finding or creating the other objects that they need to do their jobs. Instead, the container gives them references to the objects that they collaborate with.” (source: book: “Spring in action” by Craig Walls)

This procedure is called wiring. Maybe some words why this such an important aspect of dependency injection in Spring. A nice description is given by vogella.com:

To decouple Java components from other Java components the dependency to a certain other class should get injected into them rather that the class itself creates / finds this object.

This means, the Spring framework is taking care of the availability of the needed object. They don’t need to be created manually in the source code by the developer (which would lead to a hard dependency which should be avoided by dependency injection).

A common use-case for wiring is the bean discovery and automatic wiring:

  • Bean discovery via Component scanning:
    • flag the class for which Spring should create a bean with @Component. 
    • give your application one class (e.g. the Application-class with the run-method) the @ComponentScan-annotation.
      mark here: The @ComponentScan-annotation only looks for classes with @Component-annotiation in its own package. If you want to use other packages, you can manually specify them. Choose the basePackages-option.
  • Autowiring:
    • Attributes or methods can be annotated with @Autowired – Spring will then automatically create the dependent bean within the Application Context.
    • During build time, Spring will check the dependencies and raise an Exception if no suiting Bean can be served.
    • There is an option to make the attribute optional, realized via @Autowired(required=false) which deactivates this check – but this can lead to NullPointerExceptions during runtime, so use it wisely.

Unfortunately, automatic wiring can only be used if the source code of the dependent class is either already annotated properly or the source code is changeable to add the annotation.
If that’s not possible, you need to configure the dependency explicitly – either via Java or via XML. But this won’t be part of this article, I hope I won’t need it in my simple project 🙂

 

Side mark: By default, all beans in Spring are Singletons! (isn’t linked to the article directly, but imoprtant to know!)

 

</article>

featured photo by Juan Di Nella on Unsplash

my source list:

One thought on “Wiring in Spring

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s