Spring Core - Durgesh






Spring Core




> Spring is Container for beans.
    Beans : is a class with some private properties and setters and getters.




Spring helps in the creation of loosely coupled applications because of Dependency Injection.

Inversion of control- It means giving the control of creating and instantiating the spring beans to the Spring IOC container and the only work the developer does is configuring the beans in the spring xml file.








Spring Core and Spring bean provide fundamental part of framework i.e IOC and dependency Injection.


> Spring IOC container : It is provided by Spring framework, manages the lifecycle of a object.
  • Create the object
  • Hold it in a memory
  • Inject it in another object whenever required
only we need to provide it the beans and the configuration of dependency to it.


> ApplicationContext : is an interface It represents out IOC container, we can also use bean factory but ApplicationContext extends BeanFactory  so it has all the features of it and addition feature of its own.

these are the implementations of ApplicationContext













> Types of dependencies Application Context can Inject:




>  Software needed and topics to be covered below : 


> So in eclipse we will create Maven project.

> Then add all the required dependencies for our maven project in pom.xml file
here we have added spring core and spring context dependency


here create student bean.



above image is from documentation to instantiate bean.

here we instantiated object for Student bean and provided value for its object.
here the value is injected in object using setter injection.

here fetching bean object in our main using ApplicationContext interface, and passing name of bean class as parameter of ClassPathApplicationContext class here we can pass multiple xml file as String attribute.

> In config file we can set value by 2 more ways:
  • value as attribute
  • using p schema

value as attribute
we here don't need to provide value tag we are passing object value as attribute ,here the value is injected in object using setter injection.


using p schema
here using p schema bean object is instantiated in single line i.e we will close each bean tag in the end <bean           ...............all variables......................           />


So these are the 3 ways to instantiate bean object




> Similary we can instantiate and inject bean object for Collection Types:







> Below is example for collection type





> For reference type  

Class A

Class B


Now here we are injecting bean B object to A class object:
creating bean for A and B classes to instantiate and inject bean object.
other way is by ref as attribute same as value in A as given below

3rd way is by using p schema -> most used 
here we have assigned value to both A and B type in single line


This is the main class having path of config file



> Constructor Injection

Class Person


Another class certi




here we are using constructor-arg tag for taking arguments of constructor of Person class.
here we have declared one more c schema same as p schema for constructor.
we have all the 3 ways for creating bean here also as told above i.e p schema ,value as attribute and by simply using tags

Here in constructor argument we can get ambiguity problem so for removing it we should have specified type attribute for the type of attributes coz by default it takes string attribute , so if we have more then one Constructor then it will cause error.  



> What are the ambiguity problem in constructor injection ?


for this case as we have (String ,String) argument for constructor so getBean() will take Addition Constructor for String one only because by default the argument values are String.

But if we don't have (String , String) constructor then it will start from se 1 constructor and take the constructor on the top i.e having same number of argument.
So in this case the constructor that will be called is double one since it is on the top with 2 parameters.



> So , this ambiguity in selection of constructor can be removed by defining the type of argument in config xml file.
 

Similarly we can give index of position for constructor argument in config file .



> Lifecycle methods of  Spring Bean : 





these are the ways by which we can configure our init() and destroy()



> Configuring using XML Implementation


we can give init and destroy method any name Since in XML file we provide it in attribute but for simplicity we will keep init and destroy only


i.e here we have provided the init and destroy method name in attribute




here we have taken reference of AbstractApplicationContext because it is having method registerShutdownHook() which is used to destroy bean object .


o/p for above code



> Configuring using Spring Interface




these interfaces have methods for init and destroy



here we have no need of mentioning attribute for init and destroy method



> Configuring using annotation






here in beans we have also added annotation config for allowing/enabling our code to allow all annotations in our code.
And further bean and property defined as shown above.



> Autowiring in Spring : for automatically injecting dependency


can be done by two types XML and annotation



> Implementing XML :

  • byName : here address of dependency (Address) bean having same name as object in dependent (Employee) so it will inject address beans value in Employee class.
here address of dependency (Address) bean having same name as object in dependent (Employee) so it will inject address beans value in Employee class.


Employee class (dependent class)


Address class(dependency class)



  • byType :  here it searches by type of dependency type class in the config file if it matches it injects the bean else gives null. Also here if we have more then one object of same type then compiler throws exception because it get confused which object to take.
so here Address type bean is created so in Employee class it will injects its object.


Else every thing will be same. o/p will come for single object

but the error will come if we have more then one bean in config file with same type.
since here both are of same type so exception while injecting


  • constructor : here will inject dependency in constructor else in both the above it was injecting using setter method.
here autowire = "constructor" is written

here also as the dependency bean name matched parameter of constructor so it will inject address value in constructor.
else everything will remain same.



> Implementation using Annotation : It searches bean by its type and injects it .
  • on property/object
  • on setter
  • on constructor

here in config file we have bean for both the classes and also added annotation-config for allowing annotations in our code


  • on property/object


on setter

on constructor





> Qualifier annotation ( @Qualifier ) : is used along with @ Autowired to search bean by its name for given Type.


here it will inject temp2 named bean from Address class.



> Standalone Collections: here we can create collections only once in config class and then use it wherever we want in using its id and also one more benefit is that here we can specify actual type for collection i.e in list - ArrayList ,Vector, LinkedList . and so on.....


here firstly inserted schema properties util.
then created list outside bean of Vector type and gave it ID ,
So now we can use it further in our config class

Similarly for map and properties



Person Class




___________________________________________________________________________________






Comments

Popular posts from this blog

Durgesh - Exam Portal Project using Spring Boot and Angular