Spring ,Spring Boot Annotation List
Prior to annotations, the behavior of the Spring Framework was largely controlled through XML configuration. Today
@Component vs @Bean
@Component class level and @bean for method level
if the class is outside spring container whereas we can create a bean of a class using @Been
@Component auto detects and configures the beans using classpath scanning whereas @Bean explicitly declares a single bean
@Autowired — level- field , method, constructor
use for injecting the bean .
by default, it will use byType autowire .
@RequestBody — when you need to consume object then you can use RequestBody
@PostMapping(“/request”) public ResponseEntity postController( @RequestBody LoginForm loginForm) { exampleService.fakeAuthenticate(loginForm); return ResponseEntity.ok(HttpStatus.OK); }
// Approval Item services
@PostMapping(“getApprItms/{type}/{subtype}/{rqstr}/{rqstrEmail}/{role}/{geo}/{highValueTransc}”)
public ResponseEntity<List<ChangeRequestApprovalItemDTO>> createAppovalItems(@PathVariable(“type”) String type,
@RequestBody List<ChangeRequestAdjustmentDTO> adjs) {
}
@Qualifier- only field level , you can use along with
@Autowired When you are creating one or more bean for same field . then you need to more control on your bean then you must be use @Qualifier.
if you are define more beans for single field then you will get ambiguity problem .
@Component
class Demo{
@Autowired
@Qualifire(“test”)
private Test test;}
@Configuration — level only for Classes
when you delere any class with @Configuration it means that class provide one or more bean method and that method is processed by Spring container at RunTime.
@Configuration
Class AppConfig{
@Bean(name=”demoServices”)
public Demo Services(){}
@Bean level -only for method
you can use only along with @Configuration .
@ComponentScan — level main class only
@ComponentScan tells Spring in which packages you have annotated classes which should be managed by Spring.
So, for example, if you have a class annotated with @Controller which is in a package which is not scanned by Spring, you will not be able to use it as Spring controller.
Use for search Package,and particular calss (multiple Package ,multiple calss )
package — @ComponentScan({“com.in.package1”,”com.in.package2"})
class-
@ComponentScan({“com.in.springboot.basics.springbootin10steps”,”com.in.springboot.somethingelse”})
Spring Stereotype Annotation
@Component — generic sterotype for any spring managed component
@Controller -only for presentation layer
@Services — only for services layer
@EnableAutoConfiguration -level main class only
annotation auto-configures the beans that are present in the classpath
@EnableAutoConfiguration annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. For example, If HSQLDB is on your classpath, and you have not manually configured any database connection beans, then Spring will auto-configure an in-memory database.
Spring Boot Annotation List
@SpringBootApplication =@EnableAutoConfiguration+@ComponentScan+@Configuration
you must be annotated the main class with this annotation, it will be allow running Jar with embedded tomcat web server
@SpringBootApplication =
@EnableAutoConfiguration (used enable auto configuration)
+@ComponentScan+ ( scan component )
@Configuration (use for java based configuration)
Rest Application Annotation
@RestController = @Controller+@ResponceBody
@Controller
@ResponseBody
public class MVCController { .. your logic }
@RestController
public class RestFulController { …. your logic }The job of @Controller is to create a Map of model object and find a view but @RestController simply return the object and object data is directly written into HTTP response as JSON or XML.
@RequestMapping for URL mapping
Crud operation
@GetMapping — for select
@Postmapping — for create new object
@PutMapping — for update operation
@patchMapping — partial update operation
@DeleteMapping — for delete operation
@ExceptionHandler — it is used to define the class of exception .
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
}
@ExceptionHandler(RecordNotFoundException.class)
public final ResponseEntity<Object> handleUserNotFoundException(RecordNotFoundException ex, WebRequest request) {
@PathVariable -it can handle dynamic change in
url@RequestMapping(value=”/book/{ISBN}”, method= RequestMethod.GET)
public String showBookDetails(@PathVariable(“ISBN”) String id,
Model model){
model.addAttribute(“ISBN”, id);
return “bookDetails”;
}
@RequestParam -
we can use @RequestParam with @ResponceBody to retrieve the URL parameter and map into method argument
@RequestParam to extract query parameters, form parameters and even files from the request
public void example(@RequestParam (“id”) int id){}
@RequestMapping(“box”)
public String getRequest(@RequestParam int id) {
return “Example of Boxes” + id;
}
=======
Testing URL (How to provide id value)
http://localhost:9999/address/box?id=200
we can also make value can be optional
@RequestAttribute
it helps us access to preexisting global request Attribute(outside controller)
@RequstMapping(“/”)
public String handle(@RequestAttribute (“counter”) Counter counter){
//BL
return ;
}
preexisting request attribute which are managed by globally .
@ControllerAdvice class level for global Exception handler
@RestControllerAdvice = @ControllerAdvice +@ResponceBody
@SessionAttribute parameter level
#used to bind the method parameter to session Attribute
#it is used to retrive the existing attribute from session then managed Globally .
it is used at method parameter.
it can hold multiple session value
@Controller
@RequestMapping
@SessionAttribute({“fordata”,”someData”})
class Controllers{public String onSubmit(@ModelAttribute(“fordata”) FromData t){
return “”;
}
@SessionAttribute(“user)
class LoginController(){@ModelAttribute(“user”)
public User setUpUserForm(){return new User();
}
@GetMapping
public String userInfo(@sessionAttribute (“user”) User user){return user}};