Thursday, October 6, 2011

Annotations in Spring

Annotation in Java and Spring has made developer’s life very easy. To use Annotation in spring we need to add following tags in Spring Configuration file

<context:annotation-config/>

<context:component-scan base-package="com.spring.action"></context:component-scan>
           
By default, <context:component-scan> looks for classes that are annotated with one of a handful of special stereotype annotations:

  • @Component—A general-purpose stereotype annotation indicating that the class is a Spring component
  • @Controller—Indicates that the class defines a Spring MVC controller
  • @Repository—Indicates that the class defines a data repository
  • @Service—Indicates that the class defines a service
  • Any custom annotation that is itself annotated with @Component

Difference between @Service and @Component
They both belong to org.springframework.stereotype.Service

When annotating your class with @Component you mark it as a regular java component class when annotating with @Service you mark it as a “special” type of component for special purpose like transaction or associating with aspects.

@Autowired

Autowired is one of very important Annotation in spring

If we write

@Autowired
MyController c;

Its mean Spring will look for a class with name MyController If its an Interface then it will look for its implementer classes. If two classes have implemented this Interface and they both have @Component Annotation then Spring will generate exception

In this case you have two options

1) Option1
Give name to component like
@Controller("FirstController")
or
@Controller("SecondController")

In this case you have to define your variable like this

@Autowired
MyController FirstController;

Or
@Autowired
MyController SecondController;


2) Option
Use Qualifier
@Controller("FirstController")
or

@Controller("SecondController")

and in your service use

@Autowired
@Qualifier("FirstController")
MyController c;

Another Example of Autowire

package com.spring.action.controller;

import org.springframework.stereotype.Controller;

@Controller
public class ThirdController implements MyController {
            public boolean processMethod(String s){
                        System.out.println("Third Controller is processing Method");
                        return true;
            }
           
}

When Spring scans the com.spring.action package, it’ll find that ThirdController is annotated with @Controller and will automatically register it in Spring. By default, the bean’s ID will be generated by camel-casing the class name. In the case of ThirdController that means that the bean ID will be thirdController.

We can use it in any of my class by writing

@Autowired
MyController thirdController;


The @Inject annotation from JSR-330

In an effort to unify the programming model among the various dependency injection frameworks, the Java Community Process recently published the Dependency Injection for Java specification. Known in the Java Community Process as JSR-330 or more commonly as at inject, this specification brings a common dependency injection model to Java. As of Spring 3, Spring supports the at inject model.2 The centre piece of JSR-330 is the @Inject annotation. This annotation is an almost complete drop-in replacement for Spring’s @Autowired annotation. So, instead of using the Spring-specific @Autowired annotation, you might choose to use @Inject on the instrument property:

Instead of @Qualifier Inject is using

@Inject
@Named("guitar")
private Instrument instrument;


Spring 3.0 introduced @Value, a new wiring annotation that lets you wire primitive values such as int, boolean, and String using annotations.

@Value("MySong")
private String song;

 Sample Project is present at following location


1 comment:

  1. i also agree with this open source are best php are also open source

    ReplyDelete