添加实现而不用 Java 重新编译?

发布于 2024-08-23 20:23:06 字数 538 浏览 8 评论 0原文

大家好,我是一名大学四年级学生,我有了第一个真正的工作机会(令人兴奋)。我现在正处于他们需要查看编程示例的阶段,他们给了我用 Java 实现随机数生成器服务的任务,有两种不同的实现(一种使用内置的东西,另一种是我选择的) 。代码是简单的部分,但任务的一部分让我感到困惑......这里是:

作为评估者,我应该能够做到以下几点: 使用候选 jar 文件编译我自己的项目。 使用候选可执行 jar 注册我的解决方案。 运行候选可执行 jar,以某种方式告诉它运行我的实现。

基本上我正在将我的代码制作成可执行的 .jar,并且

评估者应该能够使用候选人开发的代码和编译类来插入他们自己的随机数生成器实现,而无需重新编译候选人的代码。

这到底是什么意思?也许我只是错过了一些明显的东西?我不知道如何让他们只需投入自己的实现而不必重新编译所有内容......希望这不是一项太大的任务,因为我在我的大学还没有听说过类似的事情(我认为) 。

非常感谢任何帮助/见解!

Hey everyone, I'm a college senior with my first real job opportunity (exciting). I'm at the stage now where they need to see a programming example, and they gave me the task of implementing a random number generator service in Java, with two different implementations (one using the built-in stuff and another of my choice). The code is the easy part, but one part of the task is confusing me... here it is:

As the evaluator, I should be able to do the following:
Compile my own project with the candidates jar file.
Register my solution with the candidates executable jar.
Run the candidates executable jar, somehow telling it to run my implementation.

Basically I'm making my code into an executable .jar, and

evaluators should be able to use the code and compiled classes developed by the candidate to plug in their own random number generator implementation without recompiling the candidate’s code.

What the heck does that mean? Maybe I'm just missing something obvious? I'm not sure how to allow them to just throw in their own implementation without having to recompile everything... hopefully it's not too big of a task, as I haven't heard of something like that (I think) at my University.

Any help/insight is really appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒几许 2024-08-30 20:23:06

我认为这只是意味着您应该提供一个 RandomNumberGenerationStrategy 接口作为公共 API 的一部分,以便评估器可以实现。

然后提供另一个钩子,他可以通过该钩子注册您的接口的特定实现,然后您可以通过回调调用该实现。

I think it just means that you should provide a RandomNumberGenerationStrategy interface as part of your public API, that the evaluator can implement.

Then provide another hook whereby he can register his particular implementation of your interface, which you then invoke via callback.

辞取 2024-08-30 20:23:06

他们希望您使用 URLClassLoader 加载实现 JAR (查看文档),然后使用反射实例化主类并调用正确的方法来调用随机数生成器。

They want you to load the implementation JAR with an URLClassLoader (see the docs) and then use reflection to instantiate the main class and call the correct method to invoke the random number generator.

舟遥客 2024-08-30 20:23:06

使用 Spring 并利用 ClassPathXmlApplicationContext 通过 Spring 配置交换您的实现。您的 Spring 配置应类似于以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean id="randonNumberGenerator"  class="com.me.MyGenerator"/>

    <!--
    <bean id="randonNumberGenerator"  class="com.someoneelse.ADifferentGenerator"/>
    -->
</beans>

在您的程序中,加载 Spring 上下文并查找 bean。

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
IGenerator generator = applicationContext.getBean("randonNumberGenerator");

// IGenerator in the interface which MyGenerator and ADifferentGenerator implement

请记住,您的 Spring 配置文件应该存在于您的类路径中(而不仅仅是在您的 jar 中),以便可以在运行时更改它而无需重新编译。

Use Spring and leverage ClassPathXmlApplicationContext to swap your implementations via the Spring configuration. Your Spring config should look similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean id="randonNumberGenerator"  class="com.me.MyGenerator"/>

    <!--
    <bean id="randonNumberGenerator"  class="com.someoneelse.ADifferentGenerator"/>
    -->
</beans>

In your program, load your Spring context and look up the bean.

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
IGenerator generator = applicationContext.getBean("randonNumberGenerator");

// IGenerator in the interface which MyGenerator and ADifferentGenerator implement

Remember your Spring configuration file should exist on your classpath (not just inside your jar) so it can be changed at runtime without a recompile.

乖乖公主 2024-08-30 20:23:06

看看 OSGi 框架,其中 Eclipse 使用。例如,Eclipse能够加载新插件并主动将其插入到运行环境中以立即使用(无需重新启动即可应用)。你也可以做同样的事情。

Have a look at the OSGi framework which Eclipse uses. As an example, Eclipse is able to load new plug-ins and actively insert them into the running environment for immediate usage (apply without restarting). You could do exactly the same.

婴鹅 2024-08-30 20:23:06

好吧,也许为了让事情变得尽可能简单,他们没有为您提供类的名称和您应该实现的方法原型吗?我认为这是最合乎逻辑和最直接的方法之一...就像当我们学校的教授给我们一些作业时,他要求我们的程序应该能够与他提供的驱动程序类一起运行,他通常会设置名称我们应该与方法原型一起实现的类...只是一个想法...

Well maybe to make this more simple as possible, didn't they provide you with a name of the class and the method prototypes that you should be implementing? I think this one of the most logical and straightforward way to this... Like when our professor in school give us some assignment, and he requires that our programs should be able to run with the driver classes he provided, he usually sets the name of the classes that we should implement together with the prototypes of the methods... just a thought...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文