Beans that are instantiated using "new" are not managed by spring and @Resource (or any other annotation) is not processed.
To solve that, you have 2 solutions:
The easy one is to put the resources in MyObjectCreatorImpl, and inject them in MyImpl1 or MyImpl2 using a setter
Code:
public class MyObjectCreatorImpl implements MyObjectCreator () {
@Resource(name="resource1")
private Resource1 resource1;
@Resource(name="resource2")
private Resource2 resource2;
public MyImpl getMyImpl() {
MyImpl myImpl = null;
if (some condition) {
myImpl = new MyImpl1(); // MyImpl1 implements MyImpl
} else {
myImpl = new MyImpl2(); //MyImpl2 implements MyImpl
}
myImpl.setResource1(resource1);
myImpl.setResource2(resource2);
return myImpl;
}
}
public class MyImpl1 implements MyImpl {
@Resource(name="resource1")
private Resource1 resource1;
@Resource(name="resource2")
private Resource2 resource2;
//getters and setters
public somereturntype doSomething() {
....
......
}
}
The difficult one is to use AspectJ and annotate the classes MyImpl1 and MyImpl2 with @Configurable (See "8.8 Using AspectJ with Spring applications" in spring documentation)