What is the difference between Spring and Struts?
1. Framework Difference -
Spring -
Its application framework in which spring MVC is one of the modules of Spring Framework.
Spring can be used for all layer implementation unlike Struts (only front end related) and Hibernate (only database related).
Note**-
Application framework means Spring can be used for any Java application development such as
1.Desktop application
2. Enterprise application (using EJB)
3.Mobile application
4.Web application.
5.Games
Struts -
Its web application framework.
Struts Framework is designed for development of only web applications.
2. Design Pattern Difference -
Spring -
Spring implements IOC Design Pattern.
Spring -
Struts implements MVC Design Pattern.
3.Invasion Difference -
Spring -
Spring is non-invasive , means Spring does not force us to extend or implement default Spring API Classes or Interfaces.
Struts -
Struts is invasive ,means Struts force us to extends or implement Struts API Base Classes or Interfaces such as Action.
4. Integration
Spring -
Spring allows easy integration of JDBC ,ORM.
We do not need to write manual code for this.It is readily available in Spring just need configure it.
Struts -
Struts does not allow easy integration of JDBC, ORM.
We have to write code for this manually.
5. API Weight Difference -
Spring -
It is light weight framework.
Minimum required module is Spring core.
Other modules such as AOP, MVC are need based and can be added when needed.
Struts -
It is heavy weight framework.
All Spring API is needed to be added before developing Spring application.
6.Coupling Difference-
Spring -
Spring is loose coupled because of Dependency Injection.
Struts -
It is tightly coupled because of lack of Dependency Injection.
7. Inbuilt Middle-ware -
Spring -
Spring provides in built middle-ware services such as Transaction,Logging and Connection Pooling.
Struts -
It does not provide in built middle-ware.
Reference link --->
http://myjourneyonjava.blogspot.in/2014/02/difference-between-spring-and-struts.html
Modules in Spring -
There are main 6 modules -
1.Spring Core - Mandatory to use
2.Spring Context - J2EE
3.Spring JDBC - JDBC
4.Spring ORM -
5.Spring AOP - Aspect Oriented Programming
6.Spring Web-MVC -for web app
1.Spring Core -
It's the Heart of Spring.
Tight coupling and Loose coupling of java objects is the main concept of of Spring core.
Eg.
public class Traveller {
Car c = new Car(); //Tight coupled.(when change Car object ,we have to change every object of car)
public startJourney(){
c.move();
}
}
Above problem is resolved by loose coupling which uses Dependency Injection to achieve it.
This is achieved by using Interface.
Interface Vehicle {
void move();
}
class Car Implements Vehicle {
public void move(){
//code
}
}
class Bike Implements Vehicle {
public void move(){
//code
}
}
class Traveler{
Vehicle v;
public void setV(Vehicle v){ // Car or Bike is injected by calling SetV() method.
this.v =v; //so,when object is replaced we do not need to change in Traveller class.
}
public void startJourney(){
v.move();
}
}
In short, DI is a mechanism,which Object is configured with fields by external entity. (not by Dependent class but by Spring IOC container.)
one more eg.
here, myDao needs to instantiate DataSourceImpl to get DataSource. Hence it is dependent on both
DataSourceImpl class and DataSource interface.
Also 4 strings passed as parameter also hard coded.
This arrangement is very tightly coupled.
Suppose,
We have to change the database, in this case we have to change DataSource in every class ,which is implemented this way.This is bad practice.
Solution is -
Use constructor to instantiate dataSource.
Though myDao depends on dataSource and DataSourceImpl ,it does not need to instantiate them itself.
The class whichever is going to use myDao instance will initiate it using required parameter.
more better solution is -
DI Types -
1.Setter methods (Traveller eg 1)
2.Constructor (both above eg)
3.Interface (both above eg)
Note-** In Spring each java class is called as Spring Beans.
How to create Basic Spring Project ?
Spring has jar file for each module.
Or Spring.jar contains all modules.It depends on commons-logging.jar
Spring does not provide commons-logging.jar we have to download it .
In short, we need -
1.Spring.jar
2.commons-logging.jar
1. Spring environment starts by loading Spring config xml file in Resource interface object.
Resource res = new ClassPathResource(config.xml);
2. Now Spring IOC Container object is created after reading xml file by Resource object.
IOC container is called as BeanFactory becuase it creates bean objects (java classes) and injects whenever asked.
BeanFactory factory = new XmlBeanFactory(res);
3. Now we can get required bean object from IOC Container calling getBean().
Object ob = factory.getBean(“bean1″);
FirstBean fb = (FirstBean)ob;
Hello Spring Program -
1.Welcome Bean (Welcome Class)
2. Main Class To Test Spring
The above is example of setter method for primitive Spring (DI Type 1)
Here. String message is injected on calling from main() from Main class.
http://www.java4s.com/spring/setter-injection-with-objects-spring-dependency-in-the-form-of-objects/
1. Framework Difference -
Spring -
Its application framework in which spring MVC is one of the modules of Spring Framework.
Spring can be used for all layer implementation unlike Struts (only front end related) and Hibernate (only database related).
Note**-
Application framework means Spring can be used for any Java application development such as
1.Desktop application
2. Enterprise application (using EJB)
3.Mobile application
4.Web application.
5.Games
Struts -
Its web application framework.
Struts Framework is designed for development of only web applications.
2. Design Pattern Difference -
Spring -
Spring implements IOC Design Pattern.
Spring -
Struts implements MVC Design Pattern.
3.Invasion Difference -
Spring -
Spring is non-invasive , means Spring does not force us to extend or implement default Spring API Classes or Interfaces.
Struts -
Struts is invasive ,means Struts force us to extends or implement Struts API Base Classes or Interfaces such as Action.
4. Integration
Spring -
Spring allows easy integration of JDBC ,ORM.
We do not need to write manual code for this.It is readily available in Spring just need configure it.
Struts -
Struts does not allow easy integration of JDBC, ORM.
We have to write code for this manually.
5. API Weight Difference -
Spring -
It is light weight framework.
Minimum required module is Spring core.
Other modules such as AOP, MVC are need based and can be added when needed.
Struts -
It is heavy weight framework.
All Spring API is needed to be added before developing Spring application.
6.Coupling Difference-
Spring -
Spring is loose coupled because of Dependency Injection.
Struts -
It is tightly coupled because of lack of Dependency Injection.
7. Inbuilt Middle-ware -
Spring -
Spring provides in built middle-ware services such as Transaction,Logging and Connection Pooling.
Struts -
It does not provide in built middle-ware.
Reference link --->
http://myjourneyonjava.blogspot.in/2014/02/difference-between-spring-and-struts.html
Modules in Spring -
There are main 6 modules -
1.Spring Core - Mandatory to use
2.Spring Context - J2EE
3.Spring JDBC - JDBC
4.Spring ORM -
5.Spring AOP - Aspect Oriented Programming
6.Spring Web-MVC -for web app
1.Spring Core -
It's the Heart of Spring.
Tight coupling and Loose coupling of java objects is the main concept of of Spring core.
Eg.
public class Traveller {
Car c = new Car(); //Tight coupled.(when change Car object ,we have to change every object of car)
public startJourney(){
c.move();
}
}
Above problem is resolved by loose coupling which uses Dependency Injection to achieve it.
This is achieved by using Interface.
Interface Vehicle {
void move();
}
class Car Implements Vehicle {
public void move(){
//code
}
}
class Bike Implements Vehicle {
public void move(){
//code
}
}
class Traveler{
Vehicle v;
public void setV(Vehicle v){ // Car or Bike is injected by calling SetV() method.
this.v =v; //so,when object is replaced we do not need to change in Traveller class.
}
public void startJourney(){
v.move();
}
}
In short, DI is a mechanism,which Object is configured with fields by external entity. (not by Dependent class but by Spring IOC container.)
one more eg.
public class MyDao { protected DataSource dataSource = new DataSourceImpl("driver", "url", "user", "password"); //data access methods... public Person readPerson(int primaryKey) {...} }
DataSourceImpl class and DataSource interface.
Also 4 strings passed as parameter also hard coded.
This arrangement is very tightly coupled.
Suppose,
We have to change the database, in this case we have to change DataSource in every class ,which is implemented this way.This is bad practice.
Solution is -
public class MyDao { protected DataSource dataSource = null; public MyDao(String driver, String url, String user, String password){ this.dataSource = new DataSourceImpl(driver, url, user, password); } //data access methods... public Person readPerson(int primaryKey) {...} }
Use constructor to instantiate dataSource.
Though myDao depends on dataSource and DataSourceImpl ,it does not need to instantiate them itself.
The class whichever is going to use myDao instance will initiate it using required parameter.
more better solution is -
public class MyDao { protected DataSource dataSource = null; public MyDao(DataSource dataSource){ this.dataSource = dataSource; } //data access methods... public Person readPerson(int primaryKey) {...} }
DI Types -
1.Setter methods (Traveller eg 1)
2.Constructor (both above eg)
3.Interface (both above eg)
Note-** In Spring each java class is called as Spring Beans.
How to create Basic Spring Project ?
Spring has jar file for each module.
Or Spring.jar contains all modules.It depends on commons-logging.jar
Spring does not provide commons-logging.jar we have to download it .
In short, we need -
1.Spring.jar
2.commons-logging.jar
1. Spring environment starts by loading Spring config xml file in Resource interface object.
Resource res = new ClassPathResource(config.xml);
2. Now Spring IOC Container object is created after reading xml file by Resource object.
IOC container is called as BeanFactory becuase it creates bean objects (java classes) and injects whenever asked.
BeanFactory factory = new XmlBeanFactory(res);
3. Now we can get required bean object from IOC Container calling getBean().
Object ob = factory.getBean(“bean1″);
FirstBean fb = (FirstBean)ob;
Hello Spring Program -
1.Welcome Bean (Welcome Class)
package java4s;
public class WelcomeBean {
private String message;
public void setMessage(String message)
{
this.message = message;
}
public void show()
{
System.out.println(message);
}
}
2. Main Class To Test Spring
package java4s;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class ClientLogic {
public static void main(String[] args)
{
Resource res = new ClassPathResource("spconfig.xml");
BeanFactory factory = new XmlBeanFactory(res);
Object o = factory.getBean("id1");
WelcomeBean wb = (WelcomeBean)o;
wb.show();
}
}
3.Spring Config xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="id1" class="java4s.WelcomeBean">
<property name="message" value="Welcome to spring" />
</bean>
</beans>
|
The above is example of setter method for primitive Spring (DI Type 1)
Here. String message is injected on calling from main() from Main class.
http://www.java4s.com/spring/setter-injection-with-objects-spring-dependency-in-the-form-of-objects/