跳到主要内容

Java 常用注解

· 阅读需 5 分钟

Java 常用注解整理

不断补充,更新中...

一、Spring

  • @Autowire

@Autowrie(require=false)

自动装配bean,默认不允许值为null(即require=true),可以在属性上或者Set方法上使用 先通过bytype的方式查找,再通过byname的方式查找

  • @Resource

自动装配 bean

先通过 byname 的方式查找,再通过 bytype 的方式查找

  • @Nullable

字段可以为 null

  • @Qualifier(value="xxx")

制定唯一的 Bean 注入(通过别名寻找),配合 @Autowire 使用

  • @Component、@Repository、@Service、@Controller

注册 Bean 并装配到 Spring 容器中,通过类路径扫描来自动侦测,等价于<bean id="xx" class="xxx">

一般 @Component 在 pojo 中使用、@Respository 在 Dao 中使用、@Service在 Service 中使用、@Controller 在 Controller 中使用

本质上都是 @Component

  • @Value("xxx")

注入值、等价于<property name="xxx" value="xxx"/>

  • @Scope(value="xxx")

配置 bean 的作用域,默认是单例的,可放在属性上或者set方法上

  • @Configuration

使一个 Java 类成为配置类,等价于<beans> ... </beans>

  • @ComponentScan("包名")

根据制定的配置自动扫描 package

默认扫描规则是扫描被 @Component, @Repository, @Service, @Controller 或者已经声明过 @Component 自定义注解标记的组件

  • @Import(配置类的class)

将对应类注册到 ioc 容器中

一般用于将包扫描路径外的类注册到 ios 容器中

  • @Bean

注册 Bean 并装配到 Spring 容器中,通常是在标有该注解的方法中定义产生这个 bean 的逻辑 (return对象),等价于<Bean id="方法名" class="返回值"/>

  • @Aspect

标注这个类是一个切面

@Before("execution(* com.xxx.(..))")

@After("execution( com.xxx.*(..))")

二、Mybatis

  • @Mapper

说明该接口的实现类由 mybatis 创建,并将生成后的对象注册到 ioc 容器中

  • @MapperScan

自动扫描某个包下的所有接口,相当于包下的所有接口都加上了 @Mapper 注解

  • @Param

对应 sql 中#{}占位符中的变量名称,常在不同类型时制定

TDeposit queryByGuinoAndBoxid(@Param("guino")String guino, @Param("boxid")Integer boxid);
  • @Alias

在pojo类中使用,为其起一个别名,配合@Resource使用

@Service,@Controller等注解集成了@Alias注解,直接通过value属性设置即可

@Alias("author")
public class Author {
...
}
@Service(value = "hahhh")
  • @Insert @Delete @Select @Update

对应增删改查语句

三、Lombok

  • @Data

包含@ToString、@EqualsAndHashCode、@Getter、@Setter的功能

  • @ToString

自动生成toSring()方法

  • @Getter、@Setter

自动生成get、set方法

  • @NONNULL

自动加上if (members == null) throw new java.lang.NullPointerException("members");

  • @Synchronized

自动给方法加同步锁

synchronized ($lock) { ... }

  • @Slf4j

简化日志的定义

四、SpringMVC

  • @Controller

实际上是@Component,作为组件注册到ioc容器

  • ResponseBody

返回值将和response的body绑定,即页面会直接显示返回的内容

  • @RestController

相当于@Controller+@ResponseBody的组合注解

  • @RequestBody

用于获取请求body里的内容

  • @RequestMapping

用于定于访问路径。默认请求方式是GET

/**
* value: uri地址
* method: 指定请求的method类型, GET、POST、PUT、DELETE等
* consumes: 指定Content-Type
* produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
* params: 指定request中必须包含某些参数值是,才让该方法处理。
* headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
**/
@RequestMapping(value="/test", method = RequestMethod.GET)
  • @RequestParam

从请求URL中读取请求参数。默认是必须参数

/**
* value: 入参的请求参数名字
* required: 是否必须,默认是true
* defaultValue: 默认值
**/
public String queryUserName((@RequestParam(value = "name", required = false, defaultValue = "honey") String name))
  • @PathVariable

获取URI中Path变量定义为花括号的内容

@RequestMapping(value = "/products/```{id}```", method = RequestMethod.PUT)
@PathVariable("id") String id
  • @GetMapping("/{id}"), @PostMapping("/{id}"), @PutMapping("/{id}"), @DeleteMapping("/{id}")

相当于指定了method参数的@RequestMapping

五. 异常处理

  • @ControllerAdvice

全局处理异常

  • @ExceptionHandler

@ExceptionHandler(value = ProductNotfoundException.class)

@ControllerAdvice
public class ProductExceptionController {
@ExceptionHandler(value = ProductNotfoundException.class)
public ResponseEntity\`<Object>\` exception(ProductNotfoundException exception) {
return new ResponseEntity<>("Product not found", HttpStatus.NOT_FOUND);
}
}

六、SpringBoot

  • @SpringBootApplication

@SpringBootApplication 启动注解,是一个复合注解

包含了@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan这三个注解

  • @SpringBootConfiguration

继承 @Configuration 注解,主要用于加载配置文件, 标注当前类是配置类, 并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。

  • @EnableAutoConfiguration 注解,开启自动配置功能

可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。借助于Spring框架原有的一个工具类:SpringFactoriesLoader的支持,@EnableAutoConfiguration可以智能的自动配置功效才得以大功告成

  • @ComponentScan

主要用于组件扫描和自动装配 , 自动扫描并加载符合条件的组件或bean定义,最终将这些bean定义加载到容器中。我们可以通过basePackages等属性指定@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现从声明@ComponentScan所在类的package进行扫描,默认情况下是不指定的,所以SpringBoot的启动类最好放在root package下。

  • @ServletComponentScan

在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。

  • @MapperScan(“com.Vm.server”)

只会扫描包中的接口,不会扫描类,扫描指定包中的接口

  • @EnableScheduling

spring自带的定时服务

public class ScheduledTasks {
@Scheduled(fixedRate = 1000 * 30) //每30秒执行一次
public void reportCurrentTime(){
System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat().format (new Date ()));
}
}