A spring annotations can be defined as a library that enables the use of annotations to configure your application using spring framework as a backend. The spring framework handles different types of annotations. Examples of these different annotations are mentioned below.They are

1)      @Required

The annotation @ Required in org.springframework.beans.factory.annotation package can be used to mark a property as “necessary-to-be-set ‘(i.e. an annotated (setter) method of a class must be configured for dependency to be injected with a value), otherwise an exception will be thrown by the container at runtime.


public class SimpleMovieLister {
// the SimpleMovieLister has a dependency on the MovieFinder
private MovieFinder movieFinder;
// a setter method so that the Spring container can 'inject' a MovieFinder
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
// business logic that actually 'uses' the injected MovieFinder is omitted...

2)      @Transactional

If we are using Java 5+, the functionality offered by the @Transactional annotations and support the classes is only available to you. We are using annotation based approach to transaction configuration by the addition to the XML- based declarative approach o transaction configuration. The statement of the semantics of transactions directly into Java source code puts the declarations much closer to the affected code, and there is often much danger of undue coupling, since the code is intended to be used transactionally is almost always is implemented that way anyway. The @Transactional annotation may be placed before an interface definition or a method on an interface, or a class definition, or a public method on a class. This annotation is simply a metadata that can be consumed by something that is @Transactional-aware and that can use the metadata to configure the appropriate beans with transactional behavior.

READ  Struts to Spring

The default @Transactional settings are

  • The propagation setting is PROPAGATION_REQUIRED
  • The isolation level is ISOLATION_DEFAULT
  • The transaction is read/write
  • The transaction timeout defaults to the default timeout of the underlying transaction system, or  none if timeouts are not supported
  • Any RuntimeException will trigger rollback, and any checked Exception will not

Properties of @Transactional annotations are given below,

Propagationenum: Propagationoptional propagation setting
Isolationenum: Isolationoptional isolation level
readOnlybooleanread/write vs. read-only transaction
timeoutint (in seconds granularity)the transaction timeout
rollbackForan array of Class objects, which must be derived from Throwablean optional array of exception classes which must cause rollback
rollbackForClassnamean array of class names. Classes must be derived from Throwablean optional array of names of exception classes that must cause rollback
noRollbackForan array of Class objects, which must be derived from Throwablean optional array of exception classes that must not cause rollback
noRollbackForClassnamean array of String class names, which must be derived from Throwablean optional array of names of exception classes that must not cause rollback


// the service class that we want to make transactional
public class DefaultFooService implements FooService {
Foo getFoo(String fooName);
Foo getFoo(String fooName, String barName);
void insertFoo(Foo foo);
void updateFoo(Foo foo);

3)      @Controller

Spring can use annotations to write to java classes such as controller easily.

Eg:- Making any class such as Controller we can simply write @Controller before the class seclaration

Syntax: –

Public class AnnotationController{

4)      @RequestMapping

This annotation used before any method or class declaration to handle the URL request in particular.

READ  Spring Tiles Integration


public String loadForm(Map model) {
return “loginForm”;

5)      @ModelAttribute

This annotation is used for putting the data in model component of a frame.

Syntax: –

return myService.loadCountry();
public List loadCounties() {

6)      @SessionAttribute

This annotation helps to add attribute in session.


7)      @Autowired

We can autowire by using this class.

Syntax: –

public void setMyService(MyService myService) {
this.myService = myService;


package com.name.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.name.domain.Community;
import com.name.domain.Country;
import com.name.domain.User;
import com.name.service.UserService;
public class UserController {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
public List<Country> populateCountryList() {
return userService.getAllCountries();
public List<Community> populateCommunityList() {
return userService.getAllCommunities();
@RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model) {
User user = new User();
model.addAttribute("user", user);
return "userForm";
@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@ModelAttribute("user") User user) {
return "redirect:userSuccess.htm";

In the above example the methods like populateCountryList() and populateCommunityList() are used to populate the country and community. The annotation of @ModelAttribute is used at the method level and this annotation is used to indicate that the method contain reference data used by the model, so method when extending the SimpleFormController.