简体   繁体   中英

How to inject a service Bean in a groovy script?

I have a spring boot application and a separate groovy script that runs within the app and I need to inject a CustomService into the groovy script.

How do i achieve that? i tried reading the documentation but it's not so clear

i tried adding the @Autowired annotatino but it gives me an error and I dont think that's how it should be done.

  • How to inject dependency into IOC container of spring boot project with Groovy

Let imagine here is you CustomService

public class CustomService {

    private final String smtPerhaps;
   
    ...
}

Then inject the bean as

beans {
    yourDesiredBeanNameInContainer(CustomService) {
        smtPerhaps = 'stackOverFlow'
    }
}

Its quit the same as the Java base dependency injection

@Configuration
public class JavaBeanConfig {

    @Bean
    public CustomService yourDesiredBeanNameInContainer() {
        return new CustomService("stackOverFlow");
    }
}

Find out more in here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM