zoukankan      html  css  js  c++  java
  • SpringBoot笔记 -- 注解

    @Autowired

    自动导入依赖的bean

    @Bean

    用@Bean标注方法等价于XML中配置的bean。

    @Component

    泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

    @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";
    }
    }
    

    @ComponentScan

    表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。

    @Configuration

    相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。使用Java代码可以检查类型安全。

    @ConfigurationProperties

    可以让开发者将整个配置文件,映射到对象中,比@Value 效率更高。能支持 properties 文件和 yml 文件,并且支持更复杂配置结构以及 Validation 功能。

    @EnableAutoConfiguration

    Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。

    @Inject

    等价于默认的@Autowired,只是没有required属性

    @Import

    用来导入其他配置类

    @ImportResource

    用来加载xml配置文件。

    @JsonBackReference

    解决嵌套外链问题

    @PathVariable

    通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过
    @PathVariable("xxx") 绑定到操作方法的入参中。

    @GetMapping("/{modelId}")
        @ApiOperation("根据设备型号ID获取设备型号详细信息")
    	public ResponseEntity<DeviceModel> findDeviceModel(@PathVariable("modelId") String modelId) {
    		return ResponseEntity.ok(deviceModelService.findById(modelId));
    	}
    

    @Qualifier

    当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定。与@Autowired配合使用。@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者,具体使用方式如下:

    @Autowired 
    @Qualifier(value = “demoInfoService”) 
    private DemoInfoService demoInfoService;
    

    @RequestMapping

    提供路由信息,负责URL到Controller中的具体函数的映射。可以写在类和方法上。
    博客:https://www.jianshu.com/p/27b01fb4d688

    @RestController

    是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。示例代码:

    package com.kfit.demo.web;
    
    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";
    }
    }
    

    @Resource

    @Resource(name=”name”,type=”type”):没有括号内内容的话,默认byName。与@Autowired干类似的事。

    @ResponseBody

    表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,用于构建RESTful的api。在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用。示例代码:

    @RequestMapping(“/test”) 
    @ResponseBody 
    public String test(){ 
    return”ok”; 
    }
    

    @ResponseBody注解

    @SpringBootApplication

    Spring Boot项目的核心注解,主要目的是开启组件扫描和自动配置
    实际上它是一个复合的Annotation:

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @Configuration
    @EnableAutoConfiguration
    @Component Scan
    public @interface SpringBootApplication{
        ...
    }
    

    但对于SpringBoot应用来说,重要的只有三个Annotation,而“三体”结构实际上指的就是这三个Annotation:
    @Configuration
    @Enable Auto Configuration
    @Component Scan
    这三个Annotation等同于@SpringBootApplication 。

    @Service

    一般用于修饰service层的组件

    @Value

    注入Spring boot application.properties配置的属性的值。示例代码:

    @Value(value = “#{message} ) 
    private String message;
    
  • 相关阅读:
    Android Camera子系统之Linux C应用开发人员View
    【Android】把外部文件拷贝的AVD安卓模拟器上的sdcard上,而且在AVD中浏览sdcard的文件
    HDU 2196 Computer(求树上每一个节点到其他点的最远距离)
    HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树
    scala 变量定义,基本操作符
    mybatis or
    Nginx
    hessian协议原理
    同一台电脑上装两个或两个以上的tomcat服务器
    端口号
  • 原文地址:https://www.cnblogs.com/junzifeng/p/11947037.html
Copyright © 2011-2022 走看看