@Retention(RetentionPolicy
.RUNTIME
)
public @
interface Luban {
public String
value();
}
@Luban("bb")
public class UserService {
public void find(){
System
.out
.println(this.getClass().getResource("/").getPath());
}
@Test
public void test(){
System
.out
.println(this.getClass().getResource("/").getPath());
}
}
public class AnnotationConfigApplicationContex {
public void scan(String basePackage
){
String rootPath
= this.getClass().getResource("/").getPath();
System
.out
.println(rootPath
);
String basePackagePath
=basePackage
.replaceAll("\\.","\\\\");
System
.out
.println(basePackagePath
);
File file
= new File(rootPath
+"//"+basePackagePath
);
System
.out
.println(rootPath
+ "//" + basePackagePath
);
System
.out
.println("--------");
String
[] names
= file
.list();
for(String name
:names
){
name
= name
.replaceAll(".class","");
try {
Class
clazz = Class
.forName(basePackage
+"."+name
);
if(clazz
.isAnnotationPresent(Luban
.class)){
Luban luban
= (Luban
)clazz
.getAnnotation(Luban
.class);
System
.out
.println(luban
.value());
System
.out
.println(clazz
.newInstance());
}
} catch (Exception e
) {
e
.printStackTrace();
}
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-500182.html