欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 搭建面向切面编程项目

搭建面向切面编程项目

2025/4/20 5:15:41 来源:https://blog.csdn.net/weixin_64922330/article/details/141613204  浏览:    关键词:搭建面向切面编程项目

此项目在整合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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://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>
第二步

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

@Componentpublic 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>
第四步

测试

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词