Component scopes

You can specify the scope for your components in Spring MVC. The default scope is singleton, which means that there will be only one instance of the component in the context. Every request for this component will be served with the same instance. The other scopes are as follows:

  • Prototype: Each request for the component is served with a new instance of that class.
  • Request: Valid for web applications. Single instance of a component class created for each HTTP request.
  • Session: Single instance of a component class created for each HTTP session. Used in web applications.
  • Global session: Single instance of a component class created for the global HTTP session. Used in portlet applications.
  • Application: Single instance of a component class in the web application. The instance is shared by all sessions in that application.

If the component to be injected was not instantiated at the time it was requested, then Spring creates an instance of the component. In the previous example, we have not specified the scope of the CourseDAO component, so the same instance would be injected if there is another request for injecting CourseDAO. You can specify the scope in the @Component annotation. You can also specify the component name if you want to override the default name that Spring gives to the component.

To see if a single instance of a component is injected when no scope is specified, let's change the main method in the SpringMain class and make two calls to the getBean method:

  public static void main (String[] args) { 
    //create ApplicationContext 
    ApplicationContext ctx = new 
ClassPathXmlApplicationContext("context.xml"); //call and print ctx.getBean first time System.out.println("Course Service 1 - " +
ctx.getBean("courseService")); System.out.println("Course Service 2 - " +
ctx.getBean("courseService")); }

Run the application and you should see the same instance of the courseService bean printed. Let's change the scope of the CourseService component:

@Component 
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 
public class CourseService { 
  //content remains the same 
} 

Run the application again; this time, you should see different instances of the CourseService component.

When Spring comes across the @Autowire annotation, it tries to find the component by type. In the preceding example, courseDAO is annotated with @Autowire. Spring tries to find a component of CourseDAO type; it finds an instance of CourseDAO and injects it. But what if there are multiple instances of the class in the context? In such a case, we can use the @Qualifier annotation to uniquely identify components. Let's now create the ICourseDAO interface, which will be implemented by two components, namely CourseDAO and CourseDAO1:

public interface ICourseDAO { 
} 

CourseDAO implements ICourseDAO and is uniquely qualified as "courseDAO":

@Component 
@Qualifier("courseDAO") 
public class CourseDAO implements ICourseDAO { 
} 

CourseDAO1 implements ICourseDAO and is uniquely qualified as "courseDAO1":

@Component 
@Qualifier("courseDAO1") 
public class CourseDAO1 implements ICourseDAO { 
} 

In the CourseService class, we will use a qualifier to uniquely identify whether we want CourseDAO or CourseDAO1 to be injected:

@Component 
public class CourseService { 
 
  @Autowired 
  private @Qualifier("courseDAO1") ICourseDAO courseDAO; 
 
  public ICourseDAO getCourseDAO() { 
    return courseDAO; 
  } 
} 

The qualifier can also be specified at method arguments, for example:

@Autowired 
public void setCourseDAO (@Qualifier("courseDAO1") ICourseDAO 
courseDAO) { this.courseDAO = courseDAO; }

Run the application now. You should see that an instance of CourseDAO1 is printed in the console.

We have covered the basics of dependency injection in Spring. However, Spring offers a lot more options and features for dependency injection than we have covered here. We will see more DI features as and when required in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.15