Spring Core Module
Spring
core is base module for spring framework.
All the other modules in the spring are developed on the top of core module.
In
simple words Spring core module is providing fundamental concepts of spring,
they are IOC and DI to achieve loosely coupling between Java classes.
Java Application can have multiple classes (service, controller, model, etc..!) and its methods, as part of application execution one class method should talk to another class.
How one
Java class can talk to another Java class?
By inheriting the properties
By creating objects
If we
use above approach then our classes will be tightly coupled,
because
we are creating objects for kotak, axis bank and then calling that class
methods from main class(Bank).
Suppose
If we change methods of kotak class method, we have to do changes in main
class(Bank) too.
Spring framework is used IOC container, Dependency Injection concepts to achieve loosely coupling between Java classes.
IOC container
Dependency
Injection.
IOC Container
IOC
container creates an object, destroying object and manage their complete
lifecycle of Spring bean (Java class).
The
container gets an Instruction what object to instantiate, configuration and
assemble by reading configuration metadata.
This
metadata can be xml file, annotation file or Java-based configuration.
We can start
IOC container in 2 ways.
1) BeanFactory
2) ApplicatioContext
loading
xml file from classpath
Resource
resource = new ClassPathResource("Beans.xml");
We can
load file from external file location also
Resource
resource = new FileSystemResource("D://files/Beans.xml");
1) IOC
container started with lazy loading mechanism
BeanFactory fatory = new
XmlBeanFactory(resource);
2) IOC
container started with eager loading mechanism
ApplicationContext factory=new
ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext
factory directly loading xml file.
Note:
XmlBeanFactory got deprecated in spring framework, so it is not recommended to
use.
All
XmlBeanFactory Functionalities supported by ApplicationContext factory and also
provides additionals features also.
ApplicationContext factory features are
1)
Annotation Based Configuration Support
2) I18N
Applications Support
3) Event Handling Support
Dependency Injection
Injecting dependent class object into target class
object is called as dependency Injection.
We can perform Dependency Injection in 3ways.
1) Setter Injection
2) Constructor Injection
3) Field Injection
Injecting dependent class object into target
class object using
target class setter method then it is called as
"Setter Injection".
Injecting dependent class object into target
class object using
target class constructor then it is called as
"Constructor Injection".
Injecting dependent class object into target
class object using
target class field then it is called as "Field
Injection".
No comments:
Post a Comment