Hi, Personally, I use Spring as my DAO framework. So it seems logical to me to initialize iBATIS in Spring. This allows me to initialize the database either in a web app or on a local app in the same way.
If you want to initialize iBATIS in a web app, I would prefer to use a SerlvetContextListener then a plug-in in Stripes. If you want Stripes to do the job, I think you are very close to the solution. I've just never done that myself. Christian -----Original Message----- From: Ted Schrader [mailto:[email protected]] Sent: Thursday, May 21, 2009 10:23 AM To: [email protected] Subject: [Stripes-users] init on app startup (ibatis) (equivalent of struts1 plugin) Hello all, Newbie question: I'm transitioning from Struts1 to Stripes (hooray!). I'm using iBATIS for DB access, and in the past I've used the Struts1 plugin architecture to initialize the iBATIS sqlmaps instance on application startup. How can I do something similar with Stripes? I suppose the case could be extended to any sort of sub-package initialization like, say, first-time Lucene indexing. I feel like I'm simply missing something silly but perhaps I'm going about it in a totally un-Stripesy way. Below are my feeble attempts. Any advice? Thanks! Ted p.s. Freddy, maybe this would be a good topic for a Stripes Book blog post. web.xml: (snip. sl.ext.CustomActionResolver, sl.ext.MyActionBeanContext, and sl.ext.MyExceptionHandler [per Stripes book] are being discovered, by the way) ----------------------------------------- <init-param> <param-name>Extension.Packages</param-name> <param-value>sl.ext</param-value> </init-param> IBATIS.java: ----------------------------------------- package sl.ext; import java.io.Reader; import org.apache.log4j.Logger; import net.sourceforge.stripes.config.ConfigurableComponent; import net.sourceforge.stripes.config.Configuration; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; /** Initialize iBATIS. */ // TODO: How to init iBATIS via Stripes startup? public class IBATIS implements ConfigurableComponent { private static Logger log = Logger.getLogger(IBATIS.class); /** Location of the iBatis sqlMap config file. */ private static final String CONFIG_FILE = "sl/sqlmaps/sql-map-config.xml"; /** iBatis sqlMapClient. */ private static SqlMapClient sqlMap; public void init(Configuration config) throws Exception { // Hmm. This isn't getting called. log.debug("init: starting."); initStatic(); log.debug("init: ending."); } public static void initStatic() throws Exception { log.debug("initStatic: starting."); final Reader reader = Resources.getResourceAsReader(CONFIG_FILE); log.debug("initStatic: reader init done."); sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); log.debug("initStatic: sqlMap=" + sqlMap + "."); log.debug("init: ending."); } public static SqlMapClient getMapClient() { // Yuck. This has a first-time hit penalty, but it works for now. if(null == sqlMap) { try { initStatic(); } catch (Exception e) { log.error(e, e); } } return sqlMap; } } AbstractDAO.java (snip for example of usage) -------------------------------------------------- protected static List queryForList(String queryID, Object parameters) throws Exception { return IBATIS.getMapClient().queryForList(queryID, parameters); } ------------------------------------------------------------------------------ Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT is a gathering of tech-side developers & brand creativity professionals. Meet the minds behind Google Creative Lab, Visual Complexity, Processing, & iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian Group, R/GA, & Big Spaceship. http://www.creativitycat.com _______________________________________________ Stripes-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/stripes-users ------------------------------------------------------------------------------ Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT is a gathering of tech-side developers & brand creativity professionals. Meet the minds behind Google Creative Lab, Visual Complexity, Processing, & iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian Group, R/GA, & Big Spaceship. http://www.creativitycat.com _______________________________________________ Stripes-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/stripes-users
