XML方式配置AOP
代码:
1AopSD.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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:component-scan base-package="Spring.Service"/>
<bean id="loggerAspect" class="Spring.Aspect.ShouDongAspect"/>
<!-- 手动配置AOP:-->
<aop:config>
<!-- 配置的切点 触发的条件-->
<aop:pointcut id="pointcut"
expression="execution(* Spring.Service.ProductService.doSome(..))"/>
<!-- 切面和通知-->
<aop:aspect ref="loggerAspect" order="2">
<!-- method 就是Aspect 类中真实存在的方法-->
<!-- point-ref 到切点-->
<aop:around method="SDaround" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
2.Aspect
package Spring.Aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class ShouDongAspect {
public Object SDaround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("start log:" + joinPoint.getSignature().getName());
Object obj = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return obj;
}
}
3.Service
package Spring.Service;
import org.springframework.stereotype.Component;
@Component("ps")
public class ProductService {
public void doSome(){
System.out.println("do Something");
}
public String doreturn(){
System.out.println("doreturn ");
return "return了一个值";
}
}
4.Test
package Spring.AOP.AopSDTest;
import Spring.Service.ProductService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/Aop/AopSD.xml")
public class AopSD {
@Autowired
private ProductService psd;
@Test
private void aopSd(){
psd.doSome();
}
}
使用AspectJ开发AOP
xml中的配置:
<aop:aspectj-autoproxy/>
**使用到的注解:
@Aspect
注解了这是一个Aspect
@Order(n)
n为正整数 规定了Aspect之间的执行顺序
@PointCut(value=(execution(* 类.*.(..))))
定义了切点
public void poitcut_Name(){}
//方法名就是切点名
@Before(value=“pointcut_Name() ”)//前置通知
public void before(JoinPoint joinPoint){
System.out.println("前置通知=================="+joinPoint);
}
//后置通知有返回值的可以获取返回值
@AfterReturning(value="myPointcut2()",returning = "result")**
public void afterReturing(Object result){
System.out.println("后置通知=================="+result);
}
//环绕通知Around
@Around(value="myPointcut3()")
public Object around(ProceddingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前通知================");
Object object = joinPoint.proceed();// 执行目标方法
System.out.println("环绕后通知================");
return obj;
}
//最终通知:
@After(value="myPointcut5()")
public void after() {
System.out.println("最终通知==================");
}
//异常通知 AfterThrowing
@AfterThrowing(value="myPointcut4()",throwing = "e")
public void afterThrowing(Throwable e) {
System.out.println("异常抛出通知=============="+e.getMessage());
}
AspectJ代码:
1.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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:component-scan base-package="Spring.Service"/>
<context:component-scan base-package="Spring.Aspect"/>
<aop:aspectj-autoproxy/>
</beans>
2.Service
package Spring.Service;
import org.springframework.stereotype.Component;
@Component("ps")
public class ProductService {
public void doSome(){
System.out.println("do Something");
}
public String doreturn(){
System.out.println("doreturn ");
return "return了一个值";
}
}
3.Aspect:
3.1永远在前的OrderAspect:
package Spring.Aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(1)
@Component
@Aspect
public class OrderAspect {
@Before(value = "orderPointcut()")
public void beforee(){
System.out.println( " 我永远在前面");
}
@Pointcut(value = "execution(* Spring.Service.ProductService.*(..))")
public void orderPointcut(){}
}
3.2LoggerAspect
package Spring.Aspect;
import Spring.Service.ProductService;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LoggerAspect {
@Before(value = "beforePoint()")
public void before(){
System.out.println( " Aspect Before");
}
@Around(value ="aroundPoint()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前通知================");
Object obj = joinPoint.proceed(); // 执行目标方法
System.out.println("环绕后通知================");
return obj;
}
@AfterReturning(value = "afterReturning()",returning="result")
private void afterAspect(Object result){
System.out.println("后置通知 :"+result);
}
@Pointcut(value = "execution(* Spring.Service.ProductService.*(..))")
public void beforePoint(){}
@Pointcut(value = "execution(* Spring.Service.ProductService.*(..))")
private void aroundPoint(){}
@Pointcut(value = "execution(* Spring.Service.ProductService.doreturn(..))")
public void afterReturning(){}
}
4.Test
使用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(“classpath:/Aop/Aop1.xml”)
直接又省掉了ApplicationContext的使用
package Spring.Aop1;
import Spring.Service.ProductService;
import javafx.application.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/Aop/Aop1.xml")
public class AopTest1 {
@Autowired
private ProductService pro;
@Test
public void aop1(){
pro.doSome();
System.out.println(" ----------");
pro.doreturn();
}
}