我有其他一些开发人员编写的此类:
public class ManifestFile implements Serializable {
private final static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
private final static XPathFactory xPathFactory = XPathFactory.newInstance();
private final static DateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
private final String uuid;
private final Set<File> attachments = new LinkedHashSet<File>();
private final transient ApplicationContext applicationContext = JavaService.INSTANCE.getApplicationContext();
private final transient File attachmentDirectory;
private final Date processAfter = new Date(System.currentTimeMillis() + 3 * 1000 * 60);
{
try {
documentBuilderFactory.setNamespaceAware(true);
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new StreamSource(getClass().getResourceAsStream("/StrategicEmail5.xsd")));
documentBuilderFactory.setSchema(schema);
documentBuilderFactory.setValidating(true);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
我对这部分感到惊讶:
{
try {
documentBuilderFactory.setNamespaceAware(true);
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new StreamSource(getClass().getResourceAsStream("/StrategicEmail5.xsd")));
documentBuilderFactory.setSchema(schema);
documentBuilderFactory.setValidating(true);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
有人可以解释一下这段代码是否有效,在任何方法体之外使用 {} 有什么好处?