Blog Archives

Looking up spring beans defined in java configuration classes


Recently I got rid of XML files in Spring app used for configuration by using annotated classes. A basic issue -how to “lookup” these instance if required, took me some time. The good old way with XML

AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/application-context-root.xml");

It’s still simple, use a different class with configuration class as argument

ApplicationContext context = new AnnotationConfigApplicationContext(RootConfig.class);
sessionManager = (SessionManager) context.getBean("sessionManager");