views:

435

answers:

3

When moving to Spring 2.5.x I found that it adds more stereotype annotations (on top of @Repository from 2.0): @Component, @Service and @Controller. How do you use them? Do you rely on implicit Spring support or you define custom stereotype specific functions/aspects/features? Or is it predominately for marking beans (compile time, conceptual, etc.)?

+3  A: 

The following stereotype annotations in 2.5 can be used in a Spring MVC application as an alternative to wiring the beans in XML:

  • @Repository - for DAO beans - allows you to throw DataAccessException when the data source is not available.

  • @Service - for business beans - are fairly simple beans that have some default retention policies set up.

  • @Controller - for servlets - allows you to set up page request mappings, etc.

In addition, a generic fourth annotation has been introduced: @Component. All of the MVC annotations are specialisations of this one, and you can even use @Component on it's own, though by doing this in Spring MVC, you will not make use of any future optimisations/functionality added to the higher-level annotations. You can also extend @Component to create your own custom stereotypes.

Here is a quick example of the MVC annotations in action... First, the data access object:

@Repository
public class DatabaseDAO {
    @Autowired
    private SimpleJdbcTemplate jdbcTemplate;

    public List<String> getAllRecords() {
        return jdbcTemplate.queryForObject("select record from my_table", List.class);
    }
}

The service:

@Service
public class DataService {
    @Autowired
    private DatabaseDAO database;

    public List<String> getDataAsList() {
        List<String> out = database.getAllRecords();
        out.add("Create New...");
        return out;
    }
}

And finally, the controller:

@Controller("/index.html")
public class IndexController {
    @Autowired
    private DataService dataService;

    @RequestMapping(method = RequestMethod.GET)
    public String doGet(ModelMap modelMap) {
        modelMap.put(dataService.getDataAsList());
        return "index";
    }
}

I found this article very good for giving a broad overview of the stereotype annotations, in addition to the official documentation.

seanhodges
would you say that the new (2.5) stereotypes apply to Spring MVC only?
grigory
The three high-level annotations could be viewed as MVC specific. You could re-use them for other purposes, but you might fall foul of upgrade problems if they are changed in future releases. @Component is the generic bean wiring annotation, and could be easily extended and used outside of the MVC realm.
seanhodges
A: 

dont forget to add this tag on xml

    <context:component-scan  base-package="com.example.beans"/> 
bassem
+2  A: 

The annotations isn't MVC specific anymore. See the reference documentation for more information. An example of using the @Component annotation or a specification of it is the tcServer with its monitoring support. See here for an example. This monitoring support is added with load-time AspectJ weaving.

Summarized, the annotations can be used in different settings at runtime after the Spring container is started, or at compile/load-time with AspectJ weaving.

Espen
This is not entirely true. The @Component annotation is not MVC specific, but the others listed in the OP's question are all MVC specific.
seanhodges
seanhodges: No, @Repository, @Controller and @Service all belongs to the org.springframework.context module. @Endpoint that's also a specialization of @Component is Spring WS specific and is part of the org.springframework.ws.java5 module.A snippet from the Spring 3 reference documentation (not MVC specific):In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.
Espen