全部都写在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: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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-lazy-init="true">
<!-- 定义数据源的Bean ,给Hibernate的sessionFactory-->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:ora9">
</property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<!-- 定义Hibernate的sessionFactory,通过该Bean,可以获得Hibernate的Session-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<!--设置二级缓冲-->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<!--设置二级缓冲,打开查询缓冲-->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!--设置显示Hibernate操作的SQL语句-->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
com/cntion/example/model/Product.hbm.xml
</value>
</list>
</property>
</bean>
<!-- 注入依赖类 -->
<bean id="productDao" class="com.cntion.example.dao.hibernate.ProductDaoHibernate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="productManager" class="com.cntion.example.service.impl.ProductManagerImpl">
<property name="productDao" ref="productDao"></property>
<property name="compassTemplate" ref="compassTemplate"></property>
</bean>
<bean id="productBean" class="com.cntion.example.action.ProductAction" scope="prototype">
<property name="productManager" ref="productManager"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事务特性 ,配置add、delete和update开始的方法,事务传播特性为required-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些类的方法进行事务管理,当前cn.com.jobedu.oa.service包中的子包、类中所有方法需要,还需要参考tx:advice的设置 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution (* com.cntion.example.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>
分享到:
相关推荐
下面将详细介绍Spring AOP的注解方式和XML配置方式。 ### 注解方式 #### 1. 定义切面(Aspect) 在Spring AOP中,切面是包含多个通知(advisors)的类。使用`@Aspect`注解标记切面类,例如: ```java @Aspect ...
Spring AOP通过XML配置文件提供了灵活的方式来定义和管理切面、切入点和通知,使得我们可以轻松地在应用程序中实现横切关注点的解耦。了解和掌握Spring AOP的配置实现,有助于提升我们构建松散耦合、易于维护的系统...
**Spring AOP XML方式配置通知** 在Java世界中,Spring框架是广泛应用的IoC(Inversion of Control)和AOP(Aspect Oriented Programming)容器。AOP允许开发者定义“方面”,这些方面可以封装关注点,如日志、事务...
本篇将详细讲解Spring AOP如何进行事务配置,包括注解和非注解两种方式。 1. **注解方式的事务管理** - **@Transactional**:这是Spring提供的核心注解,用于标记在一个方法或类上开启事务。当被注解的方法执行时...
在"Spring AOP管理XML版"中,我们主要关注的是通过XML配置来管理AOP。以下是一些关键的XML配置元素: - `<aop:config>`:这是AOP配置的根元素,包含所有其他的AOP配置。 - `<aop:aspect>`:定义一个切面,内部可以...
在使用Spring AOP时,我们可以通过XML配置或注解的方式来定义切面。例如,可以使用`@Aspect`注解定义一个切面类,`@Before`、`@After`等注解来声明通知,`@Pointcut`定义切点表达式。 在实际开发中,Spring AOP广泛...
Spring AOP的XML配置实例展示了如何将横切关注点(如日志、事务等)与业务逻辑解耦,提高了代码的可复用性和可维护性。这种编程模式在大型项目中尤其有用,因为它使得系统的结构更加清晰,每个组件都专注于自己的...
本篇文章主要讲解如何通过XML配置来实现Spring AOP的开发。 首先,了解AOP的基本概念。AOP通过“切面”(Aspect)来封装横切关注点,切面由“通知”(Advice)和“连接点”(Join Point)组成。通知是在特定连接点...
在Spring的XML配置文件中,我们可以创建一个`<aop:config>`元素,并在其内部定义`<aop:advisor>`来创建Advisor。Advisor的`advice-ref`属性用于指定通知bean的ID,`pointcut-ref`属性用于指定切点bean的ID。 2. ...
Spring支持两种AOP的实现方式:Spring AspectJ注解风格和Spring XML配置风格。使用AspectJ注解风格是最常见的,它允许开发者直接在方法上使用注解来定义切面。 Spring AOP中有五种不同类型的的通知(Advice): 1....
我们可以通过XML配置或注解来声明事务边界。例如,在服务层的方法上使用`@Transactional`注解,可以自动开启、提交或回滚事务。如果方法抛出未检查异常(继承自`RuntimeException`的异常),Spring会默认回滚事务;...
1. **XML配置**:在Spring的配置文件中,可以通过<aop:config>标签来定义切面,<aop:pointcut>定义切点,<aop:advisor>或<aop:aspect>定义通知。例如: ```xml <aop:config> <aop:pointcut id="myPointcut" ...
Spring AOP(面向切面编程)是Spring框架的重要组成部分,它提供了一种模块化和声明式的方式来处理系统中的交叉关注点问题,如日志、事务管理、安全性等。本示例将简要介绍如何在Spring应用中实现AOP,通过实际的...
### Spring之AOP配置文件详解 #### 一、前言 在Java开发中,Spring框架因其强大的功能和灵活的配置而被广泛应用于企业级应用的开发。其中,面向切面编程(Aspect Oriented Programming,简称AOP)是Spring框架的...
在Spring XML配置中,我们可以使用`<aop:config>`元素来定义切点表达式,然后使用`<aop:aspect>`元素来声明切面,并将通知方法与切点关联起来。此外,还可以使用注解驱动的配置,通过`@EnableAspectJAutoProxy`注解...
XML配置是Spring AOP早期的主要实现方式,虽然现在有更简洁的注解式配置,但理解XML配置方式对于深入理解AOP原理仍然很有帮助。下面我们将详细探讨如何通过XML配置实现Spring AOP。 首先,我们需要在Spring配置文件...
在实际开发中,你可以通过配置XML或使用注解来定义切面,以及通知的触发条件。例如,使用`@Aspect`注解定义切面类,使用`@Before`、`@After`等注解标记通知方法,使用`@Pointcut`定义切点表达式。 总的来说,Spring...
**Spring AOP XML配置**是Spring框架中一种重要的面向切面编程(Aspect-Oriented Programming,简称AOP)实现方式,允许开发者定义“横切关注点”,如日志、事务管理等,这些关注点可以独立于业务代码进行,提高代码...
Spring AOP的配置可以通过XML或注解方式进行: - **XML配置**: - 在`<aop:config>`标签内定义切面,`<aop:pointcut>`定义切入点,`<aop:advisor>`定义通知。 - `<aop:aspect>`标签用于定义完整的切面,包括切入...
例如,可以通过以下XML配置启用Spring AOP: ```xml <aop:aspectj-autoproxy /> ``` 然后,我们可以定义切面、切点和通知,比如: ```xml <aop:config> <aop:aspect ref="loggingAspect"> <aop:before method...