Unruly, love of life. Java Cultivator (wechat official ID: Java Cultivator), welcome to follow.

Annotations

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

@Configuration is equivalent to spring’s XML Configuration file; You can check type safety using Java code.

@enableAutoConfiguration Automatic configuration.

@componentscan ComponentScan to automatically discover and assemble some beans.

The @Component can be used in conjunction with CommandLineRunner to perform some basic tasks after the program is started.

The @RestController annotation is a combination of @Controller and @responseBody, indicating that this is a Controller bean and that the return value of the function is inserted directly into the body of the HTTP response, which is a REST-style Controller.

@autoWired Automatically imports.

@pathvariable gets arguments.

@jsonBackReference resolves nested external links.

@ RepositoryRestResourcepublic with spring – the boot – starter – data – rest.

Annotations

@springBootApplication: declare that SpringBoot will automatically configure the program as necessary. This configuration is equivalent to: @Configuration, @EnableAutoConfiguration, and @ComponentScan are configured.

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

ResponseBody: indicates that the return result of this method is directly written to the HTTP response body. 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 Response Body.

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. Concern public number programmer Xiao Le reply keyword “offer” to obtain algorithm interview questions and answers

Sample code:

@ RequestMapping ("/test ") @ ResponseBodypublic String test () {return "ok"; }Copy the code

@Controller: Defines the Controller class. In spring projects, the Controller is responsible for forwarding URL requests from the user to the corresponding service layer. Usually this annotation is in the class, and usually methods need to be annotated with @requestMapping.

Sample code:

@Controller@RequestMapping(" /demoInfo ") publicClass DemoController {@autoWired private DemoInfoService DemoInfoService; @RequestMapping("/hello") public String hello(Map<String,Object> map){ System.out.println("DemoController.hello()"); map.put("hello","from TemplateController.helloHtml"); // The hello.html or hello.ftl template will be used for rendering. }}Copy the code

@restController: A collection of annotated control-layer components such as actions in Struts, @responseBody, and @Controller.

Sample code:

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

@requestMapping: Provides routing information that maps urls to specific functions in the Controller.

@enableAutoConfiguration: Spring Boot Auto-configuration: Try to automatically configure your Spring application based on the JAR dependencies you add. For example, if HSQLDB exists in your CLASspath and you don’t manually configure any database connection Beans, we will automatically configure an in-memory database.

You can add the @EnableAutoConfiguration or @SpringBootApplication annotation to an @Configuration class to select automatic Configuration. If you find specific auto-configuration classes applied that you don’t want, you can disable them using the exclusion attribute of the @enableAutoConfiguration annotation.

@ComponentScan: automatically discovers scanned components for this class. If you scan a class with @Component, @Controller, @Service, etc., and register it as a Bean, you can automatically collect all Spring components, including the @Configuration class.

We often use the @ComponentScan annotation to search for Beans, combined with the @AutoWired annotation to import. All Spring components can be collected automatically, including the @Configuration class. We often use the @ComponentScan annotation to search for Beans, combined with the @AutoWired annotation to import.

If not, Spring Boot scans for classes that use @service, @repository annotations in the Boot package and its children.

@Configuration: Equivalent to a traditional XML Configuration file. If some third-party libraries require an XML file, it is recommended that the @Configuration class be used as the main Configuration class of the project — the XML Configuration file can be loaded using the @ImportResource annotation.

@import: Used to Import other configuration classes.

ImportResource: Used to load XML configuration files.

Autowired: Automatically import dependent beans

@service: Generally used to modify components in the Service layer

@Repository: Using the @Repository annotation ensures that DAO or Repositories provides an exception translation. The DAO or Repositories class modified by this annotation will be found and configured by ComponetScan without providing an XML configuration entry for them.

@beans: Annotating @beans is equivalent to beans configured in XML.

@value: Injects the Value of the Spring Boot Application. properties configured property.

Sample code:

@value (Value = "#{message}")private String message;Copy the code

Inject @inject: Equivalent to the default @autowired, but without the required attribute;

@Component: Component annotation. This annotation can be used when components are difficult to categorize.

@bean: equivalent to XML, on top of a method, not a class, meaning to generate a Bean and hand it over to Spring to manage.

AutoWired: Automatically import dependent beans. ByType way. The configured Bean is used to complete the assembly of attributes and methods. It can annotate class member variables, methods and constructors to complete the work of automatic assembly. When (required=false) is added, no error is reported even if the bean is not found.

@qualifier: When there are multiple beans of the same type, you can specify it as @qualifier (” name “). Used with @autowired. In addition to being injected by name, the @qualifier Qualifier provides more fine-grained control over how candidates are selected, as follows:

@Autowired@Qualifier(value = “demoInfoService”)private DemoInfoService demoInfoService;
Copy the code

@resource (name= “name”,type= “type”) : byName is the default if there is no parentheses. Same thing with @autowired. Concern public number programmer xiao Le reply key word “Java” to get big factory interview questions and answers

JPA notes

@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

MappedSuperClass: for an entity that is identified as a superclass. 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.

@column: If the field name is the same as the Column name, it can be omitted.

@id: indicates that the attribute is the primary key.

@ 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.

@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.

@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. @BASIC (fetch= fetchtype.lazy) : tag that specifies how entity attributes are loaded

JsonIgnore: Some properties of Java bean are ignored during JSON serialization, which affects serialization and deserialization.

@joinColumn (name= “loginId”) : One-to-one: indicates 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: Corresponds to one-to-one, one-to-many, many-to-one in hibernate configuration files.

4. SpringMVC annotations

@requestMapping: @requestMapping (/path) indicates that the controller processes all UR L requests of /path. 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: used in front of method arguments.

@requestParam String a =request.getParameter(” a “).

@pathvariable: PathVariable. Such as

RequestMapping(" user/get/ MAC /{macAddress} ") Public String getByMacAddress(@pathVariable String macAddress){//do something; }Copy the code

The arguments must be the same as the names in braces.

5. 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.