What's happening in the background?

Understanding what happens in the background will help us to debug problems faster when they arise. We were able to successfully launch a Spring application in the previous section. In this section, let's focus on understanding what's happening in the background. Let's look at some of the debug logs to get a better understanding.

The following are some of the important statements from the log once the context is launched using LaunchJavaContext

The first few lines show the component scan in action:

Looking for matching resources in directory tree [/target/classes/com/mastering/spring]

Identified candidate component class: file [/in28Minutes/Workspaces/SpringTutorial/mastering-spring-example-1/target/classes/com/mastering/spring/business/BusinessServiceImpl.class]

Identified candidate component class: file [/in28Minutes/Workspaces/SpringTutorial/mastering-spring-example-1/target/classes/com/mastering/spring/data/DataServiceImpl.class]

defining beans [******OTHERS*****,businessServiceImpl,dataServiceImpl];

Spring now starts to create the beans. It starts with businessServiceImpl, but it has an autowired dependency:

Creating instance of bean 'businessServiceImpl'Registered injected element on class [com.mastering.spring.business.BusinessServiceImpl]: AutowiredFieldElement for private com.mastering.spring.data.DataService com.mastering.spring.business.BusinessServiceImpl.dataService 

Processing injected element of bean 'businessServiceImpl': AutowiredFieldElement for private com.mastering.spring.data.DataService com.mastering.spring.business.BusinessServiceImpl.dataService

Spring moves on to dataServiceImpl and creates an instance for it:

Creating instance of bean 'dataServiceImpl'
Finished creating instance of bean 'dataServiceImpl'

Spring autowires dataServiceImpl into businessServiceImpl:

Autowiring by type from bean name 'businessServiceImpl' to bean named 'dataServiceImpl'
Finished creating instance of bean 'businessServiceImpl'

In summary, the following steps are performed at the launch of a Spring application:

  • A component scan is performed to identify the beans and dependencies.
  • Beans are created and dependencies are wired in as needed.
We recommend you create an example of your own using DI. Think of a class that is dependent on another class, for example, business logic needing a sorting algorithm. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset