Announcement Announcement Module
No announcement yet.
calling spring based API from non spring java API Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • calling spring based API from non spring java API

    I have developed a spring 3.1 based API (maven Java jar) which I need to invoke from a non spring API (java jar). The non spring API is in our app server classpath and used by multiple existing applications.

    Suppose in my Spring jar I have XService (which is interface) and XServiceImpl (which is the implementation) and I want to invoke XService methods in my non spring API, how can I do that?

    While researching on web, I found two approach - one is to have a Class implementing ApplicationContextAware in my Spring API

    public class SpringContext implements ApplicationContextAware {
    private static ApplicationContext context;

    public void setApplicationContext(ApplicationContext context) throws BeansException {
    this.context = context;
    public static ApplicationContext getApplicationContext() {
    return context;

    and then in non spring api class (XServiceDelegate) have the following code -

    XService service = (XService)SpringContext.getApplicationContext.getB ean(XService.class);

    The other approach is to instantiate the ApplicationContext with new operator (possibly ClasspathApplicationContext) and then get the XService from the context.

    ApplicationContext ctx = new ClassPathXmlApplicationContext("scripting/beans.xml");
    XService service = (XService) ctx.getBean(XService.class);

    Also found reference of use of ContextSingletonBeanFactoryLocator but not very clear about what exactly its is the link..

    BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
    BeanFactoryReference bf = bfl.useBeanFactory("com.mycompany.myapp");
    MyClass zed = bf.getFactory().getBean("mybean");

    Please let me know what is the best way to implement this keeping in mind the non spring methods can be unit testable with mocking the spring services (I ma using EasyMock).
    In the above solutions, how I can inject my XService to XServiceDelegate and facilitate proper unit testing and do the same (inject XserviceDelegate to its calling client)?


  • #2
    Gurus, any advise please..