一、注解(annotations)列表
1、@SpringBootApplication
包含了
@ComponentScan
、
@Configuration
和
@EnableAutoConfiguration
注解。
其中
@ComponentScan
让
Spring Boot
扫描到
Configuration
类并把它加入到程序上下文。
2、@ComponentScan
组件扫描,可自动发现和装配一些
Bean
。
3、@Configuration
等同于
Spring
的
XML
配置文件;使用
Java
代码可以检查类型安全。
4、@EnableAutoConfiguration
自动配置
5、@RestController
该注解是
@Controller
和
@ResponseBody
的合集,表示这是个控制器
Bean
,并且是将函数的返回值直接填入
HTTP
响应体中,是
REST
风格的控制器。
6、@Autowired
自动导入。
7、@PathVariable
获取参数。
8、@JsonBackReference
解决嵌套外链问题。
9、@RepositoryRestResourcepublic
配合spring-boot-starter-data-rest使用。
二、注解(annotations)详解
1、
@SpringBootApplication
:申明让
Spring Boot
自动给程序进行必要的配置,这个配置等同于:
@Configuration
,
@EnableAutoConfiguration
和
@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);
}
}
2、
@ResponseBody
:表示该方法的返回结果直接写入
HTTP Response Body
中,一般在异步获取数据时使用,用于构建
RESTful
的
api
。
在使用
@RequestMapping
后,返回值通常解析为跳转路径,加上
@ResponseBody
后返回结果不会被解析为跳转路径,而是直接写入
HTTP Response Body
中。
比如异步获取
json
数据,加上
@ResponseBody
后,会直接返回
json
数据。
该注解一般会配合
@RequestMapping
一起使用。
示例代码:
@RequestMapping(“/test”)
@ResponseBody
public String test(){
return ”ok”;
}
3、
@Controller
:用于定义控制器类,在
spring
项目中由控制器负责将用户发来的
URL
请求转发到对应的服务接口(
service
层)
一般这个注解在类中,通常方法需要配合注解
@RequestMapping
。
示例代码:
@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");
// 会使用hello.html或者hello.ftl模板进行渲染显示.
return"/hello";
}
}
4、
@RestController
:用于标注控制层组件(如
struts
中的
action
),
@ResponseBody
和
@Controller
的合集。
示例代码:
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";
}
}
5、
@RequestMapping
:提供路由信息,负责
URL
到
Controller
中的具体函数的映射。
6、
@EnableAutoConfiguration
:
Spring Boot
自动配置(
auto-configuration
):尝试根据你添加的
jar
依赖自动配置你的
Spring
应用。
例如,如果你的
classpath
下存在HSQLDB,并且你没有手动配置任何数据库连接
beans
,那么我们将自动配置一个内存型(
in-memory
)数据库”。
你可以将
@EnableAutoConfiguration
或者
@SpringBootApplication
注解添加到一个
@Configuration
类上来选择自动配置。
如果发现应用了你不想要的特定自动配置类,你可以使用
@EnableAutoConfiguration
注解的排除属性来禁用它们。
搜索Java知音公众号,回复“后端面试”,送你一份Java面试题宝典
.pdf
7、
@ComponentScan
:表示将该类自动发现扫描组件。
个人理解相当于,如果扫描到有
@Component
、
@Controller
、
@Service
等这些注解的类,并注册为
Bean
,可以自动收集所有的
Spring
组件,包括
@Configuration
类。
我们经常使用
@ComponentScan
注解搜索
beans
,并结合
@Autowired
注解导入。可以自动收集所有的
Spring
组件,包括
@Configuration
类。
如果没有配置的话,
Spring Boot
会扫描启动类所在包下以及子包下的使用了
@Service
、
@Repository
等注解的类。
8、
@Configuration
:相当于传统的
xml
配置文件,如果有些第三方库需要用到
xml
文件,建议仍然通过
@Configuration
类作为项目的配置主类——可以使用
@ImportResource
注解加载
xml
配置文件。