简体   繁体   中英

Loading beans from jar with dependency

I have a scenario where in I need to simulate the following-

Say I have a project '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'. Not only this, if classA has a dependency on classB, even classB should get loaded.

The current application context and the new classes to be loaded should typically be running in two different JVM containers.

I've gone about the problem in this way-

  1. LoadingBeans contains-

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

<?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

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

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

<?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'. On calling the 'get' function in Message.java, HelloWorld and its JSON dependencies should get loaded.

The HelloWorld class is loaded on Calling 'get' on Message.java but the JSON dependencies don't get loaded. 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? 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.

Based on your code though, since beans.xml is identical to your loaded externalBeans.xml why you need to replicate it? You can very well refer to it via your main method and beans.xml , classA and classB all come from the same 'external' jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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