整個類包以下:
html
注意:若是採用了AOP配置文件,這個前置通知就不須要寫了,只須要經過註釋說明就能夠了。前置通知在切面類裏面申明。
java
package com.yuan.aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class MymethodBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { // TODO Auto-generated method stub } }
package com.yuan.service; public interface UserService { public Integer add(String abc); public void aduser(String abc); }
package com.yuan.service.impl; import org.springframework.stereotype.Service; import com.yuan.service.UserService; @Service public class UserServiceimpl implements UserService { @Override public Integer add(String abc) { System.out.println("添加用戶信息1。"+abc); return 123; } @Override public void aduser(String abc) { // TODO Auto-generated method stub System.out.println("添加用戶信息2。"); } }
package com.yuan.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect //切面類 public class Log { /*** * 配置切入點,該方法無方法體 * 注意切入申明的格式,execution(Integer(返回的類型) com.yuan.service.UserService.add(String)) * */ @Pointcut("execution(* com.yuan.service..ad*(..))")//申明切人點,一個切入點會有不少的鏈接點 public void daddff(){};//配置切入點,該方法無方法體 /*** * 配置前置通知 * @param joinpoint */ @Before("daddff()") public void dddbefore1(JoinPoint joinPoint){ System.out.println("before添加日誌"+joinPoint); } /*** * 配置後置通知 * @param joinpoint */ @After("daddff()") public void after(){ System.out.println("after添加日誌"); } /*** * 配置環繞通知 * @param joinpoint */ @Around("daddff()") public void around(JoinPoint joinPoint){ System.out.println("開始around添加日誌"); ProceedingJoinPoint pjp=(ProceedingJoinPoint)joinPoint; try { pjp.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("結束around添加日誌"); } }
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com" /> <aop:aspectj-autoproxy/>//採用了註釋實現自動裝配實現代理 </beans>
package com.yuan.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yuan.service.UserService; public class AopTest { public static void main(String[] args){ ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ac.getBean("userServiceimpl"); userService.add("123oscar"); //userService.aduser("456"); } }
測試結果:spring
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).express
log4j:WARN Please initialize the log4j system properly.apache
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.編程
開始around添加日誌spring-mvc
before添加日誌execution(Integer com.yuan.service.UserService.add(String))mvc
添加用戶信息1。123oscarapp
結束around添加日誌ide
after添加日誌
若是不採用spring的配置文件以及註釋來申明的話,就須要實現不少的類。
好比:
通知類-用於說明切面類具備的功能.MethodBeforeAdvice
切面類-用於說明具體的須要切入的功能,這個是重點要作的,好比添加日誌的功能就在這裏log。
代理對象-用於申明要往哪裏去切入日誌功能的業務邏輯類proxyFactory。
被代理對象-具體說明業務邏輯模塊。好比Userserviceimpl 用於實現添加用戶等功能。
若是不用任何註釋。
這裏首先申明瞭切面類,
而後申明瞭業務邏輯類,
而後申明瞭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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- <context:component-scan base-package="com" /> <aop:aspectj-autoproxy/>--> <!-- 切面類 --> <bean id="log" class="com.yuan.aop.Log1"></bean> <!-- 用戶管理 --> <bean id="userServiceimpl" class="com.yuan.service.impl.UserServiceimpl"></bean> <!-- aop --> <aop:config> <!-- 切點 --> <aop:pointcut expression="execution(* com.yuan.service..*.*(..))" id="pointcut"/> <aop:aspect ref="log"> <!-- 通知 --> <aop:before method="dddbefore1" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans>
package com.yuan.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component; @Component public class Log1 { /*** * 配置切入點,該方法無方法體 * */ public Integer dddbefore1(JoinPoint joinPoint){ System.out.println("before添加日誌"+joinPoint); return 123; } public void after(){ System.out.println("after添加日誌"); } public void around(JoinPoint joinPoint){ System.out.println("開始around添加日誌"); ProceedingJoinPoint pjp=(ProceedingJoinPoint)joinPoint; try { pjp.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("結束around添加日誌"); } }