27. Annotations and Source Level Metadata Support

27.1 Introduction

Java 5 introduced source-level metadata called annotations to program elements, usually, classes and/or methods

For example we might add metadata at the class level using the Spring's @Transactional annotation that is used to support Spring's declarative transaction management features.

@Transactional
public class PetStoreImpl implements PetStoreFacade, OrderService {

We could also add metadata to a method as follows:

public class PetStoreImpl implements PetStoreFacade, OrderService {

  . . .

  @Transactional
  public void insertOrder(Order order) {    
    this.orderDao.insertOrder(order);    
    this.itemDao.updateQuantity(order);  
  }

  . . . 
}

The value of using annoations has been broadly embrassed by the JEE community. For example, it's much less verbose than the traditional XML deployment descriptors. While it is desirable to externalize some things from program source code, some important enterprise settings - notably transaction characteristics - arguably belong in program source.

Spring uses Java 5 annotations thoughout the framework across a wide range of features such as DI, MVC, and AOP and supports JEE standard annotations such as @PreDestroy and @PostConstruct defined by JSR-250. This chapter describes the @Required attribute and provides links to other parts the documentation where the various attributes are described in more detail.