Annotations
1. @ SpringBootApplication
Contains @ComponentScan, @Configuration, and @EnableAutoConfiguration annotations. @ComponentScan tells Spring Boot to scan the Configuration class and add it to the application context.
2, @ ComponentScan
Component scanning, which automatically discovers and assembles beans.
3, @ Configuration
An XML configuration file equivalent to Spring; You can check type safety using Java code.
4, @ EnableAutoConfiguration
Automatic configuration
5, @ 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.
6, the @autowired
Automatic import.
7, @ PathVariable
Get parameters.
8, @ JsonBackReference
Solve the nested external chain problem.
9, @ RepositoryRestResourcepublic
It can be used together with spring-boot-starter-data-rest.
Annotations
1. @ SpringBootApplication
This Configuration is equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan.
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
2, @ ResponseBody
Indicates that the return result of this method is directly written to the HTTP Response Body, which is usually 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.
Sample code:
@requestMapping (" /test ") @responseBody public String test(){return "ok"; }Copy the code
3, @ Controller
Defines a controller class that in spring projects is responsible for forwarding URL requests from users to the corresponding service interface (service layer)
Usually this annotation is in a 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 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
4, @ 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
5, @ RequestMapping
Provides routing information responsible for mapping urls to specific functions in the Controller.
6, @ 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.
Search the Java friend public account, reply “backend interview”, send you a Java interview questions treasure book. PDF
7, @ ComponentScan
Indicates that scan components are automatically discovered 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.
If not, Spring Boot scans for classes that use @service, @repository, and other annotations in the Boot package and its subpackages.
8, @ Configuration
Equivalent to traditional XML Configuration files, if some third-party libraries require XML files, it is recommended that you still use the @Configuration class as the main Configuration class for your project — XML Configuration files can be loaded using the @ImportResource annotation.
9, @ Import
Used to import other configuration classes.
10, @ ImportResource
Used to load the XML configuration file.
11, the @autowired
Automatically import dependent beans
12, @ Service
Typically used to modify components in the Service layer
13, @ the 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.
14, @ Bean
Using the @bean annotation method is equivalent to configuring beans in XML.
15, @ the Value
Inject the value of the Spring Boot Application.properties configured property.
Sample code:
@value (Value = "#{message}") private String message;Copy the code
16, @ Inject
Equivalent to the default @autowired, but without the required attribute;
17, @ Component
This annotation refers to components in general, and can be used to annotate components when they are difficult to categorize.
18, @ Bean
The equivalent of putting it on top of a method rather than a class in XML, which means generating a bean and handing it over to Spring to manage.
19, the @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.
20, @ the Qualifier
When there are multiple beans of the same type, you can specify it with @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
21, @ the Resource (name = “name”, type = “type”)
Without parentheses, byName is the default. Same thing with @autowired.
Search the Java friend public account, reply “backend interview”, send you a Java interview questions treasure book. PDF
JPA notes
1, @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.
3, @ NoRepositoryBean
Normally used as a repository for the parent class, Spring does not instantiate the repository with this annotation.
4, @ the Column
If the field name is the same as the column name, it can be omitted.
5, @ Id
Represents the primary key of the property.
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 property 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 entity attributes are loaded.
10, @ JsonIgnore
Some properties in the Java bean are ignored during JSON serialization, affecting both serialization and deserialization.
11, @joincolumn (name= “loginId”)
One-to-one: a foreign key in this table that points to another table. One-to-many: another table points to the foreign key of this table.
12. @onetoone, @onetomany, @manytoone
Correspond to one-to-one, one-to-many, many-to-one in the Hibernate configuration file.
4. SpringMVC annotations
1. @ 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.
2, @ RequestParam
Used in front of method arguments.
3, @ PathVariable
Path variable. Such as:
RequestMapping(" user/get/{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.
The last
I have arranged a: Spring family barrel series, Java systematic information, (including Java core knowledge, interview topics and the latest Internet real questions in 20 years, e-books, etc.) friends who need to pay attention to the public number can be obtained.