bootstrap框架怎么用,Spring框架----Spring的基于XML的AOP的实现

 2023-09-28 阅读 31 评论 0

摘要:导入依赖 <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>commons-dbutils</groupId>

导入依赖

    <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version>t</dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.9</version></dependency>
</dependencies>

导入包org.aspectj它能够帮助我们解析切入点表达式
接口类

public interface IAccountService {/*模拟保存账户*/void saveAccount();/*模拟更新账户*/void updateAccount(int i);/*模拟删除用户*/int deleteAccount();
}

实现类

/*账户的业务层实现类*/
public class AccountServiceImpl implements IAccountService {public void saveAccount() {System.out.println("执行了保存");}public void updateAccount(int i) {System.out.println("执行了更新"+i);}public int deleteAccount() {System.out.println("执行了删除");return 0;}
}

公共代码类

/*用于记录日志的工具类,它里面提供了公共的代码*/
public class Logger {/*用于打印日志,计划让其在切入点方法执行之前执行,(切入点方法就是业务层方法)*/* 用spring配置的方式如何去做呢?* */public void printLog(){System.out.println("Logger类中的printLog方法开始记录日志了");}
}

如何实现在切入点方法之前开始执行呢,我们只要创建AccountService接口的代理对象,然后再执行执行这些方法之前调用logger中的这个方法就行了

bootstrap框架怎么用,搜索xmlns:aop,找到配置内容

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

spring中基于XML的AOP配置步骤
1.把通知Bean也交给spring来管理
2.使用aop:config标签表名开始AOP的配置
3.使用aop:aspect标签表示配置切面

id属性是给切面提供一个唯一的标识
ref属性:是指定通知类bean的id
4.在aop:aspect标签的内部使用对应的标签来配置通知的类型
我们现在示例是让printlog方法在切入点方法执行之前执行。所以是前置通知
aop:before标签表示配置前置通知。
method属性:用于指定logger类中哪个方法是前置通知。
pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层哪些方法增强
切入点表达式的写法
关键字:execution(表达式)
表达式:访问修饰符 返回值 包名.包名…类名.方法名(参数列表)

标准表达式写法
public void com.yujie.service.impl.AccountServiceImpl.saveAccount();
我们的切入点表达式明确的说明了这个方法要对哪个方法进行增强。

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--配置spring的IOC,把service对象配置进来,它需要增强它想在执行它的任何一个方法都记录了日志--><bean id="accountService" class="com.yujie.service.impl.AccountServiceImpl"></bean><!--配置logger类这个日志类中有一个方法可以记录日志,我们就配置了一个日志的通知,并且说明了我们这个里面有printLog方法会在这个方法执行之前执行--><bean id="logger" class="com.yujie.utils.Logger"></bean><!--配置AOP--><aop:config><!--配置切面,引用了上面的通知,通知中的方法pringLog表示先执行,后面切入点表达式表名要对saveAccount进行增强怎么增强就是之前的代理对象的写法--><aop:aspect id="logAdvice" ref="logger"><!--配置通知的类型,并且建立通知方法--><aop:before method="printLog" pointcut="execution(public void com.yujie.service.impl.AccountServiceImpl.saveAccount())"></aop:before></aop:aspect></aop:config>
</beans>

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/101706.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息