当前位置:首页 > 开发 > 编程语言 > Spring > 正文

[转]Spring配置文件总结(一)

发表于: 2011-09-12   作者:chyx72   来源:转载   浏览次数:
摘要: 转: http://zhaohe162.blog.163.com/blog/static/3821679720105711512570/ 首先来看一个标准的Spring配置文件 applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns
转: http://zhaohe162.blog.163.com/blog/static/3821679720105711512570/
首先来看一个标准的Spring配置文件 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 default-autowire="byName" default-lazy-init="true">

 <!-- 配置数据源 -->
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName">
   <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="url">
   <value>
    jdbc:mysql://localhost/ssh?characterEncoding=utf-8
   </value>
  </property>
  <property name="username">
   <value>root</value>
  </property>
  <property name="password">
   <value>123</value>
  </property>
 </bean>

 <!--配置SessionFactory -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="mappingResources">
   <list>
    <value>com/ssh/pojo/User.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>
 
 <!-- 事务管理 -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 
 <!-- hibernateTemplate -->
 <bean id="hibernateTemplate"
  class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>

 <!-- 配置数据持久层 -->
 <bean id="userDao"
  class="com.ssh.dao.impl.UserDaoImpl">
  <property name="hibernateTemplate" ref="hibernateTemplate"></property>
 </bean>
 

 <!-- 配置业务逻辑层 -->
 <bean id="userService"
  class="com.ssh.service.impl.UserServiceImpl">
  <property name="userDao" ref="userDao"></property>
 </bean>
 

 <!-- 配置控制层 -->
 <bean id="UserAction"
  class="com.ssh.action.UserAction"  scope="prototype">
  <property name="userService" ref="userService"></property>
 </bean>
  <!-- 配置pojo -->
 <bean id="User" class="com.ssh.pojo.User" scope="prototype"/>
</beans>


////////////////////////////////////////////////     下面是详解:  ////////////////////////////////////////////////////////////////////////

1.基本配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans
 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
                    ">


<context:component-scan base-package="com.persia">
<!-- 开启组件扫描 -->
</context:component-scan>

<context:annotation-config>
<!--开启注解处理器-->
</context:annotation-config>

<!-- 使用注解,省去了propertity的xml配置,减少xml文件大小 -->
<bean id="personServiceAnno" class="com.persia.PersonServiceAnnotation"></bean>
<bean id="personDaoBeanAnno" class="com.persia.PersonDaoBean"></bean>
<bean id="personDaoBeanAnno2" class="com.persia.PersonDaoBean"></bean>

<!-- 自动注解 -->
<bean id="personServiceAutoInject" class="com.persia.PersonServiceAutoInject" autowire="byName"></bean>


<bean id="personService" class="com.persia.PersonServiceBean">
<!-- 由spring容器去创建和维护,我们只要获取就可以了 -->
</bean>

<bean id="personService2" class="com.persia.PersonServiceBeanFactory" factory-method="createInstance" lazy-init="true" 
      init-method="init"  destroy-method="destory">
<!-- 静态工厂获取bean -->
</bean>

<bean id="fac" class="com.persia.PersonServiceBeanInsFactory"></bean>
<bean id="personService3" factory-bean="fac" factory-method="createInstance" scope="prototype">
<!-- 实例工厂获取bean,先实例化工厂再实例化bean-->
</bean>


<!-- ref方式注入属性 -->
<bean id="personDao" class="com.persia.PersonDaoBean"></bean>
<bean id="personService4" class="com.persia.PersonServiceBean">
  <property name="personDao" ref="personDao"></property>
</bean>

<!-- 内部bean方式注入 -->
<bean id="personService5" class="com.persia.PersonServiceBean">
  <property name="personDao">
     <bean class="com.persia.PersonDaoBean"></bean>
  </property>
  <property name="name" value="persia"></property>
  <property name="age" value="21"></property>
  
  <property name="sets">
    <!-- 集合的注入 -->
     <set>
       <value>第一个</value>
       <value>第二个</value>
       <value>第三个</value>
     </set>
  </property>
  
  <property name="lists">
    <!-- 集合的注入 -->
    <list>
        <value>第一个l</value>
       <value>第二个l</value>
       <value>第三个l</value>
    </list>
    
  </property>
  
  <property name="properties">
    <props>
      <prop key="key1">value1</prop>
      <prop key="key2">value2</prop>
      <prop key="key3">value3</prop>
    </props>
  </property>
  
  <property name="map">
   <map>
      <entry key="key1" value="value-1"></entry>
      <entry key="key2" value="value-2"></entry>
      <entry key="key3" value="value-3"></entry>
   </map>
  </property>
</bean>

<bean id="personService6" class="com.persia.PersonServiceBean">
   <constructor-arg index="0" value="构造注入的name" ></constructor-arg>
   <!-- 基本类型可以不写type -->
   <constructor-arg index="1" type="com.persia.IDaoBean" ref="personDao">
   </constructor-arg> 
</bean>

</beans>

2.开启AOP:
<?xml version="1.0" encoding="UTF-8"?>
<beans
 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:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="myInterceptor" class="com.persia.service.MyInterceptor"></bean>
<bean id="personServiceImpl" class="com.persia.service.impl.PersonServiceImpl"></bean>
</beans>AOP的xml版本<?xml version="1.0" encoding="UTF-8"?>
<beans
 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:aspectj-autoproxy></aop:aspectj-autoproxy>

<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>
<bean id="aspectBean" class="com.persia.service.MyInterceptor"></bean>

<aop:config>
 <aop:aspect id="myaop" ref="aspectBean">
 <aop:pointcut id="mycut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..))"/>
 <aop:pointcut id="argcut" expression="execution(* com.persia.service.impl.PersonServiceImpl.*(..)) and args(name)"/>  
 <aop:before pointcut-ref="mycut" method="doAccessCheck"  />
 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
   <aop:after-throwing pointcut-ref="mycut" method="doThrowing"/>
   <aop:after pointcut-ref="argcut" method="doAfter" arg-names="name"/>
 <aop:around pointcut-ref="mycut" method="arround"/>
 </aop:aspect>
  
</aop:config>

</beans>

3.开启事务和注解:
<?xml version="1.0" encoding="UTF-8"?>
<beans
 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 
                   ">

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
                   
<!-- 配置数据源 -->   
  <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/test?useUnicode=true&amp;characterEncoding=utf-8"/>   
    <property name="username" value="root"/>   
    <property name="password" value=""/>   
     <!-- 连接池启动时的初始值 -->   
     <property name="initialSize" value="1"/>   
     <!-- 连接池的最大值 -->   
     <property name="maxActive" value="500"/>   
     <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->   
     <property name="maxIdle" value="2"/>   
     <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->   
     <property name="minIdle" value="1"/>   
  </bean>  
   
  <!-- 配置事务管理器-->   
 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
    <property name="dataSource" ref="dataSource"/>   
  </bean>  
  <!-- 配置业务bean -->
    <bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
    <property name="ds" ref="dataSource"></property>
  </bean>
   
  <!-- 采用@Transactional注解方式来使用事务 -->   
  <tx:annotation-driven transaction-manager="txManager"/>  


</beans>

XML版本:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 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 
                   ">

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
                   
<!-- 配置数据源 -->   
  <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/test?useUnicode=true&amp;characterEncoding=utf-8"/>   
    <property name="username" value="root"/>   
    <property name="password" value=""/>   
     <!-- 连接池启动时的初始值 -->   
     <property name="initialSize" value="1"/>   
     <!-- 连接池的最大值 -->   
     <property name="maxActive" value="500"/>   
     <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->   
     <property name="maxIdle" value="2"/>   
     <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->   
     <property name="minIdle" value="1"/>   
  </bean>  
   
<!-- 配置事务管理器 -->
 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
    <property name="dataSource" ref="dataSource"/>   
  </bean>  
  <!-- 配置业务bean -->
   <bean id="personService" class="com.persia.service.impl.PersonServiceImpl">
    <property name="ds" ref="dataSource"></property>
  </bean>
  
  
    <!-- 使用XML来使用事务管理-->  
<aop:config>  
    <!-- 配置一个切面,和需要拦截的类和方法 -->   
    <aop:pointcut id="transactionPointcut" expression="execution(* com.persia.service..*.*(..))"/>  
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>  
</aop:config> 
<!-- 配置一个事务通知 -->    
<tx:advice id="txAdvice" transaction-manager="txManager">  
      <tx:attributes> 
      <!-- 方法以get开头的,不使用事务 --> 
        <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/> 
      <!-- 其他方法以默认事务进行 --> 
        <tx:method name="*"/>  
      </tx:attributes>  
</tx:advice>  
   
  
</beans>

[转]Spring配置文件总结(一)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
一、前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团
转自: http://blog.csdn.net/sunitjy/article/details/6782431 一、前言: 大家好,Spring3 MVC是非
在认识Spring Security之前,所有的权限验证逻辑都混杂在业务逻辑中,用户的每个操作以前可能都需要
在认识Spring Security之前,所有的权限验证逻辑都混杂在业务逻辑中,用户的每个操作以前可能都需要
在认识Spring Security之前,所有的权限验证逻辑都混杂在业务逻辑中,用户的每个操作以前可能都需要
在认识Spring Security之前,所有的权限验证逻辑都混杂在业务逻辑中,用户的每个操作以前可能都需要
在认识Spring Security之前,所有的权限验证逻辑都混杂在业务逻辑中,用户的每个操作以前可能都需要
转:http://www.cnblogs.com/rush/archive/2012/05/15/2502264.html#lb1 Ajax与JSON的一些总结 1.1.
数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型。 基本类型的变量保存原始值,即
转:http://www.cnblogs.com/rush/archive/2012/05/15/2502264.html#lb1 Ajax与JSON的一些总结 1.1.
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号