简化代码,提高开发效率
注解分类
一、运行机制划分:
- 源码注解
- 编译时注解 (1.内的注解都是)
只在编译器上起作用 - 运行时注解
运行阶段起作用,影响运行逻辑,例如@Autowired
二、按照来源划分:
1.jdk自带注解
@Override 复写父类,接口方法
@Depreted 标记过时
@SuppressWarning(“deprecation”) 忽略警告
2.java第三方注解
Spring注解
参考文章 作者:陈袁
SpringBoot注解
作者 tanwei81 博客园
3.自定义注解
//使用@interface定义注解
// CONSTRUCTOR:构造方法声明 FIELD:字段 LOCAL_VARIABLE:局部变量
// METHOD:方法 PACKAGE:包 PARAMETER:参数 TYPE:接口.类
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD,ElementType.ANNOTATION_TYPE})
// 存在范围 source:编译时忽略掉
@Retention(RetentionPolicy.RUNTIME)
@Inherited//允许子类(子注解)继承,对接口继承没有作用[extends]
@Documented//生成javadoc会包含注解信息
public @interface Description{
//成员变量以无参无异常的方式声明
//成员类型受限于8种基本类型和String,Class,Annotation,Enumeration
String desc();
String author();
int age() default 18;
}
//只有一个成员变量时,名称必须为value(),使用时可忽略'='或成员名
public @interface Description{
String value();
}
//没有成员的注解成为标识注解
public @interface Description{
}
8种基本类型: byte,short,int,long,float,double,char,boolean
元注解: 给注解进行注解
// CONSTRUCTOR:构造方法声明 FIELD:字段 LOCAL_VARIABLE:局部变量
// METHOD:方法 PACKAGE:包 PARAMETER:参数 TYPE:接口.类
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD,ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited//注解会被子类继承
public @interface Autowired {
/**
* Declares whether the annotated dependency is required.
* <p>Defaults to {@code true}.
*/
boolean required() default true;
}
解析注释
try{
//1.找到类上的注解,反射加载类
Class c = Class.forName("com.zjy.entity.Child");
//2.找到类上面的注解
boolean isExist = c.isAnnotationPresent(Description.class);
if(isExist){
//3.存在注解实例,取出
Description d = (Description)c.getAnnotation(Description.class);
System.out.println(d.value());
}
//1.找到方法上的注解
Method[] ms = c.getMethods();
for(Method m : ms){
boolean isExist = m.isAnnotationPresent(Description.class);
if(isExist){
//2.存在注解实例,取出
Description d = (Description)c.getAnnotation(Description.class);
System.out.println(d.value());
}
}
//1.找到方法所有注解
for(Method m : ms){
Annotation[] as = m.getAnnotations();
for(Annotation a: as){
if(a instanceod Description){
Description d = (Description)a;
System.out.println(d.value());
}
}
}
}catch(ClassNotFoundException e){
logger.error("",e);
}
实例
import org.springframework.stereotype.Component;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component//作为左键能被spring扫描到
public @interface SQLFileAnnotation {
String SQLFileName();
}
主方法内扫描注解
public static void scannerBeans(){
//扫描SQL文件注解
String[] sqlFiles = applicationContext.getBeanNamesForAnnotation(SQLFileAnnotation.class);
List<String> sqlFileNameList = new ArrayList<>();
for(String string:sqlFiles){
Object sqlFile = applicationContext.getBean(string);
String sqlFileName = sqlFile.getClass().getAnnotation(SQLFileAnnotation.class).SQLFileName();
sqlFileNameList.add(sqlFileName);
}
executeSql(sqlFileNameList);
}
private static void executeSql(List<String> sqlFileNameList){
Collections.sort(sqlFileNameList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(o1.contains("-")){
o1 = o1.substring(o1.indexOf("-") + 1,o1.length());
}
if(o2.contains("-")){
o2 = o2.substring(o2.indexOf("-") + 1,o2.length());
}
return o1.compareTo(o2);
}
});
for(String name : sqlFileNameList){
String importedDBs = getImportedDBs();
if(importedDBs.contains(name)){
logger.debug("数据库已导入,不再重复导入!!");
continue;
}
boolean isSuccess = false;
try {
String content = "";
try {
content = IOUtils.toString(new FileInputStream(PathUtil.getPath(PathUtil.PathType.CONFIG)+ "sqlitemanagerTool.sql"),"utf-8");
} catch (IOException e) {
e.printStackTrace();
}
isSuccess = JdbcUtil.getInstance().insertSqlArray(content.split("#970126#"));
} catch (Exception e) {
e.printStackTrace();
}
if(isSuccess){
updateImportedDB(importedDBs + "\r\n" + name);
}
}
}
private static String getImportedDBs(){
StringBuilder importedDB=new StringBuilder();
try {
File file =new File(PathUtil.getPath(PathUtil.PathType.CONFIG) + "dbImport.txt");
if(!file.exists()){
file.createNewFile();
}
FileInputStream in=new FileInputStream(file);
byte[] b=new byte[1024];
int i;
while((i=in.read(b))>0){
importedDB.append(new String(b,0,i));
}
in.close();
}catch (Exception e){
e.printStackTrace();
}
return importedDB.toString();
}
private static void updateImportedDB(String string){
try {
File file =new File(PathUtil.getPath(PathUtil.PathType.CONFIG) + "dbImport.txt");
FileOutputStream out =new FileOutputStream(file);
byte[] newb = string.getBytes();
out.write(newb);
out.close();
}catch (Exception e){
e.printStackTrace();
}
}