Spring+HibernateEntityManager(XMLからの外部リソース参照編)

SpringFramework や Hibernate の定義ファイルには、スキーマの定義等、外部の URL を参照すると思しき箇所があります。本番環境では、外部への HTTP 接続が不可能な場合や、ネットワーク障害で外部にアクセスできなくなったりする可能性がありますので、それがシステムに影響するのは避けたいところです。

そのため、SpringFramework や Hibernate では、独自の EntityResolver 実装により、HTTPによる外部へのアクセスを避けて、クラスパスリソース等を参照する仕組みを提供しています。

通常は、あまり意識する必要はありませんが、カスタムのスキーマを追加する場合などは、クラスパス上の xsd ファイルを参照するようにしたいところです。

Spring Framework の場合

参考:B.5.2. 'META-INF/spring.schemas'
http://static.springframework.org/spring/docs/2.5.x/reference/extensible-xml.html

PluggableSchemaResolverにて、META-INF/spring.schemas 中で指定された xsd ファイルについてはクラスパスを参照するようになっています。

META-INF/spring.schemas は、URL とクラスパスリソースのマッピングを指定するようになっていて、イコールの左側が URL で、右側がその URL に対応するクラスパスリソースの指定になっています。

http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd
http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-2.5.xsd
http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd
http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd
http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd
http\://www.springframework.org/schema/jms/spring-jms-2.5.xsd=org/springframework/jms/config/spring-jms-2.5.xsd
http\://www.springframework.org/schema/jms/spring-jms.xsd=org/springframework/jms/config/spring-jms-2.5.xsd
http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd
http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd
http\://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http\://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd
http\://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd
http\://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
http\://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd

独自に xsd 設定したい場合は、独自に、META-INF/spring.schemas を作成すれば OK です。配置は、SpringFramework の jar ファイルを上書きするような必要はなく、自前のクラスと同じクラスパス上の META-INF/spring.schemas として配置すれば OK です。

Hibernateの場合

参考:http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-declaration-entity-resolution