详解spring配置文件

1.基本配置:
<?xml version="1.0" encoding="UTF-8"?>
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    ">



<!-- 开启组件扫描 --&gt


<!--开启注解处理器--&gt

<!-- 使用注解,省去了propertity的xml配置,减少xml文件大小 --&gt


<!-- 自动注解 --&gt



<!-- 由spring容器去创建和维护,我们只要获取就可以了 --&gt

      init-method="init"  destroy-method="destory">
<!-- 静态工厂获取bean --&gt



<!-- 实例工厂获取bean,先实例化工厂再实例化bean--&gt


<!-- ref方式注入属性 --&gt


 

<!-- 内部bean方式注入 --&gt

 
    
 
 
 
 
 
    <!-- 集合的注入 --&gt
    
       第一个
       第二个
       第三个
    
 
 
 
    <!-- 集合的注入 --&gt
   
        第一个l
       第二个l
       第三个l
   
   
 
 
 
   
      value1
      value2
      value3
   
 
 
 
  
     
     
     
  
 


  
   <!-- 基本类型可以不写type --&gt
  
  

2.开启AOP:
<?xml version="1.0" encoding="UTF-8"?>
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                     http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                   ">




AOP的xml版本<?xml version="1.0" encoding="UTF-8"?>
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                     http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                   ">



 
 
  
 
 
  
  
 
 
 

3.开启事务和注解:
<?xml version="1.0" encoding="UTF-8"?>
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                   ">


                  
<!-- 配置数据源 --&gt  
    
      
      
      
      
     <!-- 连接池启动时的初始值 --&gt  
       
     <!-- 连接池的最大值 --&gt  
       
     <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --&gt  
       
     <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --&gt  
       
   
  
  <!-- 配置事务管理器--&gt  
   
      
   
  <!-- 配置业务bean --&gt
   
   
 
  
  <!-- 采用@Transactional注解方式来使用事务 --&gt  
   


XML版本:

<?xml version="1.0" encoding="UTF-8"?>
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                   ">


                  
<!-- 配置数据源 --&gt  
    
      
      
      
      
     <!-- 连接池启动时的初始值 --&gt  
       
     <!-- 连接池的最大值 --&gt  
       
     <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --&gt  
       
     <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --&gt  
       
   
  
<!-- 配置事务管理器 --&gt
   
      
   
  <!-- 配置业务bean --&gt
  
   
 
 
 
    <!-- 使用XML来使用事务管理--&gt 
 
    <!-- 配置一个切面,和需要拦截的类和方法 --&gt  
     
     

<!-- 配置一个事务通知 --&gt   
 
     
      <!-- 方法以get开头的,不使用事务 --&gt
       
      <!-- 其他方法以默认事务进行 --&gt
         
       
 
  
 
4.SSH:
<?xml version="1.0" encoding="UTF-8"?>
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                   ">


 <!-- 配置数据源 --&gt  
    
      
      
      
      
     <!-- 连接池启动时的初始值 --&gt  
       
     <!-- 连接池的最大值 --&gt  
       
     <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --&gt  
       
     <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --&gt  
       
   
 
  <!-- 配置hibernate的sessionFactory --&gt

 
 
     
        com/persia/model/Person.hbm.xml
     
  
  
     <!-- 1.首先在sessionFactory里面配置以上3条设置 --&gt
        <!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 --&gt
        <!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 --&gt
             <!--使用二级缓存--&gt
             <!-- 不使用查询缓存,因为命中率不是很高 --&gt
             <!-- 使用Ehcache缓存产品 --&gt 
 
     
          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
          hibernate.hbm2ddl.auto=update
          hibernate.show_sql=false
          hibernate.format_sql=false
          hibernate.cache.use_second_level_cache=true
                hibernate.cache.use_query_cache=false
             hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
     
     

<!-- 配置Spring针对hibernate的事务管理器 --&gt

    

<!-- 配置使用注解的方式来使用事务 --&gt 

<!-- 使用手工配置的注解方式来注入bean --&gt

<!--定义要注入的业务bean --&gt

<!--将Struts的action交给Spring容器来管理 --&gt

<!--1.这里要求name和struts-config里面的action的path名称一致,因为id不允许有特殊字符--&gt
<!--2.还得在Struts-config文件里面添加Spring的请求处理器,该处理器会根据action的path属性到Spring容器里面寻找这个bean,若找到了则用这个bean来处理用户的请求--&gt
<!--3.然后去掉action的type标签和值(可选),当Spring处理器找不到该bean时,才会使用Struts的action--&gt
<!--4.最后在action里面使用Spring的注入方式来注入业务bean--&gt


5.SSH2:
<?xml version="1.0" encoding="UTF-8"?>
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                   ">


 <!-- 配置数据源 --&gt  
    
      
      
      
      
     <!-- 连接池启动时的初始值 --&gt  
       
     <!-- 连接池的最大值 --&gt  
       
     <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --&gt  
       
     <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --&gt  
       
   
 
  <!-- 配置hibernate的sessionFactory --&gt

 
 
     
        com/persia/model/Person.hbm.xml
     
  
  
     <!-- 1.首先在sessionFactory里面配置以上3条设置 --&gt
        <!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 --&gt
        <!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 --&gt
             <!--使用二级缓存--&gt
             <!-- 不使用查询缓存,因为命中率不是很高 --&gt
             <!-- 使用Ehcache缓存产品 --&gt 
 
     
          hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
          hibernate.hbm2ddl.auto=update
          hibernate.show_sql=false
          hibernate.format_sql=false
          hibernate.cache.use_second_level_cache=true
                hibernate.cache.use_query_cache=false
             hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
     
     

<!-- 配置Spring针对hibernate的事务管理器 --&gt

    

<!-- 配置使用注解的方式来使用事务 --&gt 

<!-- 使用手工配置的注解方式来注入bean --&gt

<!--定义要注入的业务bean --&gt

<!--注入Struts 2的action --&gt

6.SSJ:
<?xml version="1.0" encoding="UTF-8"?>
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                   ">


<!-- 使用手工配置的注解方式来注入bean --&gt

<!-- 1.配置Spring集成JPA --&gt

     

<!--2.配置Spring针对JPA的事务 --&gt
   
    

<!--3.开启事务注解 --&gt

 
<!--以上3个Spring集成JPA的配置,在web项目先添加Spring支持,后添加JPA支持时会自动生成 --&gt

<!-- 配置业务bean --&gt

<!-- 配置Struts的action --&gt


本文来自CSDN博客,

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8020322/viewspace-677184/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8020322/viewspace-677184/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值