In the spring "Singleton" is using as default scope which means, there is an object created by spring and it uses same object for every operation in the same configuration. Also, "Prototype" is using in order to create new object for every operation in the same configuration.
This is Singleton, there are 2 objects there and they are in the location in memory which means that they are the same object.
If scope changes using "scope";
They become different objects in the different memory allocation.
Spring has own lifecycle, which means objects are created, used and destroyed. We can implement methods during the create and destroy using by "init-method" for create and "destroy-method" for destory.
Here, simply created 2 method to check init and destroy method in TrackCoach class.
Add to method in configuration file using "init-method" and "destroy-method".
First init-method displayed then object created, finally destroy method implemented, this is the lifecycle of Spring
Spring has lots of annotation that helps to coding easily. For example; instead of create beans in configuration file, simply scan "Component" in base package and it takes these classes/constructors/setter methods/fields as a bean and directly retrieve data from spring container.
This line for scanning component annotation in given package.
Spring create id here from class name like; class -> HappyFortuneService ; id -> happyFortuneService. If you want to add manually id, you can directly implement like @Component("idName").
We can also implement Dependency Injections using annotations. Constructor Injection, Setter Injection, Field Injection are the different types of dependency injections that we can implement with annotations. The keyword to implement injection is "@Autowired". !!! You can also have different attributes to implement but if you write @Component for all of them. Application will crush so, "@Qualifier" keyword decide which implementation is comes first.
In this example; DatabaseFortuneService,HappyFortuneService,RandomFortuneService,RESTFortuneService has @Component keyword so Compailor do not decide which is the first. So in FortuneService there is @Qualifier keyword to solve this problem.
It is easy to create xml file and retrieve bean from spring container. However, there is another way to manage this property which is the implementation without XML file. Configuration can create by using Annotation and Java Classes.
Using "Configuration" annotation, class can act as configuration file, and It can contain beans with "Bean" annotation for retrieving data from them. !!!(If you want to work with whole package, you should scan all components with "ComponentScan" annotation.
When XML file implement in main class, ClassPathXmlApplicationContext class is using. But in this implementation, instead of ClassPathXmlApplicationContext, AnnotationConfigApplicationContext is using.