Aspectj

Aspectj是AOP框架,扩展了java语言

 

接口:

public interface IUserService {
public void select();
public void insert();
public void update();
public void delete();
}


实现类:
public class UserServiceImpl implements IUserService {
public void select(){
System.out.println("select ok!");
}

public void insert() {
//int result=5/0;
System.out.println("insert ok!");
}

public void update() {
System.out.println("update ok!");
}

public void delete() {
System.out.println("delete ok!");
}
}




@Aspect
public class MyAspect{
@Pointcut("execution(* day010aspectj.*.insert(..))")
public void insert(){}

@Pointcut("execution(* day010aspectj.*.update(..))")
public void update(){}

@Pointcut("execution(* day010aspectj.*.delete(..))")
public void delete(){}
//01.前置增强
@Before("execution(* day010aspectj.*.select(..))")
public void beforeAdvice(){
System.out.println("====before====");
}

//02.后置增强
@AfterReturning("execution(* day010aspectj.*.select(..))")
public void afterAdvice(){
System.out.println("====after====");
}

//环绕增强
@Around("execution(* day010aspectj.*.delete(..))")
public void aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("before====");
pjp.proceed();
System.out.println("====after====");
//前后的隔离点
}

//异常增强

@AfterThrowing("execution(* day010aspectj.*.insert(..))")
public void aroundAdvice() throws Throwable {
System.out.println("出错误了");
//前后的隔离点
}
//最终增强:类似于finally
@After("execution(* day010aspectj.*.update(..))")
public void finallyAdvice(){
System.out.println("finallyAdvice==================");
//前后的隔离点
}
}


配置文件 :
<!--1.目标对象-->
<bean id="service" class="day010aspectj.UserServiceImpl"></bean>

<!--2.增强/通知-->
<bean id="aroundAdvice" class="day010aspectj.MyAspect"></bean>

<!--3.aspectj自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>


测试类:
public class Text0010 {
@Test
public void t1(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext18aspectj.xml");
IUserService service=( IUserService) context.getBean("service");
service.select();
service.delete();
service.insert();
service.update();


}

}




Aspectj基于XML的AOP实现

public interface SomeService {
public void doSome();
public void doAny();
}




public interface SomeService1 {
public void doFirst();
public void doSecond();
public void doThird();
}




public class SomeServiceimpl implements SomeService {
public void doSome() {
int i=5/0;
System.out.println("dosome");
}
public void doAny(){
System.out.println("doany");
}

}




public class SomeServiceimpl1 implements SomeService1 {

public void doFirst() {
System.out.println("1");
}

public void doSecond() {
System.out.println("2");
}

public void doThird() {
int i=5/0;
System.out.println("3");
}
}





public class MyAspect1 {
// 前置通知
public void before(){
System.out.println("前置通知方法before()");
}
public void before(JoinPoint jp){
System.out.println("前置通知方法before() jp = " + jp);
}

// 后置通知
public void afterReturing(){
System.out.println("后置通知方法");
}
public void afterReturing(String result){
System.out.println("后置通知方法 result = " + result);
}

// 环绕通知
public void around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕通知方法,目标方法执行之前");
// 执行目标方法
Object result = pjp.proceed();
System.out.println("环绕通知方法,目标方法执行之后");
// return ((String)result).toUpperCase();
}

// 异常通知
public void afterThrowing(){
System.out.println("异常通知方法");
}
public void afterThrowing(Exception ex){
System.out.println("异常通知方法 ex = " + ex.getMessage());
}

// 最终通知
public void after(){
System.out.println("最终通知方法");
}


}





public class MyAspect {
   /* @Before("execution(* *..day05aspect.*.doSome(..))")
public void MyBefore(){
System.out.println("before");
}
@AfterReturning("execution(* *..day05aspect.*.doAny(..))")
public void MyAfter(){
System.out.println("after");
}*/
@Pointcut("execution(* *..day05aspect.*.doAny(..))")
public void doAny(){}
@Pointcut("execution(* *..day05aspect.*.doSome(..))")
public void doSome(){}
@Around("doAny()")
public void MyAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("brfore");
joinPoint.proceed();
System.out.println("after");
}
@AfterThrowing(value = "doSome()",throwing="erro")
public void MyThow(Exception erro){
System.out.println("throw"+erro);
}
@After("doSome()")
public void MyLastAfter(){
System.out.println("last");
}

}




配置类:

<bean id="service" class="day05aspect.SomeServiceimpl1"></bean>
<bean id="myAspect" class="day05aspect.MyAspect1"></bean>
<aop:config>
<aop:pointcut expression="execution(* *..SomeService1.doFirst(..))" id="doFirstPointcut"/>
<aop:pointcut expression="execution(* *..SomeService1.doSecond(..))" id="doSecondPointcut"/>
<aop:pointcut expression="execution(* *..SomeService1.doThird(..))" id="doThirdPointcut"/>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut-ref="doFirstPointcut"/>
<aop:before method="before(org.aspectj.lang.JoinPoint)" pointcut-ref="doFirstPointcut"/>
<aop:after-returning method="afterReturing" pointcut-ref="doSecondPointcut"/>
<aop:after-returning method="afterReturing(java.lang.String)" pointcut-ref="doSecondPointcut" returning="result"/>
<aop:around method="around" pointcut-ref="doSecondPointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="doThirdPointcut"/>
<aop:after-throwing method="afterThrowing(java.lang.Exception)" pointcut-ref="doThirdPointcut" throwing="ex"/>
<aop:after method="after" pointcut-ref="doThirdPointcut"/>
</aop:aspect>

</aop:config>



测试类:

public class test5aspect {
@Test
public void t2(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextday05aspect.xml");
SomeService proxyService =(SomeService) context.getBean("service");
try {
proxyService.doSome();
}catch (Exception e){

}
proxyService.doAny();
}
@Test
public void t3(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextday05aspect.xml");
SomeService1 proxyService =(SomeService1) context.getBean("service");
proxyService.doFirst();
proxyService.doSecond();
try {
proxyService.doThird();
}catch (Exception e){

}
}
}
 
 
posted @ 2018-03-13 08:54  我不是.好人  阅读(137)  评论(0编辑  收藏  举报