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" />