简体   繁体   English

如果我知道它的ClassLoader(ClassLoader命名空间),我可以在运行时在Java中获取类的实例吗?

[英]Can I get an instance of a class in Java during runtime if I know its ClassLoader (ClassLoader Namespace)

I am just wondering if I can get an instance of a class (reference to an Object) during JVM runtime if I know its Classloader. 我只是想知道如果我知道它的类加载器,我是否可以在JVM运行时获取类的实例(引用一个对象)。 Please refer to the code bellow to understand my question. 请参阅下面的代码以了解我的问题。

Class A: A类:

package local.run;
public class A {
    public static void main(String[] args) {
        B b = new B();
        b.setCount(5);
        C c = new C();
        c.dummyMethod();
        b.printCount();
    }
}

Class B: B级:

package local.run;
public class B {
    public int count = 0;
    public void setCount(int aCount) {
        count = aCount;
    }
    public void printCount() {
        System.out.println(count);
    }
}

Class C: C级:

package local.run;    
public class C {
    public void dummyMethod() {
        // Can I get the instance of class B created in class A here? 
        // I don't want to pass the instance of class B as a parameter to this method.
        System.out.println("ClassLoader Namespace -> "+B.class.getProtectionDomain().getCodeSource().getLocation());

        // I know the ClassLoader Namespace of class B
        // How to get instance of class B created in class A???
        System.out.println(b.count); // I wan't to print 5
    }
}

If this is not prossible then I am just wondering how JSF implements @ManagedProperty feature? 如果这不可能,那么我只是想知道JSF如何实现@ManagedProperty功能?

No, you cannot get an instance of a give type from a ClassLoader . 不,您无法从ClassLoader获取给定类型的实例。 How would the ClassLoader be able to identify which instance you wanted? ClassLoader如何识别您想要的实例?

You need some other kind of logic. 你需要一些其他的逻辑。 Like Peter, I don't know how JSF does it exactly, so I'll give an example with Spring. 像Peter一样,我不知道JSF是如何完成的,所以我将举一个Spring的例子。

In Spring, which provides a Inversion of Control container, you declare your beans. 在提供Inversion of Control容器的Spring中,您声明了bean。 These are managed by the container, ie. 这些由容器管理,即。 the container stores them and controls their entire life cycle. 容器存储它们并控制它们的整个生命周期。 If you have a field within one of these beans' class declared as 如果您在其中一个bean类中声明了一个字段

@Value("${someBean.someField}")
private String copyValueOfThatField;

then the container is responsible for resolving the expression within the @Value annotation and injecting the corresponding value into the copyValueOfThatField field. 然后容器负责解析@Value注释中的表达式,并将相应的值注入copyValueOfThatField字段。

In this case, it will use some component that goes through all the beans registered in the container, find the one named someBean , and try to retrieve the value of its someField property. 在这种情况下,它将使用一些组件遍历容器中注册的所有bean,找到名为someBean ,并尝试检索其someField属性的值。

You'll have tons of reflection going on in the background to get values of object fields. 您将在后台进行大量反射以获取对象字段的值。

JSF is a container in its own right and probably does something similar to the above. JSF本身就是一个容器,可能与上面的东西类似。 The use of the term Managed should tell you that. 使用“ Managed ”一词应该告诉你。

To get an instance, you have a to have a collection somewhere which retains the instance for lookup. 要获取实例,您需要在某处保留用于查找的实例的集合。 I don't know how JSF work specifically, but usually your framework looks for special annotations it recognises and it holds the instances which are used to set those fields, or it has factories to create the instance. 我不知道JSF是如何具体工作的,但通常你的框架会查找它识别的特殊注释,它包含用于设置这些字段的实例,或者它有工厂来创建实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM