Quick to the point introduction to Spring Framework
Chapters
Annotation Based Configuration
To configure the dependency injection using annotations can be possible from the version of Spring 2.5, the configuration of bean into the component class by using annotations instead of using XML to describe a bean wiring. By default Annotation wiring is not turned on in the container of Spring. So, before using annotation-based wiring, need to enable it in Spring configuration file.
The configuration file by using annotation in Spring application is look like as below:
<?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 definitions go here --> </beans>
The annotations are:
@Autowired:
The annotation @Autowired
declares a constructor, field, setter method, or configuration method to be autowired by type. Items annotated with @Autowired
do not have to be public.
JSR-250 Annotations:
JSR-250 based annotations supported by Spring which include @PostConstruct
, @PreDestroy
and @Resource
annotations.
@Qualifier:
The annotation @Qualifier
used along with @Autowired
, to remove the confusion by giving which exact bean will be wired.
@Required:
The annotation @Required
applies to bean property setter methods.
Let's go through example to understand above annotations
@Autowired:
Example:
Java file: Student
package pack1; public class Student { private Student_Details stud; @Autowired public void setName(Student stud) { System.out.println("Inside setName"); this.stud = stud; } public Student getName() { return stud; } public void display() { stud.Stud_Address(); } }
Java file: Student_Details
public class Student_Details { public Student_Details() { System.out.println("Inside Student_Details constructor"); } public void Stud_Address() { System.out.println("Inside Stud_Address"); } }
Java file: ExmTest
package pack1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ExmTest { public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("Beans.xml"); Student st = (Student) cont.getBean("studentBean"); st.Stud_Address(); } }
The configuration file is Beans.xml:
<?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-3.0.xsd"> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for studentBean bean without constructor-arg --> <bean id=" studentBean" class=" pack1.Student "> </bean> <!-- Definition for Student_Details bean --> <bean id=" Student_Details " class="pack1.Student_Details "> </bean> </beans>
Compile and run the program will get the below result:
Result:
Inside Student_Details constructor
Inside setName
Inside Stud_Address
@Autowired with (required=false):
Syntax: This @Required annotation is used along with @Autowired, where user can turn off the default behavior by using (required=false) option.
@Autowired(required = false) public void setName(Student stud) { System.out.println("Inside setName"); this.stud = stud; }
Spring JSR-250 Annotations:
@PostConstruct:
this annotation is used for initialization process where it indicates a method to be invoked after a bean has been created and dependency injection is complete.
@PreDestroy:
this annotation is used for cleanup process where it indicates a method to be invoked before a bean is removed from the context of Spring.
package pack1; public class Customer { private String cust_name; public String getCname() { return cust_name; } public void setCname(String cust_name) { this.cust_name = cust_name; } public void displayMessage() { System.out.println("Account holder name: " + cust_name); } @PostConstruct public void beanInit() { System.out.println("Inside beanInit--@PostConstruct"); } @PreDestroy public void beanDestroy() { System.out.println("Inside beanDestroy --@PreDestroy"); } }
Create the ExmTest class:
package pack1; 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 ExmTest { public static void main(String[] args) { AbstractApplicationContext c = new ClassPathXmlApplicationContext("Beans.xml"); Customer customer = (Customer) c.getBean("custbean"); customer.displayMessage(); } }
Create the 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" xmlns:p="http://www.springframework.org/schema/p" 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="custbean" class="pack1.Customer" init-method="beanInit" destroy-method="beanDestroy"> <property name="cust_name" value="Suman"> </property> </bean> </beans>
Compile and run the program will get the below result:
Result:
Inside beanInit--@PostConstruct
Account holder name: Suman
Inside beanDestroy --@PreDestroy
@Resource Annotation:
The annotation @Resource
attribute is a 'name' which will be interpreted as the name of bean to be injected
package pack1; public class Student { private Student_Details stud; @Resource(name = "stud") public void setName(Student stud) { System.out.println("Inside setName"); this.stud = stud; } public Student getName() { return stud; } public void display() { stud.Stud_Address(); } }
@Qualifier:
Java file: Student
package pack1; public class Student { private String sname; public void setName(String sname) { this.sname = sname; } public Student getName() { return sname; } }
Java file: Student_Details
package pack1; public class Student_Details { @Autowired@Qualifier("stud1") private Student student; public Student_Details() { System.out.println("Inside Student_Details constructor"); } public void printName() { System.out.println("Name : " + student.getName()); } }
Java file: ExmTest
package pack1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ExmTest { public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("Beans.xml"); Student_Details st = (Student_Details) cont.getBean("studentBean"); st.printName(); } }
The configuration file: Beans.xml
<?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-3.0.xsd"> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for student_Details bean --> <bean id="student_Details" class="pack1.Student_Details"> </bean> <!-- Definition for stud1 bean --> <bean id="stud1" class="pack1.Student"> <property name="name" value="Nimrit" /> </bean> </beans>
Compile and run the program will get the below result:
Result:
Inside Student_Details constructor
Name : Nimrit
@Required:
Java file: Student
package pack1; public class Student { private String sname; private Integer id; @Required public void setName(String sname) { this.sname = sname; } public Student getName() { return sname; }@Required public void setId(Integer id) { this.id = id; } public Integer getId() { return id; } }
Java file: ExmTest
package pack1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ExmTest { public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("Beans.xml"); Student stud = (Student) cont.getBean("studentBean "); System.out.println("Name : " + stud.getName()); System.out.println("Id : " + stud.getId()); stud.printName(); } }
The configuration file is Beans.xml:
<?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-3.0.xsd"> http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for student_Details bean --> <bean id="student_Details" class="pack1.Student_Details"> </bean> <!-- Definition for stud1 bean --> <bean id="stud1" class="pack1.Student"> <property name="name" value="Nimrit" /> <property name="id" value="21"/> </bean> </beans>
Compile and run the program will get the below result:
Result:
Name : Nimrit
Id : 21
In next part of this guide we will touch on Java based configurations in spring framework
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!