Spring — Introduction | Code Factory
3 min readJan 9, 2021
Donate : Link
WordPress Blog : Link
Applications… : Link
Spring Tutorial Index Page: Link
- Spring Developed by Rod Jhonson.
- Initial Name is Interface 21.
- Spring came for alternative for EJBs (Enterprise Java Beans)
- In case of EJB application became heavy weight and tightly coupled, Spring are light weight and loosly coupled.
- For Banking application EJB is sutable but if it is a small scale E-Commerce application and budget is limited then EJB is not sutable. We need to have some free sources and that free sources are Spring.
- Spring are light weight and AWT (Abstract Window Toolkit) are heavy weight.
- While running AWT it will use OS libraries so it is heavy weight. But in case of Spring, Springs are not have any dependencies of OS, It will use JDK’s libraries. Spring are not having any dependencies of Application Server. You no need to have any Application Server. EJBs are having dependencies of Application Server so EJBs are heavy weight.
- Loosly coupled example is like currently you use Airtel number after that you want to change sim provider then you port from Airtel to BSNL. Loosly coupled is possible because of Java Runtime Polymorphism (JRP).
/* Basic info of servlet */interface Servlet {
}abstract class GenericServlet {
}abstract class HttpServlet {
}class HelloServlet implements Servlet {
}
- * To get capabilities of Servlet we must have to implement Servlet inteface or extend servlet classes. But in case of Spring they are not force to implement any interface or any classes. If you require Spring feature, instead of implementing interfaces and extending classes you can take here class reference as a associated reference. By associate them we can get B and C class behaviour, Spring recommending us associate instead of inheritance, they are recommending has-a relationship. Then your class became light weight. Use associate model instead of using extending model your class will became light weight.
interface A {
// lets say this is Spring interface
}class B {
// lets say this is Spring class
}class C {
// lets say this is Spring class
}class D {
// normal class
B b = new B();
C c = new C();
OR
B b;
C c;
}
- In case of association your class not having tightly coupled with other classes. But in case of inheritance principle your class must have dependency of interfaces and classes.
- So to make your application loosly coupled they are using here association rule. In case of associate any other class object here (in class D), then your class call it as POJO class because here we are not extending any other API, if it is a simple class w/o implementing any class then it is POJO classes.
- By using Runtime Polymorphism and Association principles (Has-a relationship) Spring people implement Spring Framework.
- In Inheritance principle we can create objects like A a = new B() because class B implement A or extend A.
- But in Association principle to create A a = new B(), for that we can use reflaction API. In Spring we have container (IOC) support, we maintain XML document, by reading XML document pass argument to POJO classes.
- * In IOC we have 3 containers. These 3 are interfaces.
1. Core (BeanFactory (I) -> XmlBeanFactory ©)
— BeanFactory b = new XmlBeanFactory();
2. J2EE (ApplicationContext (I) -> ConfigurableApplicationContext (I) -> ClasspathXmlApplicationContext ©)
— ApplicationContext a = new ClasspathXmlApplicationContext();
— ConfigurableApplicationContext = new ClasspathXmlApplicationContext();
3. Web (WebApplicationContext (I) -> WebApplicationContextUtil (Factory Class)) (It is in Spring MVC)
— WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); - * What is Container or What Container do?
— Read xml file
— Create instances of xml beans (classes)
— After creating objects it will manage the lifecycle of bean classes
— It will dynamic pass parameters from xml to bean classes - DI (Dependency Injection) is the main asset of IOC Containers.
- To make a Spring application must we need to have a xml file and POJO classes. The main required components for spring application to start a container are below.
— POJO class
— xml file
— Driver class - The main root tag for spring application file is
<beans></beans>
.