如果您使用的是 Spring 3.0/3.1,您可以利用 Java 配置:
@Configuration
class Config {
@Bean
public SchemaFactory schemaFact() throws SAXNotSupportedException, SAXNotRecognizedException {
final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
schemaFactory.setFeature("apache.org/xml/features/validation/schema-full-checking", false);
return schemaFactory;
}
@Bean
public Schema schema() throws SAXException {
return schemaFact().newSchema(new File("3DSecure.xsd"));
}
}
但是SchemaFactory
你想调用哪个方法?似乎他们都是getter或setter,所以你可以使用普通的XML注入......或者创建自己的FactoryBean
:
class SchemaFactoryFactoryBean implements FactoryBean<SchemaFactory> {
@Override
public SchemaFactory getObject() throws Exception
{
final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
schemaFactory.setFeature("apache.org/xml/features/validation/schema-full-checking", false);
return schemaFactory;
}
@Override
public Class<?> getObjectType()
{
return SchemaFactory.class;
}
@Override
public boolean isSingleton()
{
return true;
}
}
并像这样使用它(factory-method
不需要):
<bean id="schemaFact" class="SchemaFactoryFactoryBean"/>