搭建面向切面编程项目

此项目在整合Mybatis基础上修改,可参考主页的整合Mybatis文章

注解版本

第一步

引入maven坐标

<!--     切面编程所需jar包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
第二步

修改spring核心配置文件的文件头内容并且开启切面组件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启注解-->
    <context:annotation-config/>
    <!--组件扫描-->
    <context:component-scan base-package="com.xszx"></context:component-scan>
    <!--开启切面-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <!--  配置数据源  -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql:///sjt2405"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <!--创建sqlSessionFactory对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源对象-->
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="mybatis-config.xml"></property>
    </bean>
    <!--创建sqlSessionTemplate对象-->
<!--    需要配置Spring专属的SqlSession,以便与当作参数传递给其他的类使用-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>

</beans>
第三步

新建切面层及切面的类,并且在类中标识@Aspect注解表明该类为切面类

 第四步

测试,目前已经搭建完成,只需在主方法中进行测试即可。

 XML版本

第一步

引入maven坐标

<!--     切面编程所需jar包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
第二步

创建切面测试类,无需配注解

@Component

public class aopTest {

    public void before(){
        System.out.println("我是前置通知");
    }

}
第三步

配置beans.xml文件

<!-- 配置 aop -->
<aop:config>
    <!-- 配置切入点表达式 -->
    <aop:pointcut expression="execution(* com.xszx.service.*.*(..))"
                  id="pt1"/>
    <!--  建立事务的通知和切入点表达式的关系 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
<!-- 事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--配置事务的属性 -->
    <tx:attributes>
        <!-- 指定方法名称:是业务核心方法
        read-only:是否是只读事务。默认 false,不只读。
        isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。
        propagation:指定事务的传播行为。
        timeout:指定超时时间。默认值为: -1。永不超时。
        rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。
        没有默认值,任何异常都回滚。
        no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回
        滚。没有默认值,任何异常都回滚。
        -->
        <tx:method name="addUser"  propagation="REQUIRED"/>
        <tx:method name="changeSal"  propagation="SUPPORTS" rollback-for="FileNotFoundException"/>
    </tx:attributes>
</tx:advice>
第四步

测试

  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值