Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of common notes for spring and springboot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

@RequestMapping

This annotation can be used on classes and methods, on classes, indicating the parent path. For example, demo on classes and/demo1 on methods, then the access path is demo/demo1.

The annotation has six attributes:

params: Specifies that certain parameter values must be included in the request for this method to process.

headers: Specifies that the request must contain certain specified header values for the method to process the request.

value: Specifies the actual address of the request, the specified address can be a URI Template pattern

method: Specify the method type of the request, GET, POST, PUT, Delete, etc.

consumes: Specifies the content-type of submission to process the request, e.g. application/json,text/html;

produces: Specifies the type of content to return, only if the (Accept) type in the request header contains the specified type

@PathVariable

The variable on the corresponding path is used before the parameter. The variable name on the path must be consistent with the parameter name.

RequestMapping("demo/demo1/{name}")

public String say(@PathVariable String name){

}

@RequestParam

Same as the following code

String name =request.getParameter("name ")。

@RequestBody

It means that the method parameters are bound to the HTTP request Body, and the front end cannot be submitted in the form. It needs to be submitted in the json way.

@RequestMapping(value = "/something", method = RequestMethod.PUT)

public void handle(@RequestBody String body,@RequestBody User user){

//can bind custom object types

}

@ResponseBody

Indicates that the output is data in json format.

@ModelAttribute

1. When applied to parameters, the parameters passed by the client will be injected into the specified object by name, and this object will be automatically added to the ModelMap for easy use by the View layer;

2. When applied to methods, it will be executed before each method labeled @RequestMapping. If there is a return value, the return value will be automatically added to the ModelMap;

@Bean

The equivalent of XML is to put on top of a method, not a class, meaning to generate a bean and give it to spring management.

@Qualifier

When there are multiple Beans of the same type, you can specify them with @Qualifier("name"). Used with @Autowired

@Autowired

Spring's org.springframework.beans.factory.annotation package can be used to annotate class properties, constructors, and methods.

@Resource

Annotations that do not belong to spring, but instead come from JSR-250 under the java.annotation package, which is used to specify the collaborator Bean for the target bean.

@Resource is equivalent to @Autowired and can be labeled on the setter method of a field or attribute.

Note:

@Autowired annotation is assembled by type by default. If the container contains multiple beans of the same type, an exception that the specified bean type cannot be found will be reported when the container is started. The solution is to combine the @Qualified annotation to specify the bean name injected.

@Resource If the name attribute is not specified and the dependency object is still not found by default name, the @Resource annotation falls back to assembly by type. But once you specify the name attribute, you can only assemble by name.

@Autowired annotation is easy to throw exceptions when assembling, especially when there are multiple bean types assembled, and the solution is to add @ Qualified to qualify.

Component class annotation

@Component: A standard spring Bean class.

@Repository: annotate a DAO component class.

@Service: annotates a business logic component class.

@Controller: annotates a controller component class.

@Component can replace @Repository,@Service,@Controller because these three annotations are labeled by @Component. The annotated java class is treated as a Bean instance, and the Bean instance name defaults to the lowercase initial letter of the Bean class, and other parts remain unchanged.@ Service can also customize the Bean name, but it must be unique!

@SpringBootApplication contains @Configuration,@EnableAutoConfiguration,@ComponentScanbr/> contains @Configuration,@EnableAutoConfiguration,@ComponentScan

@ComponentScan

Component scanning. If you scan for classes with annotations such as @Component @Controller @Service, register those classes as beans.

@Configuration

This class is the source of information for Bean configuration, equivalent to in XML, generally added to the main class.

@EnableAutoConfiguration

Let Spring Boot automatically configure the Spring Framework based on dependencies declared by the application, typically added to the main class.

@Profiles

Spring Profiles provide a way to isolate application configurations and make them work only in certain environments.

Any @Component or @Configuration can be tagged with @Profile, limiting when it can be loaded.

@Configuration@Profile("prod")br/>@Profile("prod")

// ...

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report