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");
About these ads

About ChandanPandey

Try to come up with a good design as by product of good coding practices

Posted on July 26, 2013, in Java, Spring and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 32 other followers

%d bloggers like this: