4

I have defined some beans with groovy dsl and tried to add them like i did previously using a xml definition for beans in my dispatcher-servlet.xml:

<import resource="/WEB-INF/config.groovy"/>

but this is not working. Whats wrong?

My bean definition looks like this:

import org.apache.commons.dbcp.BasicDataSource

beans {
   dataSource(BasicDataSource) {
      driverClassName = "com.mysql.jdbc.Driver"
      url = "jdbc:mysql://localhost:3306/test"
      username = "root"
      password = "root"
   }
}
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
GedankenNebel
  • 2,437
  • 5
  • 29
  • 39
  • Can you show us your groovy been? – Seagull Mar 25 '14 at 10:19
  • have you gotten anywhere with this? I have a similar need. There are many tools that assume spring will be configured from an XML file, for instance, the Jersey spring3 integration module looks for "applicationContext.xml" so I want to create one that just imports my applicationContext.groovy. – AlphaGeek Sep 07 '14 at 23:45

1 Answers1

3

Solved it by defining my own BeanPostprocessor:

public class GroovyConfigImporter implements BeanDefinitionRegistryPostProcessor {
    private static final Logger log = LoggerFactory.getLogger(GroovyConfigImporter.class);

    private final String config;

    public GroovyConfigImporter(String config) {
        this.config = config;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        log.info("Loading Groovy config '{}'", config);

        GroovyBeanDefinitionReader reader = new GroovyBeanDefinitionReader(registry);
        try {
            reader.importBeans(config);
        } catch (IOException e) {
            throw new ApplicationContextException("Can't open Groovy config '" + config + "'");
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    }
}

Then define in your XML:

<bean class="my.package.GroovyConfigImporter">
    <constructor-arg value="myConfig.groovy"/>
</bean>
relgames
  • 1,356
  • 1
  • 16
  • 34