You can use Ctrl + F to search, or you can search the right directory by yourself

@SpringBootApplication

Contains @ComponentScan, @Configuration, and @EnableAutoConfiguration annotations. @ComponentScan tells Spring Boot to scan the Configuration class and add it to the application context.

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code

This Configuration is equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan.

  • @ComponentScan
    • Component scanning, which automatically discovers and assembles beans.
  • @Configuration
    • An XML configuration file equivalent to Spring; Allow registering additional beans or importing additional configuration classes in the Spring context.
  • @EnableAutoConfiguration
    • Automatic configuration

@RestController

This annotation, a combination of @Controller and @responseBody, indicates that this is a Controller Bean and that it is a REST-style Controller that fills the return value of the function directly into the BODY of the HTTP response.

Example:

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @restController @RequestMapping("/ demoInfo2 ") PublicClass DemoController2 {@requestMapping ("/test") public String test(){ return"ok"; }}Copy the code

@ResponseBody

ResponseBody: indicates that the return result of this method is directly written to the HTTP ResponseBody. This method is used to obtain data asynchronously and build RESTful apis.

If @requestMapping is used, the return value is usually interpreted as a jump path. If @responseBody is added, the return value is not interpreted as a jump path and is written directly to the HTTP ResponseBody.

So if you get json data asynchronously, and you add @responseBody, it will return json data directly.

This annotation is typically used in conjunction with @requestMapping.

Example:

@requestMapping (" /test ") @responseBody public String test(){return "ok"; }Copy the code

The @autowired and @ the Resource

Automatic import. @ the Resource and the @autowired are doing bean injection is used when, in fact the @ the Resource is not Spring annotations, it’s bag is javax.mail annotation. The Resource, you need to import, but Spring supports the annotation of injection.

Common: Both can be written on fields and setter methods. If you write both on the field, then you don’t need to write setter methods anymore.

Difference:

@Autowired

  • Autowired provides annotations for Spring that require importing packagesorg.springframework.beans.factory.annotation.
  • Autowired is injected only by byType. By default it requires that the dependent object must exist, and if null is allowed, its required property can be set to false. If we want to use assembly byName (byName), we can use it in conjunction with the @qualifier annotation. As follows:

Example:

public class TestServiceImpl {
  @Autowired
  @Qualifier("userDao")
  private UserDao userDao; 
}
Copy the code

@Resource

@ the Resource by default in accordance with the ByName automatic injection, provided by J2EE, you need to import the package javax.mail. The annotation. The Resource. The @Resource has two important attributes: name and type, and Spring resolves the @Resource annotation’s name attribute to the bean’s name, and the Type attribute to the bean’s type.

So, if the name attribute is used, the byName auto-injection policy is used, and if the Type attribute is used, the byType auto-injection policy is used. If neither name nor type attributes are specified, the byName auto-injection policy is used through reflection.

Example:

@Resource (name= "baseDao" )
private BaseDao baseDao;
Copy the code

@Qualifier

As mentioned above, @AutoWired is auto-assembled by type. If more than one Bean of type UserDao exists in the Spring context, a BeanCreationException is thrown; BeanCreationException is also thrown if a Bean of type UserDao does not exist in the Spring context. We can use @Qualifier in conjunction with @AutoWired to solve these problems. As follows:

@Autowired   
@Qualifier("userServiceImpl")   
public IUserService userService;  
Copy the code

conclusion

  • Autowired: Inject by type by default
  • @qualifier (“cusInfoService”) : Usually used as a modifier for @autowired ()
  • @resource (name=”cusInfoService”) : Injection by name by default. You can use the name and type attributes to perform selective injection

@Component,@Repository,@Service, @Controller

Identifies a class as a bean that can be used for auto-assembly with the @AutoWired annotation

  • Component: Generic annotation to mark any class as a Spring Component. If a Bean does not know which layer it belongs to, it can be annotated using the @Component annotation.
  • @repository: Corresponds to the persistence layer (Dao layer), which is used for database-related operations.
  • @service: corresponds to the Service layer, which involves complex logic and requires the Dao layer.
  • @Controller: corresponds to the Spring MVC control layer, which is mainly used to accept user requests and call the Service layer to return data to the front-end page.

@Scope

Declare the scope of the Spring Bean

There are four common Spring Bean scopes:

  • Singleton: A unique bean instance. Beans in Spring are singleton by default.
  • Prototype: Each request creates a new bean instance.
  • Request: Each HTTP request generates a new bean that is valid only within the current HTTP Request.
  • Session: Each HTTP request generates a new bean that is valid only for the current HTTP session.

@Bean

Using the @bean annotation method is equivalent to configuring beans in XML. That is, the equivalent of
in XML, placed on top of a method, not a class, meaning that a bean is generated and handed over to Spring to manage.

@Value

Inject the value of the Spring Boot Application.properties configured property. Sample code:

@value (Value = “#{message}”) private String message;

@RequestMapping

RequestMapping is an annotation to handle request address mapping, which can be used on a class or method. Used on a class, this address is used as the parent path for all methods in the class that respond to requests.

This annotation has six attributes:

Params: Specifies that the request must contain some parameter values for this method to process.

Headers: Specifies that the request must contain some specified header value before the method can process the request.

Value: Specifies the actual address of the request, which can be the URI Template pattern

Method: Specifies the method type of the request, such as GET, POST, PUT, and DELETE

Consumes: specify the processing request of Content Type (the content-type), such as application/json, text/HTML.

Produces: Specifies the content type returned, only if the specified type is in the (Accept) type in the Request header

@RequestParam

Bind request parameters to your controller’s method parameters (annotations in SpringMVC that receive normal parameters)

Syntax: @requestParam (value= “parameter name”, Required = “true/false”,defaultValue= “”)

Value: indicates the parameter name

Required: Indicates whether this parameter is included. The default value is true, indicating that this parameter must be included in the request path. If not, an error is reported.

DefaultValue: the default parameter value. If this parameter is set, required=true is invalid and is automatically false. If this parameter is not passed, the defaultValue is used

@PostMapping("/ali-receive")
public void aliReceive(@RequestParam("message") String message) {
            ReceiveLog receiveLog = JSON.parseObject(message, ReceiveLog.class);

}
Copy the code

Specific examples:

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; @controller@requestMapping ("hello") public class HelloController2 {/** * Accept normal request parameters * http://localhost:8080/hello/show16? @requestParam ("name") * @return */ @requestMapping ("show16") public ModelAndView test16(@RequestParam("name")String name){ ModelAndView mv = new ModelAndView(); mv.setViewName("hello2"); Mv.addobject (" MSG ", "receive normal request parameters:" + name); return mv; } / receiving ordinary request parameters * * * * * http://localhost:8080/hello/show17 in the url displayed without a name parameter not an error, a * @ return * / @ RequestMapping (" show17 ") public ModelAndView test17(@RequestParam(value="name",required=false)String name){ ModelAndView mv = new ModelAndView();  mv.setViewName("hello2"); Mv.addobject (" MSG ", "receive normal request parameter:" + name); return mv; } / receiving ordinary request parameters * * * * http://localhost:8080/hello/show18? Display name = 998 to 998 * http://localhost:8080/hello/show18? Name is hello * @return */ @requestMapping ("show18") public ModelAndView test18(@RequestParam(value="name",required=true,defaultValue="hello")String name){ ModelAndView mv = new ModelAndView();  mv.setViewName("hello2"); Mv.addobject (" MSG ", "receive normal request parameter:" + name); return mv; }}Copy the code

The results are as follows:

@PathVariable

The value used to receive placeholders in the request path

@pathVariable (” XXX “) you can bind the placeholder parameter {XXX} in the URL to the method parameter of the processor class with @pathvariable (” XXX “).

@ RequestMapping (value = “user / {id} / {name}”) request path: http://localhost:8080/hello/show5/1/james

Specific examples:

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; @controller@requestMapping ("hello") public class HelloController2 {/** * @requestMapping (value= "user/{userId}/{userName}") http://localhost:8080/hello/show5/1/james * @param ids * @param names * @return */ @RequestMapping("show5/{id}/{name}") public ModelAndView test5(@PathVariable("id") Long ids ,@PathVariable("name") String names){ ModelAndView mv = new ModelAndView(); Mv.addobject (" MSG "," placeholder mapping: id:"+ids+"; name:"+names); mv.setViewName("hello2"); return mv; }Copy the code

Description:

Results:

JPA related

@entity: @table (name= “) : indicates that this is an Entity class. These two annotations are usually used together, but @table can be omitted if the Table name is the same as the entity class name.

2, @mappedsuperclass: for an entity that is identified as a parent class. Attribute subclasses of the parent class can be inherited.

@noRepositoryBean: normally used as a repository for the parent class. With this annotation, Spring will not instantiate the repository.

4. @column: If the Column name is the same as the Column name, you can omit it.

5. @id: indicates the primary key of the attribute.

6, @ GeneratedValue (strategy = GenerationType SEQUENCE, the generator = “repair_seq”) : Indicates that the primary key generation policy is SEQUENCE (Auto, IDENTITY, or native). Auto indicates that the service can be switched among multiple databases. The sequence name is REPAIR_SEq.

7. @SequenceGeneretor(Name = “REPAIR_SEq”, sequenceName = “SEQ_repair”, allocationSize = 1) : Name is the name of sequence, so that sequenceName is the name of the database sequence. The two names can be the same.

8. @TRANSIENT: indicates that this attribute is not a mapping to a field of a database table and will be ignored by the ORM framework.

If a property is not a field mapping of a database table, it must be marked as @TRANSIENT; otherwise, the ORM framework defaults to annotating it as @BASIC.

9, @basic (fetch= fetchtype.lazy) : the tag can specify how the attributes of the entity are loaded.

10. @jsonIgnore: Ignore some properties of Java bean during JSON serialization, serialization and deserialization are affected.

@joinColumn (name= “loginId”) : one-to-one: specifies the foreign key that points to another table in this table. One-to-many: another table points to the foreign key of this table.

@onetoone, @onetomany, @manyToOne: correspond to one-to-one, one-to-many, many-to-one in hibernate configuration files.

Global exception handling

@ControllerAdvice: contains @Component. It can be scanned. Uniformly handle exceptions.

ExceptionHandler (exception.class) : Used above a method to indicate that the following method is executed when this Exception is encountered.