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 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
> 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.
> Types of dependencies Application Context can Inject:
> Then add all the required dependencies for our maven project in pom.xml file
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...................... />
> Similary we can instantiate and inject bean object for Collection Types:
> Below is example for collection type
> For reference type
Class A
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
> Constructor Injection
Class Person
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 .
> 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
> 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
- 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 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
> Qualifier annotation ( @Qualifier ) : is used along with @ Autowired to search bean by its name for given Type.
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
Comments
Post a Comment