Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

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:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
Configuring iBatis with JavaConfig Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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"/>
     </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, 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.

    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!

    Comment


    • #3
      It worked

      Thank you!

      Comment

      Working...
      X