spring中基于XML的AOP配置步骤
生活随笔
收集整理的這篇文章主要介紹了
spring中基于XML的AOP配置步骤
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring中基于XML的AOP配置步驟
IAccountService.java
package com.itheima.service;/*** 賬戶的業務層接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount();/*** 模擬更新賬戶* @param i*/void updateAccount(int i);/*** 刪除賬戶* @return*/int deleteAccount(); }AccountServiceImpl.java
package com.itheima.service.impl;import com.itheima.service.IAccountService;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService{@Overridepublic void saveAccount() {System.out.println("執行了保存");}@Overridepublic void updateAccount(int i) {System.out.println("執行了更新"+i);}@Overridepublic int deleteAccount() {System.out.println("執行了刪除");return 0;} }Logger.java
package com.itheima.utils;/*** 用于記錄日志的工具類,它里面提供了公共的代碼*/ public class Logger {/*** 用于打印日志:計劃讓其在切入點方法執行之前執行(切入點方法就是業務層方法)*/public void printLog(){System.out.println("Logger類中的pringLog方法開始記錄日志了。。。");} }bean.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"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"><!-- 配置srping的Ioc,把service對象配置進來--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean><!-- 配置Logger類 --><bean id="logger" class="com.itheima.utils.Logger"></bean><!--配置AOP--><aop:config><!--配置切面 --><aop:aspect id="logAdvice" ref="logger"><!-- 配置通知的類型,并且建立通知方法和切入點方法的關聯--><aop:before method="printLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:before></aop:aspect></aop:config></beans>AOPTest.java
package com.itheima.test;import com.itheima.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 測試AOP的配置*/ public class AOPTest {public static void main(String[] args) {//1.獲取容器ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.獲取對象IAccountService as = (IAccountService)ac.getBean("accountService");//3.執行方法as.saveAccount();as.updateAccount(1);as.deleteAccount();} }總結
以上是生活随笔為你收集整理的spring中基于XML的AOP配置步骤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot 入门——Spri
- 下一篇: 配置切入点表达式|| 前置通知、后置通知