简体   繁体   English

从罐子中依赖加载bean

[英]Loading beans from jar with dependency

I have a scenario where in I need to simulate the following- 我有一个需要模拟以下内容的场景:

Say I have a project 'LoadingBean'. 说我有一个项目“ LoadingBean”。 I need to load classA (from project 'External' as a JAR added to classpath of 'Loading Bean') after the application context has been loaded for 'LoadingBean'. 在为“ LoadingBean”加载了应用程序上下文之后,我需要加载classA(从项目“ External”作为JAR添加到“ Loading Bean”的类路径)。 Not only this, if classA has a dependency on classB, even classB should get loaded. 不仅如此,如果classA依赖于classB,那么即使classB也应加载。

The current application context and the new classes to be loaded should typically be running in two different JVM containers. 当前应用程序上下文和要加载的新类通常应在两个不同的JVM容器中运行。

I've gone about the problem in this way- 我已经通过这种方式解决了这个问题-

  1. LoadingBeans contains- LoadingBeans包含-

Main.java Main.java

package com.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        Object obj = context.getBean("MessageBean");
            System.out.println(obj);
    }
}

Beans.xml beans.xml中

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <bean id="MessageBean" class="com.bean.Message">
        <property name="msg" value="Hello World!!!"/>
    </bean>

    <bean id="HelloWorldBean" class="com.bean.HelloWorld">
        <property name="message" value="Howdie World!!!"/>
    </bean>
</beans>
  1. 'External' contains- “外部”包含-

Message.java Message.java

package com.bean;

import org.springframework.beans.factory.annotation.Autowired;

public class Message {

    @Autowired
    private HelloWorld hw;

    private String msg;

    public HelloWorld getHw() {
        return hw;
    }

    public void setHw(HelloWorld hw) {
        this.hw = hw;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

HelloWorld.java HelloWorld.java

package com.bean;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class HelloWorld {
    private String message;

    public String getMessage() {
    try {
            return new JSONObject(message).toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

ExternalBeans.xml ExternalBeans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <bean id="MessageBean" class="com.bean.Message">
        <property name="msg" value="Hello World!!!"/>
    </bean>

    <bean id="HelloWorldBean" class="com.bean.HelloWorld">
        <property name="message" value="Howdie World!!!"/>
    </bean>
</beans>

I have created a simple JAR out of this and included it in the classpath for 'LoadingBeans'. 我由此创建了一个简单的JAR,并将其包含在“ LoadingBeans”的类路径中。 On calling the 'get' function in Message.java, HelloWorld and its JSON dependencies should get loaded. 在Message.java中调用“ get”函数时,应加载HelloWorld及其JSON依赖项。

The HelloWorld class is loaded on Calling 'get' on Message.java but the JSON dependencies don't get loaded. HelloWorld类已在Message.java上调用“ get”加载,但JSON依赖项未加载。 Is there a workaround for it? 是否有解决方法? Is there any other thing that I need to take care of for checking this scenario? 检查此场景是否还有其他需要注意的事项? I don't need to use a 'runnable JAR' right? 我不需要使用“可运行的JAR”吧? Since theres no main method in 'External'. 由于“外部”中没有主要方法。

Your question is a bit complicated and I cannot make a connection of the quoted code and the JSON specific information you are mentioning. 您的问题有点复杂,我无法将引用的代码与您提到的JSON特定信息联系起来。

Based on your code though, since beans.xml is identical to your loaded externalBeans.xml why you need to replicate it? 但是根据您的代码,由于beans.xml与加载的externalBeans.xml相同,为什么需要复制它? You can very well refer to it via your main method and beans.xml , classA and classB all come from the same 'external' jar. 您可以通过主要方法很好地引用它, beans.xmlclassAclassB都来自同一个“外部” jar。

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

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