Reading:  

Quick to the point introduction to Spring Framework


Java Based Configuration

@Configuration:

This annotation indicates that the class can be used by the container of Spring IoC as a source of bean definitions.

@Bean Annotations:

this annotation returns an object that should be registered as a bean in the Spring application context.

Let's check these annotations with use of an example

First up is our old friend class called Student, which has @Configuration and @Bean annotations as shown below

 

Java file: Student

package pack1;

@Configuration
public class Student {
	private String sname;

	@Bean
	public void setName(String sname) {
		this.sname = sname;
	}
}

The configuration file is Beans.xml:

<beans>
   <bean id="student" class="pack1.Student " />
</beans>

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 = cont.getBean("studentBean ");
                stud.setName("Nimrit");
		System.out.println("Name : " + stud.getName());
		stud.printName();
	}
}

Compile and run the program will get the below result:

Result:

Name : Nimrit

 

To load many configuration classes:

Syntax:

public static void main(String[] args) {
   AnnotationConfigApplicationContext con = 
   new AnnotationConfigApplicationContext();

   con.register(BeanConfig.class, ExmConfig.class);
   con.register(UtilityConfig.class);
   con.refresh();

   MyService myService = con.getBean(MyService.class);
   myService.doStuff();
}
 

Injecting Bean Dependencies:

The dependency is as same as one method of bean calling another when @Beans have dependencies on one another.

Example:

Java file: Student

package pack1;
import org.springframework.context.annotation.*;

@Configuration
public class StudentConfig {

	@Bean
	public Student student() {
		return new Student();
	}@Bean
	public Student_Details student_details() {
		return new Student_Details;
	}
}

 

Java file: Student

package pack1;

public class Student {
	private Student_Details student_details;


	public Student(Student_Details student_details) {
		System.out.println("Inside Student  constructor");
		this.student_details = student_details;
	}
	public void address() {
		student_details.stud_Address();
	}
}

Java file: Student_Details

package pack1;

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.annotation.*;

public class MainApp {
	public static void main(String[] args) {
		ApplicationContext app = new AnnotationConfigApplicationContext(StudentConfig.class);

		Student st = app.getBean(Student.class);
		st.address();
	}
}

Compile and run the program will get the below result:

Result:

Inside Student_Details constructor
Inside Student constructor
Inside Stud_Address

The @Import Annotation:

@Import: For loading @Bean definitions from another configuration class. Example:

@Configuration
public class StudentX {
   @Bean
   public X x() {
      return new X(); 
   }   
}

To import above declared Bean in another Bean Declaration is as shown:

@Configuration
@Import(StudentX.class)
public class StudentY {
   @Bean
   public Y y() {
      return new X(); 
   }    
}

When instantiating the context we need not to specify both StudentX.class and StudentY.class, just StudentY only needs to be supplied

public static void main(String[] args) {
   ApplicationContext app = 
   new AnnotationConfigApplicationContext(StudentY.class);
   // both beans X and Y will be available.
   X x= app.getBean(X.class);
   Y y = app.getBean(Y.class);
}

 

Lifecycle Callbacks:

The annotation @Bean supports callback methods initialization and destruction of them, it is same as Spring XML's init-method and destroy-method attributes on the element of bean:

public class Student {
   public void beanInit() {
      // initialization
   }
   public void destroy() {
      // destruction
   }
}

@Configuration
public class StudentConfig {
   @Bean(beanInitMethod = "init", destroyMethod = "destroy" )
   public Student student() {
      return new Student();
   }   
}
 

Specifying Bean Scope:

User can override by using the annotation @Scope where this is default scope is singleton.

@Configuration
public class StudentConfig {@Bean@Scope("prototype")
	public Student student() {
		return new Student();
	}
}



In next part of this guide we will take a look at event handling in Spring.

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!