??????UI??????????У?????????????????ν??PO???????е?P?????page?????????????????????????????????page?????new????page???????????鷳????????????????????????????????????????????????????????new???????????????????а?????????springMVC??????????????IOC???????????????????springMVC????????????????????ж????????????????????????£????????????????????????????????£?
????1.????????LoadPage?????
????package com.test.annotation;
????import java.lang.annotation.ElementType;
????import java.lang.annotation.Retention;
????import java.lang.annotation.RetentionPolicy;
????import java.lang.annotation.Target;
????@Target(ElementType.TYPE)
????@Retention(RetentionPolicy.RUNTIME)
????public @interface LoadPage {
????String value();
????}
????2.????????????????
package com.test.annotation;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.WebDriver;
public class LoadAllPage {
public final String basePath = "com.test";
private final String binPath = "bin";
private List<String> allClass = new ArrayList<String>();
private WebDriver driver;
public void setDriver(WebDriver driver) {
this.driver = driver;
}
public void loadAllPage(){
this.listAllFiles(binPath+File.separator+basePath.replace("."??"/"));
this.getPageInstance();
}
private void listAllFiles(String path){
path = path.replace("\"?? "/");
File file = new File(path);
if(file.isFile() && file.getName().endsWith(".class")){
String filePath = file.getPath().replace("\"?? "/");
int startIndex = 4;
int endIndex = filePath.lastIndexOf(".class");
allClass.add(filePath.substring(startIndex?? endIndex).replace("/"?? "."));
}else if(file.isDirectory()){
File[] files = file.listFiles();
for (File f : files) {
this.listAllFiles(f.getPath());
}
}
}
private void getPageInstance(){
for (String clazz : allClass) {
try {
Class<?> c = Class.forName(clazz);
if(c.isAnnotationPresent(LoadPage.class)){
LoadPage lp = c.getAnnotation(LoadPage.class);
Constructor<?> cons = c.getConstructor(WebDriver.class);
InitialManger.allInstance.put(lp.value()?? cons.newInstance(driver));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
LoadAllPage lap = new LoadAllPage();
lap.loadAllPage();
}
}