Springboot is a framework developed by Spring to quickly build javaWeb projects. Its biggest feature is that it is simple and fast, which simplifies a large number of configuration files for Spring projects. Its core idea is that conventions are greater than configuration, and annotations are simple The shortcut method is naturally the best choice. The following summarizes some commonly used annotations in springboot, and will continue to update and add new annotations in the future. Of course, for @Controller @RequestMapping which is too common, we will not give examples.
(1) Parameter class
Attribute value = “name”, required = true/false defaultValue = xxx
public void say(@RequestParam("name") String name,@ RequestParam("pwd") String password){
}
[email protected] Similar to @RequestParam Based on restful style
@RequestMapping(" /demo/{id}/{name}")
public void say(@PathVariable("id" ) String id,@PathVariable("name") String name1){
}
[email protected] Bind the name attribute in the front-end form to the attribute of the entity class, and require the name and entity class in the form The attribute names are the same.
public void< span style="color: #000000;"> say (@RequestBody User user){
}
[email protected] Get the value from the request, the interceptor or the attribute value pre-stored in @ModelAttribute The default value is true, and an exception will be thrown if there is no value ServletRequestBindingException, you can set the required attribute to false.
(2). Scanning annotations
[email protected] @Controller @Service @Repository These three are the children of @Component Class, add the scanned package to the IOC container. For the package that is not scanned, @ComponentScan can scan the specified package and define it on the class
[email protected] Tell Spring this method to generate a class for it Manage, generate an instance of Bean, @Bean is more flexible than @Component
[email protected] It is equivalent to
It is equivalent to [email protected]
in xml
package com.dxz.demo.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("TestConfiguration container starts and initializes...");
}
// @Bean annotation to register beans, and you can specify initialization and destruction method
// @Bean(name="testBean",initMethod=" start",destroyMethod="cleanUp")
@Bean
@Scope("prototype")
public TestBean testBean() {
return new TestBean();
}
}
TestBean
package< /span> com.dxz.demo.configuration;
public class TestBean {
private String username;
private String url;
private String password;
public void sayHello() {
System.out.println("TestBean sayHello...");
}
public String toString() {
return "username:" + this.username + ",url:" + this.url + ",password:" + this< span style="color: #000000;">.password;
}
public void start() {
System.out.println("TestBean initialization...");
}
public void cleanUp() {
System.out.println("TestBean destroyed...");
}
}
Test class
Copy Code
package com.dxz.demo.configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
// @Configuration annotation spring container loading method, replace with AnnotationConfigApplicationContext ClassPathXmlApplicationContext
ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
// If you load the spring-context.xml file:
// ApplicationContext context = new
// ClassPathXmlApplicationContext("spring-context.xml");
//Get the bean
TestBean tb = (TestBean) context.getBean("testBean");
tb.sayHello();
}
}
Result:
You can also add @Componet to the TestBean class in conjunction with @Configration
(3). Resource introduction class
1 @Value
@Value(“${book.name}”) Get the book.name=old man and sea in application.properties (also in your other configuration files)
@Value(“#{name}”) Get the name attribute of the book entity class
[email protected] and @Autowired
Both are used for bean injection, @ Resource is a java annotation. Spring also supports this annotation. There are Name and Type attributes. When neither is specified, the name attribute is used; @Autowired annotation is Spring’s annotation by default.
When a class has For multiple implementations, @Resource(name=”xxx”) or @Qualifier(
and @Autowired need to be in the implementation class with @Primary ( man and woman realize people, need to be realized on man or woman)[email protected](“classpath:xxxxx.xml”) Load the xml configuration file
[email protected]
Sometimes it is necessary to encapsulate the information in the configuration file into entity classes, which will be very convenient to call.
Configuration file:
connection.username=admin
connection.password=kyjufskifas2jsfs
connection.remoteAddress=192.168.1.1
Entity class:
@Component
@ConfigurationProperties(prefix = "connection")
@Data //Lombok's annotation needs to introduce lombok's dependent IDEA tool Install lombok function to automatically generate get set method
public class ConnectionSettings {
private String username;
private String remoteAddress;
private String password;
}
When in use
@ RestController
@RequestMapping("/task")
public class TaskController {
@Autowired
ConnectionSettings conn;
@RequestMapping(value = {"/", ""})
public String hellTask(){
String userName = conn.getUsername();
return "hello task !!";
}
}
Of course, you can also directly use @ConfigurationProperties and @Bean together so that there is no need to add annotations to the ConnectionSettings class
< div class="code">
@Bean
@ConfigurationProperties(prefix = "connection")
public ConnectionSettings connectionSettings(){
return new ConnectionSettings();
}