Quick to the point introduction to Spring Framework
Chapters
Bean Definition
The definition of bean contains the configuration metadata information which is needed for the container to know:
- How to create a bean
- Bean's lifecycle details
- Bean's dependencies
Spring Configuration Metadata:
To create Spring configuration metadata, this metadata tells Spring what beans “Java classes” you need to be instantiated in Spring container. From the format Spring IoC container is completely decoupled in which this metadata configuration is written. There are three methods to provide configuration metadata to the Spring Container:
- XML based configuration file.
- Annotation-based configuration
- Java-based configuration
XML Configuration:
Example: class FirstBean
package pack1; public class FirstBean { private String cName; public String getName() { return cName; } public void setName(String cName) { this.cName = cName; } @Override public String toString() { return "FirstBean [Cutomer Name: " + cName + "]"; } }
Create class SecondBean:
package pack1; public class SecondBean { private FirstBean fBean; public FirstBean getFirstBean() { return fBean; } public void setFirstBean(FirstBean fBean) { this.fBean = fBean; } @Override public String toString() { return "SecondBean [fBean: " + fBean + "]"; } }
Create the configuration 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="firstBean" class="pack1.FirstBean"> <property name="name" value="Nimrit" /> </bean> <bean id="secondBean" class="pack1.SecondBean"> <property name="fBean" ref="fBean" /> </bean> </beans>
Create class SampleTest:
package pack1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import pack1.FirstBean; import pack1.SecondBean; public class SampleTest { public static void main( String[] args ) { ApplicationContext ctxXML = new ClassPathXmlApplicationContext("Bean.xml"); SecondBean secondBean = (SecondBean) ctxXML.getBean("secondBean"); System.out.println( secondBean); } }
Now when you execute this test you will get below result
Result:
SecondBean [fBean=FirstBean [name=Suman]]
Annotation-Based Configuration:
Example: class FirstBean
- @Component(value=“fBean2″) : this method which enable Spring to find this bean when scanning and instantiate a bean named “fBean2“
- @Value(value=“AnnotationBean”) : this equal to fbean2.setName(“AnnotationBean”);
package pack1; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component(value="fBean2") public class FirstBean { private String cName; public String getName() { return cName; } @Value(value="Suman") public void setName(String cName) { this.name = cName; } @Override public String toString() { return "FirstBean [name=" + cName + "]"; } }
Create class SecondBean:
- @Component(value=“secondBean”) : this method which enable Spring to find this bean when scanning and instantiate a bean named “secondBean”
- @Autowired @Qalifier(“fBean2″): this equal to secondBean.setFirstBean(fbean2)
package pack1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component(value="secondBean") public class SecondBean { private FirstBean fBean; public FirstBean getFirstBean() { return fBean; } @Autowired @Qualifier("fBean2") public void setFirstBean(FirstBean fBean) { this.fBean = fBean; } @Override public String toString() { return "SecondBean [fBean=" + fBean + "]"; } }
Create the configuration xml file as: BeanAnnotation.xml, to enable annotations:
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="pack1"/> <!-- The use of <context:component-scan> implicitly enables the functionality of <context:annotation-config> -->
<bean id="firstBean" class="pack1.FirstBean">
<property name="name" value="Nimrit" />
</bean>
<bean id="secondBean" class="pack1.SecondBean">
<property name="fBean" ref="fBean" />
</bean>
</beans>
Create class SampleTest:
package pack1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import pack1.SecondBean; public class SampleTest { public static void main( String[] args ) { ApplicationContext ctxAnnotation = new ClassPathXmlApplicationContext("BeanAnnotation.xml"); SecondBean sBean = (SecondBean) ctxAnnotation.getBean("sBean"); System.out.println( sBean); } }
When you execute above test, you will get the result as shown below
SecondBean [fBean=FirstBean [name=Nimrit]]
Java-Based Configuration:
Example: class FirstBean
package pack1; import org.springframework.beans.factory.annotation.Value; public class FirstBean { private String cName; public String getName() { return cName; } @Value(value="Nimrit") public void setName(String cName) { this.cName = cName; } @Override public String toString() { return "FirstBean [name=" + cName + "]"; } }
Create class SecondBean:
package pack1; import org.springframework.beans.factory.annotation.Autowired; public class SecondBean { private FirstBean fBean; public FirstBean getFirstBean() { return fBean; } @Autowired public void setFirstBean(FirstBean fBean) { this.fBean = fBean; } @Override public String toString() { return "SecondBean [fBean=" + fBean + "]"; } }
Create Java configuration file:
package pack1; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import pack1.SecondBean; import pack1.FirstBean; @Configuration public class ConfigFile { @Bean public FirstBean fBean(){ return new FirstBean(); } @Bean(name = "config") public SecondBean sBean(){ return new SecondBean(); } }
Create class SampleTest
package pack1; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import pack1.SecondBean; import pack1.ConfigFile; public class App { public static void main( String[] args ) { ApplicationContext ctxJavaConfig = new AnnotationConfigApplicationContext(ConfigFile.class); SecondBean sBean = (SecondBean) ctxJavaConfig.getBean("config"); System.out.println( sBean); } }
When you execute above test case, we will get
Result:
SecondBean [fBean=FirstBean [name=Nimrit]]
Description
This tutorial covers various topics releated to Spring Framework as listed below
- Framework Overview
- Modules
- Environment Setup
- Saying Hello World with Spring
- IoC Containers
- Bean Definition
- Bean Scopes
- Bean Life Cycle
- Bean Post Processors
- Bean Definition Inheritance
- Dependency Injection
- Injecting Inner Beans
- Injecting Collection
- Beans Auto-Wiring
- Annotation Based Configuration
- Java Based Configuration
- Event Handling in Spring
- Custom Events in Spring
- JDBC Framework Overview
- Transaction Management
Prerequisites
Prior knowledge of Java is essential
Audience
Beginners or students seeking quick introduction to Spring
Learning Objectives
This tutorial is for beginners seeking quick introduction to Spring Framework.
Author: Subject Coach
Added on: 22nd Jun 2015
You must be logged in as Student to ask a Question.
None just yet!