Announcement Announcement Module
Collapse
No announcement yet.
Using Spring + groovy -> @Autowired used in Groovy not working? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Spring + groovy -> @Autowired used in Groovy not working?

    Hi there,

    I'm currently facing some issues using Spring 3.0 and Groovy 1.7.3, espacially the @Autowire annotation.

    Let me explain to you my configuration :

    I have a controller used by spring to handle my requests :

    Code:
    FrontController.java
    
    ......
    
    @Controller
    public class FrontController {
    
    	@Autowired
    	private GroovyClassLoader dcl;
    
    	@RequestMapping(value = "/", method = RequestMethod.GET)
    	public ModelAndView home(HttpServletRequest request,
    			HttpServletResponse response, HttpSession session) {
    		
    ...
    
    		Class<?> c = Class.forName("org.mycomp.groovy.MyGroovyClass",true,dcl);
    		Method m = c.getMethod("testMethod", String.class);
    		m.invoke(o, "Parameter testphrase");
    		
    ...
    
    	}
    }
    I've removed tries/catches to symplify the class.


    The class I use with the "forName" is a Groovy class that follows :

    Code:
    "MyGroovyClass.groovy"
    
    package org.mycomp.groovy;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import org.mycomp.exceptions.DatabaseServiceException;
    import org.mycomp.services.persistence.DaoImpl;
    
    
    public class Contrlr{
    	
    	@Autowired 
    	private DaoImpl daoImpl;
    	
    	public Contrlr(){
    		
    	}
    	
    	public void testMethod(String passphrase) throws DatabaseServiceException{
    		TestObj usr = new TestObj();
    		usr.setTestChar(passphrase);
    		if (null != this.daoImpl)
    			this.daoImpl.persistFromScratch(usr);
    	}
    
    }
    but I get an error because of a null "daoImpl" : it seems that it isn't autowired... Does anyone know why? Is it possible to do this using Spring and Groovy the way I do it?

    Thanks for any answer or tip

  • #2
    I'm no Groovy expert, but seems like your "org.mycomp.groovy.MyGroovyClass" is not a spring managed bean. The fact that you tried to access it using "Class.forName" shows that spring does not have anything to do with the class. So that's why the instance is not injected with the required dependencies.

    Comment


    • #3
      I'm no Groovy expert, but seems like your "org.mycomp.groovy.MyGroovyClass" is not a spring managed bean. The fact that you tried to access it using "Class.forName" shows that spring does not have anything to do with the class. So that's why the instance is not injected with the required dependencies.
      That's right, that IS the problem. You have to define your Groovy classes as Spring beans somehow (either by xml explicit definition or @Component + context:component-scan), then use dependency injection to inject your groovy bean in your controller.

      I also wonder why you're using Groovy as you don't seem to be taking full advantage of its capabilities: you still using ";" markers, you're using access modifiers, you're expliciting returns for methods. If I were to write your Groovy class I would write this:

      Code:
      package org.mycomp.groovy;
      
      import org.springframework.beans.factory.annotation.Autowired;
      
      import org.mycomp.exceptions.DatabaseServiceException;
      import org.mycomp.services.persistence.DaoImpl;
      
      
      class Contrlr //no access modifier needed
      {	
      	@Autowired 
      	DaoImpl daoImpl
      
              @Autowired
              TestObj usr //use prototype beans instead of creating objects yourself
      	
              //you don't need to explicit default constructor either
      	
      	def testMethod(String passphrase) throws DatabaseServiceException //use duck typing!
              {
      		usr.setTestChar(passphrase)
      		daoImpl.persistFromScratch(usr)
      	}
      }

      Comment


      • #4
        Originally posted by Enrico Pizzi View Post
        I also wonder why you're using Groovy as you don't seem to be taking full advantage of its capabilities
        I use it to have a dynamic model : I use it in a multitenant environment, to allow each tenant to custom its model objects, with a JPA implementation that allows each tenant to have its own model performed in database as if it was alone on a server.

        Groovy was a good choice for me for its integration in the JVM, but as I don't know very well the language, I write my code as if it was Java code.

        My "TestObj" object isn't a real bean, it's a test object used to try some logic to perform my "dynamic objects" in database, it has nothing to do with the annotations.
        The real bean is the "daoImpl" one, and as you noticed, it is a Spring managed bean, and I also guess that the context isn't aware of the creation of the groovy controller, that's why it wouldn't autowire the dao in my controller...

        As I wrote, I use a GroovClassLoader to load my groovy classes, and I'm actually looking for solutions to turn the application context aware of groovy objects creation. I've tried the "@component" annotation use, but it doesn't work either.

        Comment

        Working...
        X