Features of Spring Framework:
- Transaction Management: A generic abstraction layer for transaction management allows the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues.
- JDBC Exception Handling: The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy
- Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS.
- AOP Framework: Spring is best AOP framework
- MVC Framework: Spring comes with MVC web application framework, built on core Spring functionality.
Spring Architecture
Spring is well-organized architecture consisting of seven modules. Modules in the Spring framework are:
1. Spring AOP
To provide declarative enterprise services, especially as a replacement for EJB declarative services.
To allow users to implement custom aspects, complementing their use of OOP with AOP
2. Spring ORM
The ORM package that is related to the database access, provides integration layers for popular object-relational mapping APIs, including JDO, Hibernate and iBatis.
3. Spring Web
The Spring Web module is part of Spring’s web application development stack, which includes Spring MVC.
4. Spring DAO
The DAO (Data Access Object) support in Spring is primarily for standardizing the data access work using the technologies like JDBC, Hibernate or JDO.
5. Spring Context
This package builds on the beans package to add support for message sources and for the Observer design pattern, and the ability for application objects to obtain resources using a consistent API.
6. Spring Web MVC
This is the Module which provides the MVC implementations for the web applications.
7. Spring Core
This component provides the Dependency Injection features. The BeanFactory provides a factory pattern which separates the dependencies like initialization, creation and access of the objects from your actual program logic
Two way to configure Spring IOC container
a) xml file: The bean configuration is defined in xml file and then instruct the Spring to use the xml file to configure the beans.
b) Annotation: The @configuration spring annotation can also be used to configure the Spring IOC container. Here programmer add the @configuration to the Java class and this class is considered as special configuration class. The @Bean tag is used to define the bean, and the Spring framework executes the method and then register the object returned. By default the name of the method is used as the bean name.
XML Bean-Injection
InjectClass.java
class InjectClass {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return String.format("Name: " , this.name);
}
}
InjectClass is the java class. It has overided the toString() method.
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mybean" class="InjectClass"
p:name="XML Bean-Injection">
</beans>
xmlns:p="http://www.springframework.org/schema/p":-Namespace:"p" that cannot be validated.
<bean id="mybean" class="InjectClass">:- Here "InjectClass" is the name of the bean class which would be referred in the xml file with the id "mybean".
The xml file declares the spring bean "mybean" . The <bean .../> tag is used to declare a bean in the xml file. The xml file is used to configure the spring run-time environment. Spring framework manages the beans in our program.
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String[] args) {
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"context.xml"));
InjectClass demo = (InjectClass) beanFactory.getBean("mybean");
System.out.println(demo);
}
}
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml")): creates an instance of the XmlBeanFactory which is used to read bean definition from an XML document
new ClassPathResource("context.xml"):-Creates a new ClassPathResource for ClassLoader .Here the context.xml is the file which is to be loaded.
No comments:
Post a Comment