I have a spring boot application that I wish to script with groovy.
I wish to autowire spring boot services to the script but when I compile and run the script autowired accountService turns out to be null.
What am I missing. Thanks.
I compile the script I loaded from db as follows:
public void start(Run run) throws Throwable { ScriptCode scriptCode = run.getScriptCode(); String groovy = scriptCode.getBody(); if ( scriptCode.isEncrypted() ) { groovy = Crypto.decrypt( groovy, scriptCode.getScript().getCreateBy().getJkey() ); } String processId = getProcessId(); run.setPid( processId ); runService.save( run ); CustomScript customScript = compile( groovy ); customScript.main( run ); } public CustomScript compile(String groovy) throws Throwable { CompilerConfiguration compilerConfiguration = new CompilerConfiguration(); CustomScriptImportCustomizer importCustomizer = new CustomScriptImportCustomizer(); compilerConfiguration.addCompilationCustomizers( importCustomizer ); ClassLoader classLoader = this.getClass().getClassLoader(); GroovyClassLoader groovyClassLoader = new GroovyClassLoader( classLoader, compilerConfiguration ); Class<CustomScriptInterface> clazz = groovyClassLoader.parseClass( groovy ); CustomScript script = (CustomScript) clazz.newInstance(); return script; }
My groovy script is as follows:
@Component class Runner extends CustomScript { @Autowired AccountService accountService @Override public void main(Run run) throws Throwable { println 'hello v3!' accountService.hello() println 'hello v3 done!' } }
Groovy imports are customized as follows:
addStarImports("org.springframework.beans.factory.annotation"); addImports("org.springframework.stereotype.Component");
AccountService is as follows:
@Service public class AccountService { public void hello() { System.out.println( "hello!" ); } }
https://stackoverflow.com/questions/67376833/autowired-service-is-null-in-embedded-groovy-script May 04, 2021 at 07:05AM
没有评论:
发表评论