Table of Contents
Tutorial Content: Spring tutorial for beginners
- Introduction to spring framework
- Spring interview questions
- Dependency injection(ioc) in spring
- Spring XML based configuration example
- Spring java based configuaration
- Dependency injection via setter method in spring
- Dependency injection via constructor in spring
- Spring Bean scopes with examples
- Initializing collections in spring
- Beans Autowiring in spring
- Inheritance in Spring
- Spring ApplicationContext
- Spring lifetime callbacks
- BeanPostProcessors in Spring
- Annotation based Configuration in spring
- Spring AOP tutorial
Spring supports JSR 250 Annotations such as @PostConstruct, @PreDestroy and @Resource annotations.Lets discuss each in brief.
@Resource:
@Resource
annotation on fields or bean property setter methods. This is a common pattern found in Java EE 5 and Java 6 which Spring supports for Spring-managed objects as well.Example:
For configuring spring in your eclipse ide please refer hello world example
1.Country.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.javapostsforlearning; import org.springframework.beans.factory.annotation.Autowired; public class Country { String countryName; @Resource(name="capitalA") Capital capital; public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public Capital getCapital() { return capital; } } |
2.Capital.java
Create Capital.java under package org.arpit.javapostsforlearning.java.Above Country class contains object of this class.Copy following content into Capital.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.javapostsforlearning; public class Capital { String capitalName; public String getCapitalName() { return capitalName; } public void setCapitalName(String capitalName) { this.capitalName = capitalName; } } |
3.QualifierAnnotationInSpringMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.javapostsforlearning; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class QualifierAnnotationInSpringMain{ public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("CountryBean"); String countryName=countryObj.getCountryName(); Capital capital=countryObj.getCapital(); String capitalName=capital.getCapitalName(); System.out.println(capitalName+" is capital of "+countryName); } } |
4.ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <bean id="CountryBean" class="org.arpit.javapostsforlearning.Country"> <property name="countryName" value="India" /> </bean> <bean id="capitalA" class="org.arpit.javapostsforlearning.Capital"> <property name="capitalNamehttp://www.blogger.com/blogger.g?blogID=1114068605611316469#editor/target=post;postID=2213204035101507334" value="Delhi" /> </bean> <bean id="capitalB" class="org.arpit.javapostsforlearning.Capital"> <property name="capitalName" value="Mumbai" /> </bean> </beans> |
As you can note here we are having two beans of same type.In Country.java we have used @Resource(name-“capitalA”) it means we want to autowire capital property of country with bean id=”capitalA” in XML configuration file.
5.Run it:
When you will run above application,you will get following as output.
1 2 3 |
Delhi is capital of India |
@PostContruct and @PreDestroy:
Example:
For configuring spring in your eclipse ide please refer hello world example
1.Country.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.javapostsforlearning; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Country { String countryName ; public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } @PostConstruct public void init() { System.out.println("In init block of country"); } @PreDestroy public void destroy() { System.out.println("In destroy block of country"); } } |
2.LifetimeCallbacksMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.javapostsforlearning; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class LifetimeCallbacksMain{ public static void main(String[] args) { AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("country"); System.out.println("Country Name: "+countryObj.getCountryName()); appContext.registerShutdownHook(); } } |
3.ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="country" class="org.arpit.javapostsforlearning.Country" init-method="init" destroy-method="destroy"> <property name="countryName" value="India"/> </bean> </beans> |
4.Run it
1 2 3 4 5 |
In init block of country Country Name: India In destroy block of country |