【spring】關於location路徑的設置(相對路徑和絕對路徑)

引用:http://blog.csdn.net/szwangdf/article/details/43969445


1、相對路徑

可以通過classpath和classpath*設置、如:

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
  <property name="mapperLocations" value="classpath*:mapper/**/*Mapper.xml"></property>
 </bean>

 <context:property-placeholder location="classpath:/jdbc.properties" />

關於classpath和classpath*的區別請參照:

1.無論是classpath還是classpath*都可以加載整個classpath下(包括jar包裏面)的資源文件。
2.classpath只會返回第一個匹配的資源,查找路徑是優先在項目中存在資源文件,再查找jar包。
3.文件名字包含通配符資源(如果spring-*.xml,spring*.xml),   如果根目錄爲"", classpath加載不到任何資源, 而classpath*則可以加載到classpath中可以匹配的目錄中的資源,但是不能加載到jar包中的資源



2、絕對路徑(可以把配置文件放到工程目錄以外、如tomcat和jboss的bin目錄下:這樣做的目的是隔離開發環境和發佈環境的配置文件、將差異化配置放到war以外、方便發佈)

可以通過file設置、如:

<property name="locations"  value="file:D/tomcat6/bin/db.properties" />

也可以通過file加系統變量的方式設置、如

在tomcat6下bin中的catalina.sh/catalina.bat 中 (或者jboss的run.sh/run.bat )文件中、JAVA——OPTS的後面追加

-Djdbc.properties=D:/tomcat6/bin/db.properties

<property name="locations"  value="file:#{systemProperties['jdbc.properties']}" />


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章