0

I have a Java (Maven) pkg with an interface (IHelloWorld) and implementation (HelloWorld)

I have confirmed that I am able to consume the package with and without Spring IoC, but app is crashing when I try to use auto-wiring

What am I doing wrong?

external package:

package com.example.library;

public class HelloWorld implements IHelloWorld
{
    private String _name;

    public void setName(String name)
    {
        this._name = name;
    }

    public String getName()
    {
        return this._name;
    }

    public String greet()
    {
        return "Hello " + this._name + "!";
    }
}

In my consuming test project, I can manually create the object and can also use "old style" bean wiring like

config.xml:

<bean id="talk" class="com.example.library.HelloWorld">
    <property name="name" value="#{systemProperties['usr']}" />
</bean>

Context helper class:

package com.example.demo;

import org.springframework.context.ApplicationContext;

public final class IoCHelper
{
    private static ApplicationContext _context;

    private IoCHelper() { }

    public static void setContext(ApplicationContext context) { _context = context; }

    public static ApplicationContext getContext() { return _context; }
}

main:

IoCHelper.setContext(new ClassPathXmlApplicationContext("myApp.xml"));

test class:

package com.example.demo;

import com.example.library.IHelloWorld;

public class SomeClass
{
    private IHelloWorld _myObj;

    public void initTest()
    {
        Object obj = IoCHelper.getContext().getBean("talk");

        this._myObj = (IHelloWorld)obj;
    }

    public IHelloWorld getTest() { return this._myObj; }
}

controller:

@RequestMapping("/test2")
public String TestMe2()
{
    SomeClass x = new SomeClass();

    x.initTest();
    return x.getTest().greet();
}

When I add @Component and @Autowired to my test class "SomeClass", my app fails with error

Field _myObj in com.example.demo.SomeClass required a bean of type 'com.example.library.IHelloWorld' that could not be found.

@Component
public class SomeClass
{
    @Autowired
    private IHelloWorld _myObj;

I have tried with both in the config xml, but neither has worked

<context:annotation-config />
<context:component-scan base-package="com.example.library,com.example.demo" />
Jason
  • 3,844
  • 1
  • 21
  • 40
  • @JB Nizet - in the question you are pointing to, the op created a new `MileageFeeCalculator` that he/she expects to be injected/available in Spring. However I am not instantiating a new `HelloWorld` anywhere. In fact I can do context.getBean(...) to get bean from Spring. Can you tell me how this is a duplicate? – Jason Feb 24 '19 at 22:56
  • You're wondering why _myObj, autowired in SomeClass, is null. How is the instance of SomeClass created? `new SomeClass()`. You're doing the exact same mistake as in the linked question. – JB Nizet Feb 24 '19 at 22:58
  • @JB Nizet - got it now, so what is the recommended way of using "SomeClass"? auto-wire SomemClass to my Controller, e.g. auto-wire all the way? – Jason Feb 24 '19 at 23:04
  • Yes. Spring is a dependency injection container. You inject your dependencies. Why are you still using XML, BTW? It's been years since Java config is the recommended way. – JB Nizet Feb 24 '19 at 23:06
  • @JB Nizet - new to Java / Spring, inheriting project, .NET dev by trade. Thank you for the pointers :) – Jason Feb 24 '19 at 23:09

0 Answers0