Spring Installation and setup ( Hands on using Eclipse IDE )
1.Create java project in eclipse.
2.Download the latest release jar files Zip(Latest spring-framework-4.2.3.RELEASE) from below link
http://repo.spring.io/release/org/springframework/spring/
2.Download the latest release jar files Zip(Latest spring-framework-4.2.3.RELEASE) from below link
http://repo.spring.io/release/org/springframework/spring/
3.If you want to get the jar by using Maven or Gradle use the below link
http://projects.spring.io/spring-framework
http://projects.spring.io/spring-framework
4.Unzip the Downloaded file, go to the libs folder, you will get all the necessary jars.
5.Create custom spring jar library in you Spring project and include all downloaded jars.
Right click on project-->Properties-->Java Build Path-->Add Library-->User Library-->Next-->User Libraries-->4
New-->User library name-->Write your Custom Spring Library(Spring Jars Library)-->Ok-->Now you get Spring Jars Library
-->Click on Spring Jars Library-->Add External JARs-->add all Downloaded jars.
6.For spring we also need commons-loggings-1.1.3.jars
you can download from https://commons.apache.org/proper/commons-logging/download_logging.cgi
5.Create custom spring jar library in you Spring project and include all downloaded jars.
Right click on project-->Properties-->Java Build Path-->Add Library-->User Library-->Next-->User Libraries-->4
New-->User library name-->Write your Custom Spring Library(Spring Jars Library)-->Ok-->Now you get Spring Jars Library
-->Click on Spring Jars Library-->Add External JARs-->add all Downloaded jars.
6.For spring we also need commons-loggings-1.1.3.jars
you can download from https://commons.apache.org/proper/commons-logging/download_logging.cgi
Spring- creating first Spring application
Steps to create our First Spring Application:
1.Create a Java class -Restaurant .java
1.Create a Java class -Restaurant .java
2.Create a spring configuration file - SpringConfig.xml
3.Write a test class TestRestaurant .java
3.Write a test class TestRestaurant .java
1.Restaurant .java
package com.om.springcore;
/**
* Spring bean
*/
public class Restaurant {
public void greetCustomer() {
System.out.println("welcome to our restaurant!!");
}
}
/**
* Spring bean
*/
public class Restaurant {
public void greetCustomer() {
System.out.println("welcome to our restaurant!!");
}
}
2. SpringConfig.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="restaurantBean" class="com.gontuseries.springcore.Restaurant">
</bean>
</beans>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="restaurantBean" class="com.gontuseries.springcore.Restaurant">
</bean>
</beans>
3.TestRestaurant .java
package com.om.springcore;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringProject {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
Restaurant restaurantObj = (Restaurant) context.getBean("restaurantBean");
restaurantObj.greetCustomer();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringProject {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
Restaurant restaurantObj = (Restaurant) context.getBean("restaurantBean");
restaurantObj.greetCustomer();
}
}
O/P:-welcome to our restaurant!!
Spring- creating Second Spring application
1.Welcome.java
package com.om;
public class Welcome {
String welcome;
public void setWelcome(String welcome) {
this.welcome = welcome;
}
public void getWelcome(){
System. out.println(welcome );
}
}
2. SpringConfig.xml
<beans xmlns=............ >
<bean id="welcome" class="com.om.Welcome" >
<property name="welcome" value="Welcome to Spring world!!"></ property>
</bean >
</beans>
3.WelcomeTest.java
package com.om;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WelcomeTest {
private static ApplicationContext context;
public static void main(String args[]){
context = new ClassPathXmlApplicationContext("SpringConfig.xml" );
Welcome welcome=(Welcome)context.getBean("welcome");
welcome.getWelcome();
}
}
O/P:-Welcome to Spring world!!
Spring - Dependency Injection using constructor way ( Hands on using Eclipse IDE )
1.Create a Restaurant class.
2.Create a Tea class.
3.Create an interface-IHotDrink
2.Create a Tea class.
3.Create an interface-IHotDrink
1.IHotDrink.java
package com.om;
public interface IHotDrink {
public void prepareHotDrink();
}
2.Tea.java
package com.om;
public class Tea implements IHotDrink {
@Override
public void prepareHotDrink() {
System. out.println("Dear customer, we are preparing tea for you!!");
}
}
3.Restaurant.java
package com.om;
public class Restaurant {
IHotDrink iHotDrink;
Restaurant(IHotDrink iHotDrink){
this. iHotDrink=iHotDrink ;
}
public void preHotDrink(){
iHotDrink.prepareHotDrink();
}
}
4.SpringConfig.xml
<beans xmlns=......... >
<bean id="restaurantBean" class="com.om.Restaurant" >
<constructor-arg ref="teaBean"></ constructor-arg>
</bean >
<bean id="teaBean" class="com.om.Tea" >
</bean >
</beans>
5.TestRestaurant.java
package com.om;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestRestaurant {
public static void main(String args[]){
ApplicationContext context=new ClassPathXmlApplicationContext("SpringConfig.xml" );
Restaurant restaurantObj =(Restaurant)context .getBean("restaurantBean");
restaurantObj.preHotDrink();
}
}
O/P:-Dear customer, we are preparing tea for you!!
Spring- Dependency Injection using setter method way ( Hands on using Eclipse IDE )
1.IHotDrink.java
package com.om;
public interface IHotDrink {
public void prepareHotDrink();
}
2.Tea.java
package com.om;
public class Tea implements IHotDrink {
@Override
public void prepareHotDrink() {
System. out.println("Dear customer, we are preparing tea for you!!");
}
}
3.Restaurant.java
package com.om;
public class Restaurant {
IHotDrink iHotDrink;
public void setiHotDrink(IHotDrink iHotDrink ) {
this.iHotDrink = iHotDrink;
}
public void preHotDrink(){
iHotDrink.prepareHotDrink();
}
}
4.SpringConfig.xml
<beans xmlns=......... >
<bean id="restaurantBean" class="com.om.Restaurant" >
<bean id="restaurantBean" class="com.om.Restaurant">
<property name="iHotDrink" ref="teaBean" ></property>
</bean >
<bean id="teaBean" class="com.om.Tea" >
</bean >
</beans>
5.TestRestaurant.java
package com.om;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestRestaurant {
public static void main(String args[]){
ApplicationContext context=new ClassPathXmlApplicationContext("SpringConfig.xml" );
Restaurant restaurantObj =(Restaurant)context .getBean("restaurantBean");
restaurantObj.preHotDrink();
}
}
O/P:-Dear customer, we are preparing tea for you!!
Spring - Dependency Injection with a Collection property ( Hands on using Eclipse IDE )
1.Restaurant.java
package com.om;
import java.util.List;
public class Restaurant {
List restaruantWaitersList ;
public void setRestaruantWaitersList(List restaruantWaitersList ) {
this.restaruantWaitersList = restaruantWaitersList;
}
public void displayWaitersNames() {
System. out.println("All waiters working in Restaruant : " + restaruantWaitersList );
}
}
2.SpringConfig.xml
<beans xmlns=....... >
<bean id="restaurantBean" class="com.om.Restaurant" >
<property name="restaruantWaitersList" >
<list >
<value >ALOK</ value>
<value >SANGITA</ value>
<value >ASHOK</ value>
<value >KUSHUM</ value>
</list >
</property >
</bean >
</beans>
3.TestRestaurant.java
package com.om;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestRestaurant {
public static void main(String args[]){
ApplicationContext context=new ClassPathXmlApplicationContext("SpringConfig.xml" );
Restaurant restaurantObj =(Restaurant)context.getBean("restaurantBean" );
restaurantObj.displayWaitersNames();
}
}
O/P:-All waiters working in Restaruant : [ALOK, SANGITA, ASHOK, KUSHUM]
Name some of the important Spring Modules?
Some of the important Spring Framework modules are:
- Spring Context – for dependency injection.
- Spring AOP – for aspect oriented programming.
- Spring DAO – for database operations using DAO pattern
- Spring JDBC – for JDBC and DataSource support.
- Spring ORM – for ORM tools support such as Hibernate
- Spring Web Module – for creating web applications.
- Spring MVC – Model-View-Controller implementation for creating web applications, web services etc.
What is IOC and DI?
IOC (Inversion of Control) and DI (Dependency Injection) is a design pattern to provide loose coupling. It removes the dependency from the program.
Let's write a code without following IOC and DI.
Now, there is dependency between Employee and Address because Employee is forced to use the same address instance.
Let's write the IOC or DI code.
Now, there is no dependency between Employee and Address because Employee is not forced to use the same address instance. It can use any address instance.
What are the types of IOC container in spring?
There are two types of IOC containers in spring framework.
- BeanFactory
- ApplicationContext
What is the difference between BeanFactory and ApplicationContext?
BeanFactory is the basic container whereas ApplicationContext is the advanced container. ApplicationContext extends the BeanFactory interface. ApplicationContext provides more facilities than BeanFactory such as integration with spring AOP, message resource handling for i18n etc.
What are different scopes of Spring Bean?
There are five scopes defined for Spring Beans.
- singleton: Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure spring bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues because it’s not thread-safe.
- prototype: A new instance will be created every time the bean is requested.
- request: This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.
- session: A new bean will be created for each HTTP session by the container.
- global-session: This is used to create global session beans for Portlet applications.
Spring Framework is extendable and we can create our own scopes too, however most of the times we are good with the scopes provided by the framework.
To set spring bean scopes we can use “scope” attribute in bean element or @Scope annotation for annotation based configurations.
What is autowiring in spring? What are the autowiring modes?
Autowiring enables the programmer to inject the bean automatically. We don't need to write explicit injection logic.
Let's see the code to inject bean using dependency injection.
The autowiring modes are given below:
No. | Mode | Description |
---|---|---|
1) | no | this is the default mode, it means autowiring is not enabled. |
2) | byName | injects the bean based on the property name. It uses setter method. |
3) | byType | injects the bean based on the property type. It uses setter method. |
4) | constructor | It injects the bean using constructor |
The "autodetect" mode is deprecated since spring 3.
No comments:
Post a Comment