Reputation: 2587
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"
}
}
Upvotes: 4
Views: 780
Reputation: 1376
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>
Upvotes: 3