SpringBoot常用注解
来源:互联网 发布:策略模式 java示例 编辑:程序博客网 时间:2024/06/06 00:45
@ComponentScan 组件扫描,可自动发现和装配一些Bean。
@Configuration 等同于spring的XML配置文件;使用Java代码可以检查类型安全。
@EnableAutoConfiguration 自动配置。
@SpringBootApplication 申明让spring boot自动给程序进行必要的配置
这个配置等同于:@ComponentScan ,@Configuration 和 @EnableAutoConfiguration 三个配置。
其中 @ComponentScan 让spring Boot扫描到Configuration类并把它加入到程序上下文。示例代码:~
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
~@ResponseBody 表示该方法的返回结果直接写入HTTP的 response body中
在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用。示例代码:
~
@RequestMapping(“/test”)
@ResponseBody
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";}
}
~~~
- @RestController 注解是 @ResponseBody和 @Controller的合集
> 表示这是个控制器bean,将返回值直接填入HTTP响应体中,是REST风格的控制器。
用于标注控制层组件(如struts中的action),示例代码:
~~~@RestController @RequestMapping(“/demoInfo2”) publicclass DemoController2 { @RequestMapping("/test") public String test(){ return"ok"; }}~~~
@PathVariable 获取参数。
~
RequestMapping("user/get/mac/{macAddress}")
public String getByMacAddress(@PathVariable String macAddress){
//do something;
}
~@RequestMapping 提供路由信息,负责URL到Controller中的具体函数的映射。
@Service 一般用于修饰service层的组件
@Autowired 自动导入依赖的bean。
@Inject 等价于默认的@Autowired,只是没有required属性;
@Bean 用@Bean标注方法等价于XML中配置的bean,意思是产生一个bean,并交给spring管理
@Import 用来导入其他配置类。
@ImportResource 用来加载xml配置文件。
@JsonBackReference 解决嵌套外链问题。
@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Repository 使用@Repository注解可以确保DAO或者repositories提供异常转译
@Resource(name=”name”,type=”type”) 没有括号内内容的话,默认byName。与@Autowired干类似的事
@Value 注入Spring boot application.properties配置的属性的值。示例代码:
~
@Value(value = “#{message}”)
private String message;
~@Qualifier 当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定
与@Autowired配合使用。@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者,具体使用方式如下:
~
@Autowired
@Qualifier(value = “demoInfoService”)
private DemoInfoService demoInfoService;
~
- springboot 常用注解
- springboot常用注解二
- 常用springboot注解
- springboot常用注解总结
- SpringBoot常用注解
- SpringBoot常用注解
- SpringBoot常用注解
- Springboot 常用注解
- 【SpringBoot】常用注解
- Springboot 常用注解
- SpringBoot一些常用注解
- springboot常用注解集锦
- springboot常用注解
- Springboot 常用注解
- springBoot Restfull 常用注解
- SpringBoot常用注解
- java springboot常用注解
- SpringBoot中常用的注解
- 排序问题
- Oracle查询以及修改表空间大小,临时表空间大小,SGA
- 基于《机器学习--周志华》一书对数据挖掘的理解
- python:变量和字符串
- HTML页面 css 怎么让内容自适应 出现拖动的滚动条
- SpringBoot常用注解
- hibernate检索分类
- 移动端(三)—— rem适配和flex适配
- python学习笔记之yield使用浅析
- Java中的异常
- bzoj1069 [SCOI2007]最大土地面积(凸包+旋转卡壳)
- Excel在统计分析中的应用—第九章—非参数检验-独立性检验(x2统计量简化公式应用)
- java 中的方法
- 优秀的无线AP产品,改变你的工作感受