I'm quite new to spring (switching from PHP to Java).
In my code I have a java.lang.reflect.Method objects and I need to instantiate it's class with all it's dependencies.
Normally I'd use @Autowired annotation in my code, but it's not possible because my code gets different Method objects, not specific classes.
Question is - how to get a class instance from dependency container without using annotations and having just class name?
In php i used libraries which gave me access to container and I could just get DI services by it's class name just like:
$this->container->get('My\Class\Name'); In spring I tried:
@Autowired private ApplicationContext context; void myMethod(Method method){ this.context.getBean(method.getClass()); and
this.context.getBean(method.getClass().getName()); and that was resulting in NullPointerException.
EDIT Thanks for quick replys,
I tried using
context.getBean(method.getDeclaringClass()); and
context.getBean(method.getDeclaringClass().getSimpleName()); And it both resulted in NullPointerException as well.
Actually it's okay for my needs to get that class by class or by name. I'm trying to write my own command bus for CQRS. Let me show you some code:
Handler:
public class SimpleCommandBus implements CommandBus { @Autowired private ApplicationContext context; private Map<Class, Method> registry = new HashMap<>(); @Override public void register(Class c, Method o) { registry.put(c, o); } @Override public void dispatch(Object command) { if (!registry.containsKey(command.getClass())) { throw new CommandDispatchException(String.format("Handler for command %s was not defined.", command)); } Method method = registry.get(command.getClass()); Object handler = context.getBean(method.getDeclaringClass().getSimpleName());//line causing exception Service class:
@Service public class TestHandler { public void handle(TestCommand command){ System.err.println(command.getId()); } } Calling command bus:
Method method = TestHandler.class.getMethod("handle", TestCommand.class); TestCommand command = new TestCommand("Test command"); commandBus.register(TestCommand.class, method); commandBus.dispatch(command);