spring配置:
applicationContext-services.xml中配置
<context:annotation-config />:Spring2.1添加了一个新的context的Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在Spring配置文件中定义这些Bean显得比较笨拙。Spring为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是<context:annotation-config />: <context:annotationconfig />将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。
<context:component-scan />:的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
<context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:
- 过滤器类型 表达式范例 说明
- 注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
- 类名指定 org.example.SomeClass 过滤指定的类
- 正则表达式 com\.kedacom\.spring\.annotation\.web\..* 通过正则表达式过滤一些类
- AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类
- 值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用<context:component-scan />后,就可以将<context:annotation-config />移除了。
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="taduBeanId" />
</bean>
<bean id="taduBeanId" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="${com.tadu.db.url}"></property>
<property name="user" value="${com.tadu.db.user}"></property>
<property name="password" value="${com.tadu.db.password}"></property>
<property name="maxIdleTime" value="${com.tadu.db.maxIdleTime}" />
<property name="unreturnedConnectionTimeout" value="${com.tadu.db.unreturnedConnectionTimeout}" />
<property name="minPoolSize" value="${com.tadu.db.minPoolSize}" />
<property name="maxPoolSize" value="${com.tadu.db.maxPoolSize}" />
<property name="acquireIncrement" value="${com.tadu.db.acquireIncrement}" />
<property name="maxStatements" value="${com.tadu.db.maxStatements}" />
<property name="maxStatementsPerConnection" value="${com.tadu.db.maxStatementsPerConnection}" />
<property name="breakAfterAcquireFailure" value="${com.tadu.db.breakAfterAcquireFailure}" />
<property name="testConnectionOnCheckout" value="${com.tadu.db.testConnectionOnCheckout}" />
<property name="testConnectionOnCheckin" value="${com.tadu.db.testConnectionOnCheckin}" />
<property name="acquireRetryAttempts" value="${com.tadu.db.acquireRetryAttempts}" />
<property name="acquireRetryDelay" value="${com.tadu.db.acquireRetryDelay}" />
<property name="checkoutTimeout" value="${com.tadu.db.checkoutTimeout}" />
<property name="initialPoolSize" value="${com.tadu.db.initialPoolSize}" />
<property name="idleConnectionTestPeriod" value="${com.tadu.db.idleConnectionTestPeriod}" />
<property name="numHelperThreads" value="${com.tadu.db.numHelperThreads}" />
<property name="automaticTestTable" value="${com.tadu.db.automaticTestTable}" />
<property name="preferredTestQuery" value="${com.tadu.db.preferredTestQuery}" />
</bean>
从而实现数据库的持久化连接
<tx:advice>:配置事务属性的,如持久层的所有get属性都是只读的,如create事务异常时会回滚
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="create*" rollback-for="Exception" />
<tx:method name="update*" rollback-for="Exception" />
<tx:method name="save*" rollback-for="Exception" />
<tx:method name="delete*" rollback-for="Exception" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>:所有的切面和通知器都必须定义在 <aop:config> 元素内部, 一个application context可以包含多个 <aop:config>。 一个 <aop:config> 可以包含pointcut,advisor和aspect元素(注意它们必须按照这样的顺序进行声明)。切面使用<aop:aspect>来声明
一个描述service层中表示所有service执行的切入点可以如下定义(引用:http://www.cnblogs.com/yangy608/archive/2010/11/14/1876833.html):
<aop:config>
<aop:advisor pointcut="execution(* *com.tywire.tadu.wap.service..*.*(..))" advice-ref="txAdvice"/>
</aop:config>
advisor是是PointCut和Advice的综合体,完整描述了一个advice将会在pointcut所定义的位置被触发