当前位置: 首页 > article >正文

搭建面向切面编程项目

此项目在整合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>
第四步

测试


http://www.kler.cn/a/280537.html

相关文章:

  • mysql事务不加锁一致性读
  • Adobe Photoshop 2024 25.5安装与下载教程(直接用)
  • hive客户端
  • Datawhale AI夏令营第五期学习!
  • ArcGIS Pro基础:如何将数据和引用地图样式一起打包分享
  • elasticsearch -- RestClient操作文档
  • 二十三设计模式速记
  • 攻防演练之-最有价值安全工具大巡礼
  • Linux下TCP编程
  • 基于vue框架的便利店收银管理系统im2gw(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
  • 3个方法快速打开rar压缩包文件
  • Python+tkinter实现2048游戏
  • 视频结构化从入门到精通——认识视频结构化
  • arm接口技术三--裸机开发环境搭建与GPIO开发步骤总结
  • Redis远程字典服务器(11)—— redis客户端介绍
  • 新华三H3C HCL配置IS-IS基本配置
  • ​拓​竹​二​面​
  • 漏洞披露-金慧-综合管理信息系统-SQL
  • 方法引用
  • 【Linux系统编程】system V——信号量