Springboot 常用注解整理
自动装配
@ComponentScan
用于配置Spring需要扫描的被组件注解注释的类所在的包。
@Component
用于标注一个普通的组件类,它没有明确的业务范围,只是通知Spring被此注解的类需要被纳入到Spring Bean容器中并进行管理。
@Autowired
Autowired用于自动装配,对于接口的实现类,可以使用该注解,消除get和set方法。
声明一个接口
public interface UserService {
void readyTest(String var);
}
单个实现类
新建一个类,实现该接口
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Override
public void readyTest(String var) {
System.out.println("方法被调用,收到参数:"+var);
}
}
使用@Autowired注解,实现属性的自动装配
@SpringBootTest
class TestApplicationTests {
// 属性自动装配,可以省略get和set方法
// 此处的属性名称可以任意自定义,都会去找 UserService 接口的唯一实现类
@Autowired
UserService userServiceImpl;
@Test
void contextLoads() {
userServiceImpl.readyTest("Autowired");
}
}
多个实现类
我们新建一个实现类
import org.springframework.stereotype.Service;
@Service
public class UserServiceNewImpl implements UserService{
@Override
public void readyTest(String var) {
System.out.println("新方法被调用,收到参数:"+var);
}
}
当有多个实现类的情况下,会报错:无法自动装配。存在多个 'UserService' 类型的 Bean。
idea会自动识别此错误。
此时需要显式指定实现类:
@SpringBootTest
class TestApplicationTests {
@Autowired
UserService userServiceNewImpl;// 参数名称为类名
@Test
void contextLoads() {
userServiceNewImpl.readyTest("Autowired");
}
}
或者配合@Qualifier注解使用:
@SpringBootTest
class TestApplicationTests {
@Autowired
@Qualifier("userServiceNewImpl") // 指定实现类
UserService userService;
@Test
void contextLoads() {
userService.readyTest("Autowired");
}
}
@Resource
@Resource 和 @Autowired 一样,是用来实现依赖注入的。
声明一个接口
public interface UserService {
void readyTest(String var);
}
单个实现类
新建一个类,实现该接口
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Override
public void readyTest(String var) {
System.out.println("方法被调用,收到参数:"+var);
}
}
使用@Resource 注解,实现属性的自动装配
@SpringBootTest
class TestApplicationTests {
@Resource
UserService userService;
@Test
void contextLoads() {
userService.readyTest("Resource");
}
}
当单个实现类,对应不同的bean时,也可以使用name属性指定具体的bean
@SpringBootTest
class TestApplicationTests {
@Resource(name="student")
private SayInterface student;
@Resource(name="teacher")
private SayInterface teacher;
@Test
void contextLoads() {
student.say();
teacher.say();
}
}
@Configuration
public class HumanConfig {
@Bean(name = "student",initMethod = "init")
public Human getStudent() {
Human student = new Human();
student.setName("Teacher");
return student;
}
@Bean(name = "teacher",destroyMethod = "destroy")
public Human getTeacher() {
Human teacher = new Human();
teacher.setName("Student");
return teacher;
}
}
多个实现类
我们新建一个实现类
import org.springframework.stereotype.Service;
@Service
public class UserServiceNewImpl implements UserService{
@Override
public void readyTest(String var) {
System.out.println("新方法被调用,收到参数:"+var);
}
}
当有多个实现类的情况下,会报错:org.springframework.beans.factory.BeanCreationException: Error creating bean with name
需要区分的是,idea不会自动识别此错误,在运行时才会报错。
解决方法就是手动指定@Resource的name属性:
@SpringBootTest
class TestApplicationTests {
@Resource(name = "userServiceNewImpl")
UserService userService;
@Test
void contextLoads() {
userService.readyTest("Resource");
}
}
@Configuration
@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
简单来说,就是初始化bean所对应的对象,提供默认属性。
@Configuration
public class HumanConfig {
@Bean(name = "student",initMethod = "init")
public Human getStudent() {
Human student = new Human();
student.setName("Teacher");
return student;
}
@Bean(name = "teacher",destroyMethod = "destroy")
public Human getTeacher() {
Human teacher = new Human();
teacher.setName("Student");
return teacher;
}
}
@ConditionalOnWebApplication
只有当spring为web服务时,才使注解生效
@AutoConfigureAfter
在加载配置类之后再加载当前类
@ConditionalOnProperty
控制配置类是否生效
name
配置项的名字
havingValue
与配置的值对比
matchIfMissing
未配置属性时的匹配方式
@Bean
方法级别上的注解,产生一个被IOC容器所管理的bean。bean可以理解为一个豆子,一个对象。
创建一个类:
import lombok.Data;
@Data
public class Human implements SayInterface {
private String name;
@Override
public void say() {
System.out.println("Hello " + name);
}
public void init() {
System.out.println(name + " init");
}
public void destroy() {
System.out.println(name + " destroy");
}
}
创建一个配置类:
@Configuration
public class HumanConfig {
// 默认bean的名称和方法名相同
// 使用name定义bean的名称
// initMethod声明周期,创建后执行
@Bean(name = "student",initMethod = "init")
public Human getStudent() {
Human student = new Human();
student.setName("Teacher");
return student;
}
// destroyMethod声明周期,销毁后执行
@Bean(name = "teacher",destroyMethod = "destroy")
public Human getTeacher() {
Human teacher = new Human();
teacher.setName("Student");
return teacher;
}
}
@Primary
当一个接口有多个不同的实现时,使用自动装配会报错
该注解可以设置默认优先选择
@Import
用来导入配置类或者一些需要提前加载的类
@Service
@Service用于标注业务层组件,会将类自动注入到Spring容器。
默认名称是类名,可以使用@Service("customerName")指定名称,使用@Autowired可以自动注入,当指定名称时,可以使用@Qualifier指定具体的实现类。
默认的bean是单例的。
@Repository
是@Component注解的延伸,与@Component注解一样,被此注解标注的类会被Spring自动管理起来,@Repository注解用于标注DAO层的数据持久化类。
@DependsOn
指定依赖关系,在实例化某个Bean之前,必须先实例化其他的Bean
SpringBoot
@SpringBootApplication
代表程序的入口类,核心注解,这是一个组合注解,用于开启自动配置
@SpringBootTest
加载ApplicationContext,启动spring容器
@Order
定义Bean在spring中执行的顺序
@Test
单元测试
@Data
Lombok注解
添加依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
@Data会自动生成无参构造、属性的get/set方法、equals/canEqual方法、hashCode方法、toString方法
@SuppressWarnings
@SuppressWarnings用于编译器镇压警告。
value - 取消显示的警告集,可以取如下值:
unchecked
java中,如果使用了未进行参数化的操作,会产生警告信息,进行了未经检查的调用
意思是使用了容器,但是没有使用泛型,通常产生在ArrayList等集合的调用,使用泛型作为方法参数类型的方法调用等。
@SuppressWarnings(value = {"unchecked"})
rawtypes
对于泛型类,如果将其对象,设置成某类的属性,而没有指定具体的泛型时,会产生如下警告
形参化类xxx的原始使用
使用rawtypes可以镇压此警告
ConstantConditions
镇压 NullPointerException
基本注解
@Value
用于给变量注入值
类似的还有@PropertySource、@Environment、@ConfigurationProperties
可以使用${}的方式,注入配置文件中的值
@Value("${binx.captcha.type}")
private String captchaType;
binx:
captcha:
type: math
@Scope
用来调节作用域
//多实例,IOC容器启动创建的时候,并不会创建对象放在容器在容器当中,当你需要的时候,需要从容器当中取该对象的时候,就会创建。
@Scope("prototype")
//单实例 IOC容器启动的时候就会调用方法创建对象,以后每次获取都是从容器当中拿同一个对象(map当中)。
@Scope("singleton")
//同一个请求创建一个实例
@Scope("request")
//同一个session创建一个实例
@Scope("session")
@Override
@Override 表示重写父类的方法或者实现接口方法,不写该注解也可以,写上主要有三个好处:
- 作为注释
- 表示这是对父类方法的重写
- 方便编辑器做验证,防止方法名写错
@JsonFormat
实现格式的自动转换
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
元注解
@Retention
定义注解被保留的时间长短
取值(RetentionPoicy)有:
- SOURCE:在源文件中有效(即源文件保留)
- CLASS:在class文件中有效(即class保留)
- RUNTIME:在运行时有效(即运行时保留)
@Retention(RetentionPolicy.RUNTIME)
public @interface Excel {
}
@Target
注解修饰对象的范围
@Target(ElementType.FIELD) // 用于描述域(属性)
public @interface Excel {
}
@Documented
生成Javadoc的时候是否会把注解标注出来
@Inherited
使注解具有继承性,如果一个类使用了该注解,则其子类自动具有该注解
字段校验
@NotBlank
只能作用在String上,不能为null,而且调用trim()后,长度必须大于0
@NotBlank(message = "参数名称不能为空")
@Size
验证对象的长度是否在给定的范围内
本文来自博客园,作者:Bin_x,转载请注明原文链接:https://www.cnblogs.com/Bin-x/p/16488642.html