@Override protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception { Assert.notNull(scriptSource, "scriptSource must not be null"); VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = this.binding != null ? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding) : new VariableBindingGroovyObjectCustomizerDecorator(); if (this.customizer != null) { customizerDecorator.setCustomizer(this.customizer); } if (!CollectionUtils.isEmpty(variables)) { customizerDecorator.setVariables(variables); } GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator); if (this.beanClassLoader != null) { factory.setBeanClassLoader(this.beanClassLoader); } if (this.beanFactory != null) { factory.setBeanFactory(this.beanFactory); } Object result = factory.getScriptedObject(scriptSource); return (result instanceof GString) ? result.toString() : result; }
@Override protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception { Assert.notNull(scriptSource, "scriptSource must not be null"); VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = this.binding != null ? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding) : new VariableBindingGroovyObjectCustomizerDecorator(); if (this.customizer != null) { customizerDecorator.setCustomizer(this.customizer); } if (!CollectionUtils.isEmpty(variables)) { customizerDecorator.setVariables(variables); } GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator); if (this.beanClassLoader != null) { factory.setBeanClassLoader(this.beanClassLoader); } if (this.beanFactory != null) { factory.setBeanFactory(this.beanFactory); } Object result = factory.getScriptedObject(scriptSource); return (result instanceof GString) ? result.toString() : result; }
@Override public void registerGroovyController(String scriptLocation) throws IOException { if (scriptNotExists(scriptLocation)) { throw new IllegalArgumentException("script not exists : " + scriptLocation); } scriptLastModifiedMap.put(scriptLocation, scriptLastModified(scriptLocation)); // Create script factory bean definition. GroovyScriptFactory groovyScriptFactory = new GroovyScriptFactory(scriptLocation); groovyScriptFactory.setBeanFactory(getBeanFactory()); groovyScriptFactory.setBeanClassLoader(getBeanFactory().getBeanClassLoader()); Object controller = groovyScriptFactory .getScriptedObject(new ResourceScriptSource(getApplicationContext().getResource(scriptLocation))); String controllerBeanName = scriptLocation; // 1、如果RequestMapping存在则移除 removeRequestMappingIfNecessary(controllerBeanName); if (getBeanFactory().containsBean(controllerBeanName)) { getBeanFactory().destroySingleton(controllerBeanName); // 移除单例bean // 移除注入缓存 否则Caused by: java.lang.IllegalArgumentException: object is not an // instance of declaring class getInjectionMetadataCache().remove(controller.getClass().getName()); } // 2、注册新的GroovyController getBeanFactory().registerSingleton(controllerBeanName, controller); // 注册单例bean getBeanFactory().autowireBean(controller); // 自动注入 // 3、注册新的RequestMapping registerRequestMappingIfNecessary(controllerBeanName); }