Spring Boot 常用注解目录
1:@SpringBootApplication
SpringBootApplication:
包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。
其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。
@SpringBootApplication:申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。
package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//与@Configuration @EnableAutoConfiguration @ComponentScan相同
public class Application {
public static void main (String [] args) {
SpringApplication.run(Application.class,args);
}
}
2:@Configuration
@Configuration 等价于spring的XML配置文件即:(等价于 < beans > < /beans >)
3:@EnableAutoConfiguration
Spring Boot自动配置(自动配置):尝试根据您添加的jar依赖自动配置您的Spring应用。
4:@ComponentScan
@ComponentScan等价于<context:component-scan base-package=”com.xxx.xxx”/> ,表示将类自动发现扫描组件
5:@Component
@Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
6:@RestController
@RestController:用于标注控制层组件(如struts中的action),@ ResponseBody和@Controller的合集。@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML
7:@Autowired
@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用 如下:
@Autowired()
@Qualifier("xxxDao")
private XxxDao xxxDao;
8:@PathVariable
@PathVariable(“参数名称”)获取URL中对应的参数值。如果@PathVariable标明参数名称,则参数名称必须和URL中参数名称一致。
提示 |
---|
本人以抱着学习的态度去分享,以上内容如有雷同,不胜荣幸!如有不足,欢迎评论留言! |