2014-06-18 19 views
5

Mam aplikację Spring Boot z powłoką-rozrusznika-zderzaka-sprężyny. Po umieszczeniu tego skryptu hello.groovy wypisuje "cześć" i jest OK.Wstrzykiwanie fasoli sprężystej w fasoli groszkowej

package commands 

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

class hello { 

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

} 

Ale kiedy próbuję wstrzyknąć trochę fasoli Spring, zawsze ma ona wartość zerową.

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"; 
    } 

} 

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

+0

Byłoby łatwiej znaleźć odpowiedź, jeśli podasz przykład pracy na GitHubie. – Opal

Odpowiedz

3

Wiosna BeanFactory można przyjmować z kontekstu wywołania.

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"; 
    } 

} 
Powiązane problemy