Spring 配置文件详解

来源:互联网 发布:外星人源码论坛 骗局 编辑:程序博客网 时间:2024/06/03 10:29

        Spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Java EE程序员必须学会并灵活应用这份"图纸"准确地表达自己的"生产意图"。Spring配置文件是一个或多个标准的XML文档,applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。

        下面列举的是一份比较完整的配置文件模板,文档中各XML标签节点的基本用途也给出了详细的解释,这些XML标签节点在后续的知识点中均会用到,熟练掌握了这些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:util="http://www.springframework.org/schema/util"      xmlns:tx="http://www.springframework.org/schema/tx"      xsi:schemaLocation="http://www.springframework.org/schema/beans                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd                          http://www.springframework.org/schema/context                           http://www.springframework.org/schema/context/spring-context-4.0.xsd                          http://www.springframework.org/schema/aop                           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd                          http://www.springframework.org/schema/util                           http://www.springframework.org/schema/util/spring-util-4.0.xsd                          http://www.springframework.org/schema/tx                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">      <description>1、组件信息</description>      <!-- 引入其它资源文件 -->      <import resource="app-content.xml"/>      <!-- 开启注解处理器 -->      <context:annotation-config />      <!-- 开启组件自动扫描,扫描路径由base-package属性指定 -->      <context:component-scan base-package="www.lingmm.com.study.impl" />      <!-- 开启基于@Aspctj切面的注解处理器 -->      <aop:aspectj-autoproxy />      <!-- 使用class属性指定类的默认构造方法创建一个单实例Bean,名称由id属性指定 init-method初始化时调用的方法名 destroy-method对象在销毁时调用的方法 -->        <description>2、注入各类型值</description>      <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl" init-method="sayHello" destroy-method="sayHello">          <!-- 类属性引用的javaBean -->          <property name="propName" ref="referencedBean">          <!-- 直接指定属性值 -->          <property name="propName" value="value">      </bean>        <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl">          <!-- 类属性为list对象 -->          <property name="listProperty">              <list>                  <value>指定list对象的value</value>                  <ref bean="要引用的bean名称" />              </list>          </property>      </bean>        <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl">          <!-- 类属性为set对象 -->          <property name="setProperty">              <set>                  <value>指定set对象的value</value>                  <ref bean="要引用的bean名称" />              </set>          </property>      </bean>        <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl">          <!-- 类属性为map对象 -->          <property name="mapProperty">              <map>                  <value>指定set对象的value</value>                  <ref bean="要引用的bean名称" />              </map>          </property>      </bean>        <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl">          <!-- 类属性为props对象 -->          <property name="propProperty">              <props>                  <prop key="propKey">propValue</prop>              </props>          </property>      </bean>        <bean id="helloWorld" class="www.lingmm.com.study.impl.HelloWorldImpl">          <!-- bean类中要初始化为Null的属性名称 -->          <property name="nullProperty">              <null />          </property>      </bean>        <description>3、切面</description>      <aop:config>          <aop:aspect id="切面id" ref="要引用的切面实例名称">              <aop:pointcut id="切入点名称"  expression="切入点正则表达式"/>              <aop:before pointcut-ref="切入点名称" method="切面类中用做前置通知的方法名"/>              <aop:after-returning pointcut-ref="切入点名称" method="切面类中用做后置通知的方法名"/>              <aop:after-throwing pointcut-ref="切入点名称" method="切面类中用做异常通知的方法名"/>              <aop:after pointcut-ref="切入点名称" method="切面类中用做最终通知的方法名"/>              <aop:around pointcut-ref="切入点名称" method="切面类中用做环绕通知的方法名"/>          </aop:aspect>      </aop:config>        <description>4、事务</description>      <!-- 配置事务管理器 -->      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">          <property name="dataSource" ref="dataSource" />      </bean>        <!-- 配置事务通知 -->      <tx:advice id="transaction" transaction-manager="transactionManager">          <tx:attributes>              <!-- 方法以get开头不使用事务 -->              <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>              <!-- 其它方法默认使用事务 -->              <tx:method name="*"/>          </tx:attributes>      </tx:advice>        <!-- 使用AOP技能实现事务管理 -->      <aop:config>          <aop:pointcut id="事务切入点名称" expression="事务切入点正则表达式"/>          <aop:advisor advice-ref="事务通知名称" pointcut-ref="事务切入点名称" />      </aop:config>    </beans>  
引用地址:http://book.51cto.com/art/201004/193743.htm


0 0
原创粉丝点击