另一个选项可能是从应用程序上下文获取已经配置的bean:
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ws.wsdl.WsdlDefinition;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:yourWsdlConfig.xml"})
public class WsdlGeneratorTest {
@Autowired
private ApplicationContext ctx;
/*
* Path relative to module root (or absolute path can be used).
*/
private static final String OUTPUT_PATH = "target";
@Test
public void test() {
String names[] = ctx.getBeanNamesForType(WsdlDefinition.class);
TransformerFactory tFactory = TransformerFactory.newInstance();
for (String name : names) {
String filename = OUTPUT_PATH + File.separator + name + ".wsdl";
WsdlDefinition wd = (WsdlDefinition) ctx.getBean(name);
Source source = wd.getSource();
try {
Transformer transformer = tFactory.newTransformer();
FileOutputStream fo = new FileOutputStream(filename);
StreamResult result = new StreamResult(fo);
transformer.transform(source, result);
fo.close();
} catch (TransformerException e) {
fail("Error generating " + filename + ": TransformerException: " + e.getMessage());
} catch (IOException e) {
fail("Error generating " + filename + ": IOException: " + e.getMessage());
}
}
}
}