Results 1 to 3 of 3

Thread: Configuring iBatis with JavaConfig

  1. #1
    Join Date
    Mar 2009
    Posts
    3

    Unhappy 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"/>
     </bean>
    The datasource was easy to figure out, but i can't figure out howto pass the resource in. Any ideas?
    Code:
    	@Bean
    	public DataSource dataSource(){
    		JndiObjectFactoryBean jndi = new JndiObjectFactoryBean();
    		jndi.setExpectedType(DataSource.class);
    		jndi.setJndiName("ezpweb");
    		return (DataSource) jndi.getObject();
    	}
    	
    	@Bean
    	public SqlMapClient sqlMapClient(){
    		SqlMapClientFactoryBean sql = new SqlMapClientFactoryBean();
    		sql.setDataSource(dataSource());
    		sql.setConfigLocation(??????);
    	}
    EDITED:
    I forgot to note that this is a web app running inside Jetty.
    Last edited by chotchki; Feb 12th, 2010 at 02:24 PM. Reason: Forgot to give extra details.

  2. #2
    Join Date
    Apr 2007
    Posts
    307

    Default

    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.

    Code:
    @Configuration
    public class WebAppConfig implements ServletContextAware {
    
        private ServletContext servletContext;
    
        @Override
        public void setServletContext(ServletContext servletContext) {
            this.servletContext = servletContext;
        }
    
        @Bean
        public SqlMapClient sqlMapClient(){
            SqlMapClientFactoryBean fb = new SqlMapClientFactoryBean();
            fb.setDataSource(dataSource());
            fb.setConfigLocation(new ServletContextResource(this.servletContext, "/WEB-INF/.../sqlmap-config.xml"));
            fb.afterPropertiesSet();
            return sql.getObject();
        }
    
        @Bean
        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

    Code:
    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!
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  3. #3
    Join Date
    Mar 2009
    Posts
    3

    Default It worked

    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •