我试过寻找答案,但没有找到任何答案。我想利用一个现有的 JAR,它在包的几个部分下有几个类。这些类具有 JAX-RS 注释,因此我希望使用 CXF 来加载所有这些类并将它们连接为 CXF 端点。
CXF 是否可以将其指向一个包并将所有类连接到端点?
据我所知CXF
,不会从一些jar
带有注释的类中加载一些类。您需要手动执行此操作。例如,要获取使用特定注释注释的类,您可以使用以下命令:
public class AnnotationHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationHandler.class);
/**
* Scans all classes accessible from the context class loader which belong to the given package and sub packages.
*
* @param packageName the base package
* @return The classes
* @throws ClassNotFoundException if class not found exception occurs
* @throws IOException if IO error occurs
*/
public Iterable<Class> getClasses(String packageName)
throws ClassNotFoundException, IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
LinkedList<File> dirs = new LinkedList<File>();
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
LinkedList<Class> classes = new LinkedList<Class>();
for (File directory : dirs) {
classes.addAll(findClasses(directory, packageName));
}
return classes;
}
/**
* Recursive method used to find all classes in a given directory and sub directories.
*
* @param directory the base directory
* @param packageName the package name for classes found inside the base directory
* @return the classes
* @throws ClassNotFoundException if class not found exception occurrs
*/
private LinkedList<Class> findClasses(File directory, String packageName)
throws ClassNotFoundException {
LinkedList<Class> classes = new LinkedList<Class>();
if (!directory.exists()) {
return classes;
}
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
classes.addAll(findClasses(file, packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) {
classes.add(Class.forName(
packageName + '.'
+ file.getName().substring(0, file.getName().length() - 6)));
}
}
}
return classes;
}
/**
* Finds all classes annotated with passed annotation in provided package. Unchecked system exception might be
* thrown if the class is not found or IO exception occurs.
*
* @param annotationClass annotation class
* @param packageName package name to search for annotated classes
* @return list of annotated class with specified annotation
*/
public LinkedList<Class> findAnnotatedClasses(Class annotationClass, String packageName) {
LinkedList<Class> classes = new LinkedList<Class>();
try {
for (Class clazz : getClasses(packageName)) {
if (clazz.isAnnotationPresent(annotationClass)) {
classes.add(clazz);
}
}
} catch (ClassNotFoundException ex) {
LOGGER.error("Class not found exception occurred.", ex);
throw new SystemException("Class not found exception occurred.", ex);
} catch (IOException ex) {
LOGGER.error("IO exception occurred.", ex);
throw new SystemException("IO exception occurred.", ex);
}
return classes;
}
}
通过提供注解类和包名进行调用findAnnotatedClasses
,您将获得一个使用指定注解注解的类列表。
然后你可以用这些类做任何你想做的事情。