Announcement Announcement Module

JavaConfig forum decommissioned in favor of Core Container

As described at

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at
See more
See less
Configuring iBatis with JavaConfig Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuring iBatis with JavaConfig

    I am trying to figure out howto convert the following Spring bean xml to the new JavaConfig method:

    HTML Code:
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
     	<property name="configLocation" value="/WEB-INF/classes/db/ibatis/sqlmap-config.xml"/>
     	<property name="dataSource" ref="dataSource"/>
    The datasource was easy to figure out, but i can't figure out howto pass the resource in. Any ideas?
    	public DataSource dataSource(){
    		JndiObjectFactoryBean jndi = new JndiObjectFactoryBean();
    		return (DataSource) jndi.getObject();
    	public SqlMapClient sqlMapClient(){
    		SqlMapClientFactoryBean sql = new SqlMapClientFactoryBean();
    I forgot to note that this is a web app running inside Jetty.
    Last edited by chotchki; Feb 12th, 2010, 03:24 PM. Reason: Forgot to give extra details.

  • #2
    This one is a little tricky. setConfigLocation() accepts a Resource object. Usually when using XML the creation of the Resource object is done automatically for you by a PropertyEditor (type converter) that translates the String that you provide into a ServletContextResource object.

    You need to do this manually in the case of JavaConfig.

    The trouble with doing so, is that a ServletContextResource needs not only the path to the file ("/WEB-INF/..."), but also needs access to the ServletContext object from Jetty. You can get hold of this by having your @Configuration class implement Spring's ServletContextAware interface.

    public class WebAppConfig implements ServletContextAware {
        private ServletContext servletContext;
        public void setServletContext(ServletContext servletContext) {
            this.servletContext = servletContext;
        public SqlMapClient sqlMapClient(){
            SqlMapClientFactoryBean fb = new SqlMapClientFactoryBean();
            fb.setConfigLocation(new ServletContextResource(this.servletContext, "/WEB-INF/.../sqlmap-config.xml"));
            return sql.getObject();
        public DataSource dataSource() {
            return ...;
    Note that this situation would be much easier if you were not loading the sqlmap-config.xml as a servlet context resource. If, for example it were being loaded from the classpath, it would be as simple as saying

    new ClassPathResource("/path/to/sqlmap-config.xml");
    ServletContextResource is just by nature a little more involved as it has to interact with the servlet context directly.

    Hope that helps!


    • #3
      It worked

      Thank you!