Spring容器啟動配置
什么時(shí)候spring容器啟動?其實(shí)就是程序中執(zhí)行加載 web.xml配置文件的時(shí)候。
Spring可以通過(Servlet) org.springframework.web.context.ContextLoaderServlet 和Listener( org.springframework.web.context.ContextLoaderListener )兩個(gè)類作為Spring啟動的入口。
- 1.應(yīng)用程序下加載
ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
applicationContext.xml內(nèi)容如下:
<! DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd" >
?
< beans >
???? <!-- ?dataSource?config? -->
???? < bean? id? ="dataSource" ?class? ="org.apache.commons.dbcp.BasicDataSource" ?destroy-method ="close" > ?
???????? < property? name ="driverClassName" ?value ="com.mysql.jdbc.Driver" ? /> ?
???????? < property? name ="url" ?value ="jdbc:mysql://localhost:3306/game" ? /> ?
???????? < property? name ="username" ?value ="root" ? /> ?
???????? < property? name ="password" ?value ="root" /> ?
???? </ bean > ?
????
???? <!-- ?SessionFactory? -->
???? < bean? id ="sessionFactory"
????????class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
???????? < property? name ="dataSource" >
???????????? < ref? bean ="dataSource" />
???????? </ property >
???????? < property? name ="configLocation" >
???????????? < value > classpath:com\sterning\bean\hibernate\hibernate.cfg.xml </ value >
???????? </ property > ????????
???? </ bean >
????
???? <!-- ?TransactionManager??不過這里暫時(shí)沒注入 -->
???? < bean? id ="transactionManager"
????????class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
???????? < property? name ="sessionFactory" >
???????????? < ref? local ="sessionFactory" />
???????? </ property >
???? </ bean >
????
???? <!-- ?DAO? -->
???? < bean? id ="booksDao" ?class ="com.sterning.books.dao.hibernate.BooksMapDao" >
???????? < property? name ="sessionFactory" >
???????????? < ref? bean ="sessionFactory" />
???????? </ property >
???? </ bean >
????
???? <!-- ?Services? -->
???? < bean? id ="booksService" ?class ="com.sterning.books.services.BooksService" >
???????? < property? name ="booksDao" >
???????????? < ref? bean ="booksDao" />
???????? </ property >
???? </ bean >
????
???? < bean? id ="pagerService" ?class ="com.sterning.commons.PagerService" />
????
???? <!-- ?view? -->
???? < bean? id ="bookAction" ?class ="com.sterning.books.web.actions.BooksAction" ?singleton ="false" >
???????? < property? name ="booksService" >
???????????? < ref? bean ="booksService" />
???????? </ property >
???????? < property? name ="pagerService" >
???????????? < ref? bean ="pagerService" />
???????? </ property >
???? </ bean > ??
????
</ beans >
?
- 2.web模式下加載
web.xml:
???<context-param>
?????????? <param-name>
log4jConfigLocation
</param-name>
???????? ?<param-value>/WEB-INF/classes/log4j.properties</param-value>
? </context-param>
?<!-- ContextConfigLocation -->
?<context-param>
??? ?<param-name>
contextConfigLocation
</param-name>
??? ?<param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
? ?</context-param>
? ?
?<!-- Listener contextConfigLocation -->
? ?<listener>
??? ?<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
? ?</listener>
?<!-- Listener log4jConfigLocation -->
? ?<listener>
??? ?<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
? ?</listener>
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
