Announcement Announcement Module
Collapse
No announcement yet.
Autowiring does not work with Groovy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring does not work with Groovy

    Hi
    I have a regular Java bean and a Groovy bean:
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	     xmlns:p="http://www.springframework.org/schema/p"
    		 xmlns:util="http://www.springframework.org/schema/util"
    		 xmlns:lang="http://www.springframework.org/schema/lang"
    	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
               http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"
               default-init-method="init"
               default-autowire="byType">
    	      
    	<lang:groovy id="vinGenerator" script-source="file:C:/projects/mygroovy/groovy/VINBuildQuery.groovy">
    	    <lang:property name="kefe" value="I Can Do The Frug" />
    	</lang:groovy>
    	<bean id="brush" class="com.toyota.tms.groovy.Brush"/>
    </beans>
    Whether I do autowiring byType or byName, the Groovy class does get injected:
    Code:
    class VINBuildQuery {
    	
    	Brush brush 
    ..
    Any ideas? Do I do something wrong? I just do a very simple context loading:
    Code:
    ClassPathXmlApplicationContext  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    		Object obj = ctx.getBean("vinGenerator");
    		System.out.println(obj);

  • #2
    If you want to autowire your Groovy bean, your Groovy bean MUST implement a java interface. For example,
    ...
    class VINBuildQuery implements VINBuildQueryInterface {

    BrushInteface brush
    ..
    In your VINBuildQueryInterface java file. You should have:
    public interface VINBuildQueryInterface {
    ...
    public void setBrush(BrushInterface brush) ;
    ...
    Also there should be a BrushInteface java file. For example:
    public interface BrushInteface {
    ...
    }

    Autowire use the java reflection to find the set method. Without the java interface file, autowire will not be able to find the set method for brush. The following link contains some practices for using Groovy in Spring context.
    Practices on Using Groovy, Beanshell and JRuby in Spring 2.0

    cheers
    chris tam
    xenium

    Comment


    • #3
      Autowiring does not need interfaces

      Hi
      Thanks for the input. I tried it and debugged it. Autowiring does not work because the bean definition does not inherit the autowire="byType" etc. attribute from the root. I am not sure how to set it.
      It has nothing to do with interfaces as far as I can see it.
      Thanks.

      Comment


      • #4
        You are correct. The problem is nothing to do with interfaces. I am sorry for my misunderstanding. Java interface is required to autowire different Groovy beans or Beanshell beans or JRuby beans. But it is not needed in this case since your requirement is to inject a normal java object into a Groovy bean and there is no exception appear. I have look at the source code and find that the RootBeanDefinition is created in

        org.springframework.scripting.config.ScriptBeanDef initionParser.java file "parseInternal" method.

        After I had set the RootBeanDefinition's autowireMode to AUTOWIRE_BY_TYPE, the brush java object is injected into VINBuildQuery groovy script. But I cannot find any reference to DocumentDefaultsDefinition, that contain the default autowire setting, in the ScriptBeanDefinitionParser file. It seems that document autowire attribute is not inherit by the RootBeanDefinition when the RootBeanDefinition is created. May be you can find some workaround in the "parseInternal" method of ScriptBeanDefinitionParser that is used to create RootBeanDefinition. Or simple change your context xml to:
        ...
        <lang:groovy id="vinGenerator"
        script-source="file:C:/projects/mygroovy/groovy/VINBuildQuery.groovy">
        <lang: property name="kefe" value="I Can Do The Frug" />
        <lang: property name="brush" ref="brush" />
        </lang:groovy>
        ...
        Both changing the RootBeanDefinition or changing xml setring work for me. I will investigate furthur the reason of skipping default-autowire setting. If you have more information, please share with me. Thanks a lot for your help

        cheer
        chris tam
        xenium
        Last edited by cltam96; Feb 24th, 2007, 02:52 PM.

        Comment


        • #5
          I too am very interested in a solution for this. Any progress?

          Comment


          • #6
            Captured in: http://opensource.atlassian.com/proj...rowse/SPR-3679

            Comment


            • #7
              Just share my 2 cent experience. In Groovy, you can always define untype variable. If it is allowed Groovy scripting bean to use autowire by type, how do spring framework determine whether it should autowire the untype variable to any defined bean in the application context or not. Spring must either autowire the untype variable to any defined bean int the application context or not autowire any defined bean but both solutions have unexpected side effect that will surprise the programmers. Juergen Hoeller may have better solution that can solve this problem.

              chris tam
              Hong Kong

              Comment

              Working...
              X