Announcement Announcement Module
Collapse
No announcement yet.
JRuby & annotation-based configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JRuby & annotation-based configuration

    Hello. I'm having an issue using annotation-based xml configuration when using Spring in JRuby. Given a pojo:

    Code:
    import org.springframework.stereotype.Component;
    
    @Component("pojoTest")
    public class BasicPojo {}
    and this xml configuration:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="basicPojo" class="com.springtest.BasicPojo"/>
    
    </beans>
    when I run this jruby script:

    Code:
    require 'java'
    # require spring jars and jar containing BasicPojo omitted#
    
    context = org.springframework.context.support.ClassPathXmlApplicationContext.new("my-context.xml")
    puts bean.java_class.to_s

    Everything works fine. However, if I don't explicitly put the bean tag in the xml and rely instead on annotation configuration:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:component-scan base-package="com.springtest"/>
    
    </beans>

    I get a "No bean named 'basicPojo' is defined" error.

    I'm confused as to how this could be, as the ClassPathXmlApplicationContext class should be unaffected by the fact that it is being executed from a JRuby script... I can run the same setup with a basic main method in Java

    Code:
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("my-context.xml");
    		BasicPojo basicPojo = (BasicPojo) ctx.getBean("pojoTest");
    		System.out.println(basicPojo.getClass().toString());
    	}
    and it works fine. Any ideas of what I'm doing wrong? Has anyone else had success with a similar setup? I'm stumped.


    Thanks in advance.

  • #2
    This is more of a Spring question than a Spring Integration question. However, I was unable to reproduce this error using JDK 1.6, Spring 3.1, JRuby 1.6.2. The example below works:
    Code:
    include Java
    import org.springframework.context.ApplicationContext
    import org.springframework.context.support.ClassPathXmlApplicationContext
    
    applicationContext = ClassPathXmlApplicationContext.new("spring-config.xml")
    bean = applicationContext.getBean("annotationBean");
    puts bean
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:context="http://www.springframework.org/schema/context"	 
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
           <context:component-scan base-package="org.dturanski.test"/>
    	 
    </beans>
    Code:
    package org.dturanski.test;
    
    import org.springframework.stereotype.Component;
    
    @Component("annotationBean")
    public class AnnotationBean {
    
    }

    Comment


    • #3
      Thanks a ton for your response. Sorry about the forum; I agree that it was posted to the wrong one. I was using Spring 3.0.5 + JRuby 1.6.1.

      I've taken your code as well as upgraded to Spring 3.1 + JRuby 1.6.2. However, I'm still having the same issue. Can you tell me how you are setting your classpath? My issue is potentially not getting the component-scan the proper classpath. You omitted that from your JRuby script. Here's what I've got:

      Code:
      require 'java'
      require 'java/spring3.1/org.springframework.asm-3.1.0.M1.jar'
      require 'java/spring3.1/org.springframework.beans-3.1.0.M1.jar'
      require 'java/spring3.1/org.springframework.context.support-3.1.0.M1.jar'
      require 'java/spring3.1/org.springframework.context-3.1.0.M1.jar'
      require 'java/spring3.1/org.springframework.core-3.1.0.M1.jar'
      require 'java/spring3.1/org.springframework.expression-3.1.0.M1.jar'
      require 'java/commons-logging.jar'
      require 'java/commons-dbcp.jar'
      require 'java/commons-pool-1.5.jar'
      require 'java/test_data.jar'
      import org.springframework.context.ApplicationContext
      import org.springframework.context.support.ClassPathXmlApplicationContext
      
      applicationContext = ClassPathXmlApplicationContext.new("spring-config.xml")
      bean = applicationContext.getBean("annotationBean");
      puts bean
      Would you mind sharing your require statements? (I've also done this at the command line by passing -I arguments to jruby with the same result).

      Comment


      • #4
        Originally, I set CLASSPATH externally to include all dependent jars. I have the jars in 'lib' so:
        Code:
        require 'java'
        require 'lib/spring-asm-3.1.0.M1.jar'
        require 'lib/spring-beans-3.1.0.M1.jar'
        require 'lib/spring-context-support-3.1.0.M1.jar'
        require 'lib/spring-context-3.1.0.M1.jar'
        require 'lib/spring-core-3.1.0.M1.jar'
        require 'lib/spring-expression-3.1.0.M1.jar'
        require 'lib/commons-logging-1.1.1.jar'
        require 'lib/log4j-1.2.14.jar'
        import org.springframework.context.ApplicationContext
        import org.springframework.context.support.ClassPathXmlApplicationContext
        
        applicationContext = ClassPathXmlApplicationContext.new("spring-config.xml")
        bean = applicationContext.getBean("annotationBean");
        puts bean
        also works for me. Try adding
        Code:
        com.springtest.BasicPojo.new()
        to your script. If that fails then you have a basic class path problem.

        Comment


        • #5
          Thanks again dturanski for the replies. Got it working. No magic necessary (still not quite sure what I was doing incorrectly, but am in the process of an autopsy). If I find anything useful, I'll post it back to the list (although most likely I made an elementary mistake).

          Comment


          • #6
            One last post here (hopefully this will help somebody), and then I'm going to forget this ever happened. I was using Eclipse to create the jar file; by default, eclipse will not add empty directories to the jar. However, when you use the jar command at the command line, it does. Long story short, the cmd-line jar was working and the Eclipse-generated jar was not. Simply checking the "Add directory entries" checkbox in the jar export wizard rectified the issue. D'oh.

            Comment

            Working...
            X