Paul,
The only thing that Arjen left out is that you need to call the afterPropertiesSet method on the beans that would normally be managed by Spring. I found that it was easier to reuse the Spring XML file and implement a BeanPostProcessor to do the generation. Take a look at the included code.
Here's how it is configured:
Code:
<bean id="wsdlGenerator" class="josh.WsdlGenerationPostProcessor">
<property name="outputDir" value="C:/temp/wsdlgen/wsdl"/>
</bean>
Here is some sample code:
Code:
package josh;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.ws.wsdl.wsdl11.Wsdl11Definition;
public class WsdlGenerationPostProcessor implements BeanPostProcessor, InitializingBean {
private String outputDir;
private Transformer transformer;
private File outputDirAsFile;
public void setOutputDir(String outputDir) {
this.outputDir = outputDir;
}
public void afterPropertiesSet() throws Exception {
this.transformer = TransformerFactory.newInstance().newTransformer();
this.outputDirAsFile = new File(this.outputDir);
this.outputDirAsFile.mkdirs();
}
//Ignore
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof Wsdl11Definition) {
Wsdl11Definition definition = (Wsdl11Definition)bean;
//Assume that the beanName will be the wsdl name. Change appropriately.
String wsdlFileName = beanName + ".wsdl";
File wsdlFile = new File(outputDirAsFile, wsdlFileName);
try {
transformer.transform(definition.getSource(), new StreamResult(wsdlFile));
}
catch(TransformerException te) {
throw new FatalBeanException("Error generating wsdl for bean: " + beanName,te);
}
}
return bean;
}
}
Regards,
Joshua