SpringBoot注解大全
点击上方目录,找你所找
@SpringBootApplication
标识为启动类!
申明让spring boot自动给程序进行必要的配置,这个配置等同于:
@Configuration
,@EnableAutoConfiguration
和@ComponentScan
三个配置。
其中@ComponentScan
让spring Boot扫描到Configuration类并把它加入到程序上下文。
package com.example.myproject;
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);
}
}
@Configuration
相当于传统Spring的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过
@Configuration
类作为项目的配置主类——可以使用@ImportResource
注解加载xml配置文件。
@RestController
加上RestController 表示修饰该Controller所有的方法返回JSON格式,直接可以编写Restful接口。
@RestController
注解是@Controller
和@ResponseBody
的合集,
示例代码:
@RestController
@RequestMapping(“/demoInfo2”)
publicclass DemoController2 {
@RequestMapping("/test")
public String test(){
return"ok";
}
}
Controller
用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解@RequestMapping。
示例代码:
@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");
//会使用hello.html或者hello.ftl模板进行渲染显示.
return"/hello";
}
}
@Controller和@RestController的区别?
官方文档:
@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
中文翻译:
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1)如果只是使用@RestController
注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
例如:本来应该到success.jsp页面的,则其显示success.
2)如果需要返回到指定页面,则需要用 @Controller
配合视图解析器InternalResourceViewResolver
才行。
3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody
注解。
@EnableAutoConfiguration
Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring框架。
例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration
或者@SpringBootApplication
注解添加到一个@Configuration
类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration
注解的排除属性来禁用它们。
@ComponentScan
表示将该类自动发现扫描组件。
basePackages:控制器扫包范围。
个人理解相当于,如果扫描到有@Component
、@Controller
、@Service
等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration
类。我们经常使用@ComponentScan
注解搜索beans,并结合@Autowired
注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service
,@Repository
等注解的类。
@ComponentScan(basePackages = "com.wangys.controller")
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@Component
泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Component
可配合CommandLineRunner使用,在程序启动后执行一些基础任务。
@RequestMapping
提供路由信息,负责URL到Controller中的具体函数的映射。
@ResponseBody
表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,用于构建RESTful的api。
在使用@RequestMapping
后,返回值通常解析为跳转路径,加上@responsebody
后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody
后,会直接返回json数据。该注解一般会配合@RequestMapping
一起使用。
示例代码:
@RequestMapping(“/test”)
@ResponseBody
public String test(){
return ”ok”;
}
@PathVariable
获取参数。
@JsonBackReference
解决嵌套外链问题。
@RepositoryRestResourcepublic
@RepositoryRestResourcepublic
配合spring-boot-starter-data-rest使用。
@Import
用来导入其他配置类。
@ImportResource
用来加载xml配置文件
@Service
一般用于修饰service层的组件
@Repository
使用
@Repository
注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被@ComponetScan
发现并配置,同时也不需要为它们提供XML配置项。
@Bean
用
@Bean
标注方法等价于XML中配置的bean,放在方法的上面,而不是类上面,意思是产生一个bean,并交给spring管理。
@Value
注入SpringBoot application.properties配置的属性的值。
示例代码:
@Value(value = “#{message}”)
private String message;
@Inject
等价于默认的
@Autowired
,只是没有required属性;
@Autowired
自动导入依赖的bean。
byType方式。
把配置好的Bean拿来用,完成属性、方法的组装,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
当加上(required=false)时,就算找不到bean也不报错。
@Qualifier
当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定。与@Autowired配合使用。@Qualifier限定描述符除了能根据名字进行注入,也能进行更细粒度的控制如何选择候选者,
具体使用方式如下:
@Autowired
@Qualifier(value = “demoInfoService”)
private DemoInfoService demoInfoService;
@Resource
@Resource(name=”name”,type=”type”):没有括号里的内容的话,默认byName。与
@Autowired
干类似的事。
ExceptionHandler
表示拦截异常
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<String, Object> exceptionHandler() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("errorCode", "101");
map.put("errorMsg", "系統错误!");
return map;
}
}