注解
Java注解又称Java标注,是在 JDK5 时引入的新特性,注解(也被称为元数据)。
Java注解它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类、方法、成员变量等)进行关联。
Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
备注
注解就是告诉编译器, 这段代码是干嘛的
应用
提供信息给编译器:注解可以提供一些额外的信息给编译器,使得编译器在编译过程中可以对代码进行检查或处理。例如,@Override注解用于指示方法应该覆盖父类中的方法,如果没有正确覆盖,编译器会产生警告或错误。
编译时代码生成:注解可以用于在编译时生成代码。例如,Lombok库使用注解来生成getter、setter和构造函数等代码。通过这种方式,开发者可以减少样板代码的编写,提高代码的可读性和可维护性。
运行时处理:注解可以在运行时被读取和处理,从而改变程序的行为。许多框架和库使用注解来配置和管理程序的运行时行为。例如,Spring框架中的@Autowired注解用于自动注入依赖,而JPA(Java Persistence API)使用注解来定义实体类和表之间的映射关系。
代码文档和辅助:注解可以作为一种在代码中提供额外信息的方式,帮助开发者了解代码的用途和行为。这可以提高代码的可读性和可维护性。例如,JUnit测试框架使用@Test注解来标识测试方法。
分类
常用注解
@Override:子类重写父类的方法
@Autowired:自动创配,IOC容器将对象注入到使用的代码里
备注
如果一个类没有加@Conponent注解,就不会被加入到IOC容器里,当然也不能通过@Autowired注入到代码中,不然会报错
可以通过@Autowired(required = false)注入到代码里
这样编写代码的时候不会报错,但执行方法的时候会报错
@Validated:开启校验
@Valid:级联校验,当对象参数的类里,有其他对象的属性,就需要在级联的对象上添加@Valid,这样才能校验级联对象里的属性
@RestController:复合注解,包含@Controller和@ResponseBody
@Controller
Service
@Component:类上加了这个注解,这个类就会被SpringBoot扫描后加入到容器里,在需要的时候注入到其他类中
@Configuration:配置类,常与@bean一起用。可替换xml配置文件,被注解的类内部包含有一个或 多个 被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
@SpringBootApplication:表明当前类是SpringBoot项目的启动类
@ConfigurationProperties:用于将外部配置属性绑定到java类中的字段或方法上。通过使用该注解,可以方便地从配置文件(exception-code.properties或application.yml)获取配置
项目参考地址:misscmszb项目下的FileConfiguration文件
package com.zb.misscmszb.module.file;
import com.zb.misscmszb.common.factory.YamlPropertySourceFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "cms.file")
@PropertySource(value = "classpath:com/zb/misscmszb/extension/file/config/config.yml", encoding = "UTF-8", factory = YamlPropertySourceFactory.class)
@Component
public class FileConfiguration {
private static final String[] DEFAULT_EMPTY_ARRAY = new String[0];
private String storeDir = "/assets";
private String singleLimit = "2MB";
private Integer nums = 10;
private String domain;
private String[] exclude = DEFAULT_EMPTY_ARRAY;
private String[] include = DEFAULT_EMPTY_ARRAY;
/**
* 文件存储路径
*/
private String servePath = "assets/**";
public String getServePath() {
return servePath;
}
public void setServePath(String servePath) {
this.servePath = servePath;
}
public String getStoreDir() {
return storeDir;
}
public void setStoreDir(String storeDir) {
this.storeDir = storeDir;
}
public String getSingleLimit() {
return singleLimit;
}
public void setSingleLimit(String singleLimit) {
this.singleLimit = singleLimit;
}
public Integer getNums() {
return nums;
}
public void setNums(Integer nums) {
this.nums = nums;
}
public String[] getExclude() {
return exclude;
}
public void setExclude(String[] exclude) {
this.exclude = exclude;
}
public String[] getInclude() {
return include;
}
public void setInclude(String[] include) {
this.include = include;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
}
YamlPropertySourceFactory 重新指定属性源
package com.zb.misscmszb.common.factory;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.List;
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
return sources.get(0);
}
}
备注
默认情况下,我们只能获取properties里的配置,如果想获取yml里的配置,就需要在文件上添加@PropertySource注解,重新指定factory
PropertySourceFactory是Spring框架中的一个接口,用于自定义加载和解析外部属性源(property source)的策略
PropertySourceFactory接口定义了一个方法org.springframework.core.env.PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException,该方法根据给定的属性源名称和资源来创建PropertySource对象。
自定义PropertySourceFactory时,需要实现PropertySourceFactory接口,并重写createPropertySource方法。在这个方法中,你可以根据自己的需求,加载和解析外部属性源,并将其包装成PropertySource对象返回。
元注解
是用于修饰注解的注解,通常用在注解的定义上
@Retention: 作用是定义被它所注解的注解保留多久,一共有三种策略,定义在RetentionPolicy枚举中.
source:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;被编译器忽略
class:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期
runtime:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
这3个生命周期分别对应于:Java源文件(.java文件) ---> .class文件 ---> 内存中的字节码。
@Documented:生成文档信息的时候保留注解,对类作辅助说明
@Target:指明了修饰的这个注解的使用范围,即把被修饰的注解用在哪里
ElementType的取值包含以下几种:
TYPE:类,接口或者枚举
FIELD:域,包含枚举常量
METHOD:方法
PARAMETER:参数
CONSTRUCTOR:构造方法
LOCAL_VARIABLE:局部变量
ANNOTATION_TYPE:注解类型
PACKAGE:包
@Constraint:自定义注解的时候用,表示具体的实现类是哪个
自定义注解
自己根据业务需求,定义的注解
具体的可以查看自定义密码校验注解