2014-06-18 35 views
5

Tôi có ứng dụng Khởi động mùa xuân với spring-boot-starter-remote-shell. Khi tôi đặt kịch bản hello.groovy này, nó in 'hello' và điều đó là OK.Tiêm đậu mùa xuân trong đậu groovy

package commands 

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

class hello { 

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

} 

Nhưng khi tôi cố gắng tiêm một số hạt mùa xuân, nó luôn luôn rỗng.

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

} 

Tôi có @ComponentScan(basePackages={"com....", "commands"})

+0

Sẽ dễ dàng hơn để tìm câu trả lời nếu bạn cung cấp ví dụ làm việc tối thiểu trên GitHub chẳng hạn. – Opal

Trả lời

3

Spring BeanFactory có thể được lấy từ bối cảnh Invocation.

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

} 
Các vấn đề liên quan