为什么当我将 class 调用到 javabean 中时,java 反射不起作用?
Why when i call my class into a javabean the java reflection dosn't work?
我尝试用 java 反射显示我所有的包名称,当我用 main 方法测试时它工作得很好:
获取class中名为test1.java的所有包名称的方法:
public Set<String> findAllPackages(String packagebase) {
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false),
new ResourcesScanner())
.setUrls(
ClasspathHelper.forClassLoader(classLoadersList
.toArray(new ClassLoader[0])))
.filterInputsBy(
new FilterBuilder().include(FilterBuilder
.prefix(packagebase))));
Set<Class<? extends Object>> classes = reflections
.getSubTypesOf(Object.class);
Set<String> packageNameSet = new TreeSet<String>();
for (Class classInstance : classes) {
packageNameSet.add(classInstance.getPackage().getName());
}
return packageNameSet;
}
我的主要方法在同一个class:
public static void main(String[] args) throws ClassNotFoundException,
IOException {
for (String pack : new test1().findAllPackages("org.basepackage")) {
System.out.println(pack);
}
}
当我尝试将我的方法放入我的 managedbean 中或者只是将我的 test1.java class 实例化到我的 menagedBean 中时,它也不起作用,当我调试时,我发现它没有成功进入 findAllPackages 的循环。
我的豆子看起来像:
@Named
@ViewScoped
public class mymanagedbean implements Serializable {
private List<SelectItem> packageNameItems;
@PostConstruct
public void init() {
try {
packageNameItems=new ArrayList<SelectItem>();
packageNameItems=(new test1().loadpackageName("org.basepackage"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我在那个 managedbean 中有 packageNameItems 的 getter 和 setter
我的 jsf 看起来像:
<h:form>
<h:selectOneMenu value="#{mymanagedbean.packageName}">
<f:selectItems value="#{mymanagedbean.classNameItems}"/>
</h:selectOneMenu>
</h:form>
你有什么想法吗?
我解决了这个问题:
public Set<String> findAllPackages(String packagebase) {
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false),
new ResourcesScanner())
.setUrls(
ClasspathHelper.forClassLoader(classLoadersList)))
.filterInputsBy(
new FilterBuilder().include(FilterBuilder
.prefix(packagebase))));
Set<Class<? extends Object>> classes = reflections
.getSubTypesOf(Object.class);
Set<String> packageNameSet = new TreeSet<String>();
for (Class classInstance : classes) {
packageNameSet.add(classInstance.getPackage().getName());
}
return packageNameSet;}
我尝试用 java 反射显示我所有的包名称,当我用 main 方法测试时它工作得很好:
获取class中名为test1.java的所有包名称的方法:
public Set<String> findAllPackages(String packagebase) {
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false),
new ResourcesScanner())
.setUrls(
ClasspathHelper.forClassLoader(classLoadersList
.toArray(new ClassLoader[0])))
.filterInputsBy(
new FilterBuilder().include(FilterBuilder
.prefix(packagebase))));
Set<Class<? extends Object>> classes = reflections
.getSubTypesOf(Object.class);
Set<String> packageNameSet = new TreeSet<String>();
for (Class classInstance : classes) {
packageNameSet.add(classInstance.getPackage().getName());
}
return packageNameSet;
}
我的主要方法在同一个class:
public static void main(String[] args) throws ClassNotFoundException,
IOException {
for (String pack : new test1().findAllPackages("org.basepackage")) {
System.out.println(pack);
}
}
当我尝试将我的方法放入我的 managedbean 中或者只是将我的 test1.java class 实例化到我的 menagedBean 中时,它也不起作用,当我调试时,我发现它没有成功进入 findAllPackages 的循环。
我的豆子看起来像:
@Named
@ViewScoped
public class mymanagedbean implements Serializable {
private List<SelectItem> packageNameItems;
@PostConstruct
public void init() {
try {
packageNameItems=new ArrayList<SelectItem>();
packageNameItems=(new test1().loadpackageName("org.basepackage"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我在那个 managedbean 中有 packageNameItems 的 getter 和 setter
我的 jsf 看起来像:
<h:form>
<h:selectOneMenu value="#{mymanagedbean.packageName}">
<f:selectItems value="#{mymanagedbean.classNameItems}"/>
</h:selectOneMenu>
</h:form>
你有什么想法吗?
我解决了这个问题:
public Set<String> findAllPackages(String packagebase) {
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false),
new ResourcesScanner())
.setUrls(
ClasspathHelper.forClassLoader(classLoadersList)))
.filterInputsBy(
new FilterBuilder().include(FilterBuilder
.prefix(packagebase))));
Set<Class<? extends Object>> classes = reflections
.getSubTypesOf(Object.class);
Set<String> packageNameSet = new TreeSet<String>();
for (Class classInstance : classes) {
packageNameSet.add(classInstance.getPackage().getName());
}
return packageNameSet;}