Simple implementation of Spring AOP Aspect (based on XML)

The first step: Lead Pack

 

Step two: cut class and implementation class

Service("userService")
public class IUserviceImpl implements IUserService{
private String name ;
@Override
public void add(User user) {

}

@Override
public void addUser() {
System.out.println("添加用户");
}

@Override
public void updateUser() {
System.out.println("更新用户");

}

@Override
int public deleteUser ( int ID ) {
the System. OUT .println ( "delete user ID" ) ;
return . 1 ;
}
@Override public void deleteUser () { the System. OUT .println ( "delete user" ) ; // int I 10/0 =; } public String getName () { return name ; } public void the setName ( String name ) { the this . name = name; }















@Override
public void the Add () {
. The System OUT .println ( "Create User" ) ;

}
}

 

@Component //AOP底层
@Aspect
public class MyAspect {

@Before("execution(* com.zz.service.UserServiceImpl.*(..))")
public void myBefore(JoinPoint joinPoint){
System.out.println("1 前置通知..."+joinPoint.getSignature().getName());
}

public void myAfterReturning(){
System.out.println("4 后置通知...");
}

/* public Object myAround() throws Throwable {
return myAround();
*} /
Public Object myAround ( ProceedingJoinPoint PJP ) throws the Throwable { . The System OUT .println ( "2 surrounding the open transaction notification ..." ) ; . The System OUT .println ( pjp.getSignature () .getName ()) ; // the entry point to the method name // release Object retObj = pjp.proceed () ; . System OUT .println ( "3 commit the transaction commits the transaction ..." ) ; return retObj; } public void myAfterThrowing










( JoinPoint jp,Throwable e) {
System.out.println("异常通知..." + jp.getSignature().getName() + "===" + e.getMessage());
}

public void myAfter(JoinPoint jp){

System.out.println("最终通知 ");

}

 

 

The third step: the Spring 's xml configuration

 

<!--  配置UserService-->
<bean id="userService" class="com.zz.service.UserServiceImpl"></bean>
<!-- 配置切面类对象-->
<bean id="myAspect" class="com.zz.aspect.MyAspect"></bean>

 

 <aop:config >

<- starting point:! 2 
expression The: Expression
foregoing method for each service are open transaction and end transaction
AOP: Configuration of a transaction log records -> < AOP : Aspect REF = "myAspect" > // specified section of location < AOP : the pointcut ID = "myPointcut" expression The = "(. * com.zz.service.UserServiceImpl * (..)) Execution" /> // pointcuts <- pre-configured notification ...! -> < AOP : before Method = "myBefore" the pointcut-REF = "myPointcut" /> ! <- arranged after advice ...--> < AOP : after returning- Method = "myAfterReturning" pointcut-ref







= "myPointcut" /> ! <- Configuration abnormality notification ..throwing = "e" value, the method parameter is the name .--> < AOP : After the throwing- Method = "myAfterThrowing" the pointcut-REF = "myPointcut" the throwing = "E" /> ! <- arranged around advice -> < AOP : around Method = "myAround" the pointcut-REF = "myPointcut" > </ AOP : around > ! <- final notification arranged -> < AOP : After Method = "myAfter" the pointcut-REF = "myPointcut" > </ AOP :after></aop:aspect>







</aop:config>

Step Four: Test

 

    
@Test
public void test()throws Exception{
  ApplicationContext applicationContext =new ClassPathXmlApplicationContext("Beans16.xml");


//从spring 容器获取userservice 对象
IUserService userService1= (IUserService) applicationContext.getBean("userService");

userService1.deleteUser();

}
}

 

 

 

 

 

 

 

 

 

 

 

AOP Alliance specifications

AOP achieve

specification

aspectj achieve

 

Guess you like

Origin www.cnblogs.com/orangezhangzz/p/11745905.html