SpringBoot的常用注解及解释

1.@Controller     即为SpringMVC的注解,用来处理http请求;

2.@RestController     Spring4后新增的注解,是@Controller和@ResponseBody的组合注解,用来返回json字符串;

3.@GetMapping      @RequestMapping和Get请求方法的组合;

4.@PostMapping     @RequestMapping和Get请求方法的组合;

5.@PutMapping     @RequestMapping和Put请求方法的组合;

6.@DeleteMapping     @RequestMapping和Delete请求方法的组合;

7.@Mapper       SpringBoot继承MyBatis的时候,在Mapper接口中加@Mapper注解

8.@MapperScan    延续7,也可以在运行的主类上添加@MapperScan("***.mapper")注解包扫描

9.@EnableTransactionManagement      在入口类中使用注解,用来开启事务

10.@Transactional      在访问的数据库service方法上添加注解用来开启事务,和9同用。

11.@PathVariable        获取url中的数据,用在SpringBoot开发RESTFull 该注解是实现RESTFull最主要的一个注解

    RESTFull不是一个框架,而是一个互联网软件架构设计的风格,基于这种理念的设计的接口可以更简洁,更有层次;

    REST这个词,是Roy Thomas Fielding在他2000年的博士论文中提出的;    

    比如我们要访问一个http接口:http://localhost:80080/api/order?id=1021&status=1

    采用RESTFull风格则http地址为:http://localhost:80080/api/order/1021/1

    @PostMapping 接收和处理post方式的请求

    @PutMapping 接收put方式的请求,可以用PostMapping代替

    @GetMapping 接收和处理get方式的请求

    @DelectMapping 接收delect方式的请求,可以用GetMapping代替

    一个demo用来展示RESTFull的开发

   

@GetMapping("/boot/user/{id}")
public @ResponseBody Object user(@PathVariable("id") Integer id){

    User user = new User();
    user.setId(id);
    user.setName("张三");

    return user ;
}

12.@Component      该注解是Spring的,是在SpringBoot集成Dubbo的时候使用,在Dubbo的接口实现类中使用,和@Service一同使用,@Servcie是Dubbo提供的。

13.@SpringBootApplication       用于Dubbo服务提供者的入口main程序。

14.@EnableDubboConfiguration       和13放在同一main方法上,用于开启Dubbo的配置支持。

15.@Reference       用于Dubbo的消费者,即controller中,是Dubbo的注解,用来引用远程的Dubbo服务

16.@Configuration      在该配置类上添加@Configuration注解,标注此类为一个配置类,让SpringBoot扫描到。

17.@WebServlet(urlPatterns=""),SpringBoot中使用servlet,在servlet上使用。

18.@ServletComponentScan(basePackages="com.***.***"),和17一同使用,此注解在main方法的主类上添加,如果有多个servlet类,注解为@ServletComponentScan(basePackages={"com.***.servlet","com.***.***"})

19.@WebFilter(urlPatterns="/*")      SpringBoot中使用filter,即过滤器,在servlet上使用,同样,在main方法主类上添加@ServletComponentScan(basePackages="com.***.***")注解。

20.@Bean    等价于spring的xml配置文件中的

<bean id="***" class="org.springframework.boot.web.servlet.ServletRegistrationBean">
    <property name="" value=""/>
 </bean>

方法名等价于bean的id

方法返回值等价于bean的class



猜你喜欢

转载自blog.csdn.net/sicky_b/article/details/79641364