2014-06-18 16 views
5

Ho un'applicazione Spring Boot con spring-boot-starter-remote-shell. Quando metto questo ciao.groovy script stampa 'ciao' e va bene.Inject Spring bean in groovy bean

package commands 

import org.crsh.cli.Usage 
import org.crsh.cli.Command 

class hello { 

    @Usage("Say Hello") 
    @Command 
    def main(InvocationContext context) { 
     return "hello"; 
    } 

} 

Ma quando provo a iniettare del bean Spring è sempre nullo.

package commands 

import org.crsh.cli.Usage 
import org.crsh.cli.Command 
import org.springframework.beans.factory.annotation.Autowired 
import org.springframework.stereotype.Component 
import org.springframework.batch.core.launch.JobLauncher 

@Component 
class hello { 
    @Autowired 
    JobLauncher jobLauncher; 

    @Usage("Say Hello") 
    @Command 
    def main(InvocationContext context) { 
     if(jobLauncher != null){ 
      return "OK"; 
     }else{ 
      return "NULL"; 
     } 
     return "hello j"; 
    } 

} 

devo @ComponentScan(basePackages={"com....", "commands"})

+0

Sarebbe più facile trovare una risposta se per esempio si fornisce un esempio di lavoro minimo su GitHub. – Opal

risposta

3

Primavera BeanFactory può essere presa dal contesto Invocazione.

package commands 

import org.crsh.cli.Usage 
import org.crsh.cli.Command 
import org.crsh.command.InvocationContext; 
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.batch.core.launch.JobLauncher 

class hello { 

    @Usage("Say Hello") 
    @Command 
    def main(InvocationContext context) { 
     BeanFactory beanFactory = (BeanFactory) context.getAttributes().get("spring.beanfactory"); 
     JobLauncher jobLauncher = beanFactory.getBean(JobLauncher.class); 
     if(jobLauncher != null){ 
      return jobLauncher.toString(); 
     }else{ 
      return "NULL"; 
     } 
     return "hello j"; 
    } 

}