Using the Application context in SpringFrom WikiJavabuy this book
This article shows how to use the Spring Framework in it's most basic usage. We use the
Why is this useful?Imagine this situation: you want to develop a program of a decent size. This program is made of different components, which are independent and collaborate to the processing overall. You can't develop all the components at the same time, but once you finish the first component you're going to need some dummy behaviour from the other components to test it. What you can do is to create dummy implementations of the other components and wire them using the dependency injection from the Spring Framework. These dummy components will behave pretty much as the component you implemented expects, for example a DAO could return always the same hard coded object. So you can test the component in a pretty much realistic context. Once you're done with the component, you can start working on the second one and wire it in the Another situation where you can smoothly use Spring beans is when you have an application which has configurable components. You can deploy the different components by just wiring them in the application context. For example you may have a software which supports the mySql Database, but you want to deploy it on a system with Oracle. You can simply reimplement the interface creating new DAO's that support Oracle, then just wire them in the application context instead of the MySql DAO's. If you need to switch back to MySql, just rewire the old DAO's and you're done. You don't even need to recompile. the BatBotResourceFactory ClassFor this application I created a Factory class which is also a Singleton called I did this to have a unique place to access the Spring beans, since I consider the beans provided by spring like external resources I don't want my software to be too much dependent on the framework. In the whole program there are no links to Spring, so with this architecture I can easily completely remove the spring framework from my application by just reimplementing the
How to get the singletonThe Singleton is simply implemented in the private static BatBotResourceFactory batBotResourceFactory; public synchronized static BatBotResourceFactory getSingleton() { if (null == batBotResourceFactory) { batBotResourceFactory = new BatBotResourceFactory(); } return batBotResourceFactory; } private BatBotResourceFactory() { context = new FileSystemXmlApplicationContext(APPLICATION_CONTEXT_FILE); } Note that the constructor for the class is declared This is the most normal lazy Singleton in the world.
Initializing the Application ContextBefore using the application context we need to instantiate it via the This expensive operation is the reason why I created the factory method and made is as a Singleton. private ApplicationContext context; private static final String APPLICATION_CONTEXT_FILE = "applicationContext.xml"; private BatBotResourceFactory() { context = new FileSystemXmlApplicationContext(APPLICATION_CONTEXT_FILE); } obtaining the BeansOnce the All you need to do is to call the Object org.springframework.beans.factory.BeanFactory.getBean(String name) throws BeansException The method is used as follows: public FileHelperI getFileHelper() { return (FileHelperI) context.getBean("fileHelperBean"); }
the Full code for the class
/** * This source file is part of BatBot, * Copyright (C) 2008 Giulio Giraldi * Find more details on the BatBot Project at: * http://www.wikijava.org/wiki/batBot:Main_page * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package org.wikijava.network.batBot.delegates; import java.util.logging.Logger; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.wikijava.network.batBot.delegates.interfaces.ActionExecutorI; import org.wikijava.network.batBot.delegates.interfaces.FileHelperI; import org.wikijava.network.batBot.delegates.interfaces.HttpHelperI; public class BatBotResourceFactory { private transient static Logger log = Logger .getLogger(BatBotResourceFactory.class.getName()); private static final String APPLICATION_CONTEXT_FILE = "applicationContext.xml"; ApplicationContext context; private static BatBotResourceFactory batBotResourceFactory; public static BatBotResourceFactory getSingleton() { if (null == batBotResourceFactory) { batBotResourceFactory = new BatBotResourceFactory(); } return batBotResourceFactory; } private BatBotResourceFactory() { context = new FileSystemXmlApplicationContext(APPLICATION_CONTEXT_FILE); } public FileHelperI getFileHelper() { return (FileHelperI) context.getBean("fileHelperBean"); } public HttpHelperI getHttpHelper() { return (HttpHelperI) context.getBean("httpHelperBean"); } public ActionExecutorI getActionExecutorI() { return (ActionExecutorI) context.getBean("ActionExecutorBean"); } } the application context file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="fileHelperBean" class="org.wikijava.network.batBot.delegates.XStreamFileHelper"> </bean> <bean id="httpHelperBean" class="org.wikijava.network.batBot.delegates.HttpHelper"> </bean> <bean id="ActionExecutorBean" class="org.wikijava.network.batBot.delegates.ActionExecutor"> </bean> </beans> |

I'm plannin' to learn Spring and your article is great. Very well written.
Would you please explain me better the uses of the application context file?
--Alcor 16:30, 2 January 2009 (UTC)