Different Annotations in Spring framework

Connect with

Spring Framework
Spring is a one of the most popular Application Framework in java. the user communities of Spring framework are wide and huge.

Different Annotation used in Spring Framework

There are different annotation used in Spring Application Framework , here are some key annotations used in Spring. If you novice in Spring Framework you can view my another post , Spring Restful Webservice example with Maven and Tomcat.

[table “” not found /]

example of @Controller

code snippet of @Controller

@Controller
@RequestMapping(value="/customer")
public class CustomerController { 
 ...
// some methods
}

example of @RestController

code snippet of @RestController

@RestController
@RequestMapping(value="/customer")
public class CustomerController { 

--
}

example of @Service

code snippet of @RestController

@Service("customerService")
public class CustomerServiceImpl implements CustomerService{

 @Autowired
 private CustomerDao customerDao;
--
}

example of @Repository

code snippet of @Repository

@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {

--
// some methods
}

example of @RequestMapping

code snippet of @RequestMapping

@RestController
@RequestMapping(value="/customer")
public class CustomerController {


/**
  * @return
  */
 @RequestMapping(value = "/", method = RequestMethod.GET)
 public ResponseEntity> getCustomerListHandler() {
  List customerList = null;
  HttpStatus status = HttpStatus.OK;
  try {
   customerList = customerService.getCustomers();
   if (customerList == null) {
    
   }
  } catch (Exception e) {
   status = HttpStatus.BAD_REQUEST;
  }

  return new ResponseEntity>(customerList, status);
 }

}

example of @Autowired

code snippet of @Autowired

@Controller
@RequestMapping(value="/customer")
public class CustomerController {

// create the reference of CustomerService, 
// Spring container inject it implementation bean into it.
 @Autowired
 private CustomerService customerService;

// some methods below
}

Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *