Spring容器启动配置

Spring容器启动配置

什么时候spring容器启动?其实就是程序中执行加载 web.xml配置文件的时候。

Spring可以通过(Servlet)org.springframework.web.context.ContextLoaderServlet和Listener(org.springframework.web.context.ContextLoaderListener)两个类作为Spring启动的入口。

  • 1.应用程序下加载

ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");

applicationContext.xml内容如下:

复制代码
<? xml version="1.0" encoding="UTF-8" ?>
<! 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  不过这里暂时没注入 -->
    
< 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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值