我已经尝试过搜索答案,但还没有想出任何答案。我想利用一个现有的JAR,它在一个包的几个部分下面有几个类。这些类具有JAX-RS注释,因此我希望使用CXF加载所有这些类并将它们连接为CXF端点。
CXF是否可以将其指向包并将所有类连接到端点?
答案 0 :(得分:1)
据我所知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
,您将获得使用指定注释注释的类列表。
然后你可以用这些课做你想做的事。