基于注解的aspectJ

一、知识点
基于注解的aop配置:
切面类:
@Aspect

通知类型:
@Before
@AfterReturning
@Around
@After
@AfterThrowing

在spring.xml中添加
aop:aspectj-autoproxy/

=================================================================================
spring整合 单元测试:基于注解
必须 导入 spring-test.jar专门用来测试的;

@Test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {“classpath:config/spring.xml”}

测试类中 使用 @Autowried自动 装配
导入的jar包和基于xml的一样
二、工程架构
(1)架构图
基于注解的aspectJ_第1张图片
(2)案例Code

1.com.oracle.aspect包:
package com.oracle.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {
    //切入点
    @Pointcut("execution(* com.oracle.serviceImpl.UserServiceImpl.*(..))")
    public void myPointCut(){

    }
    //前置通知
    //@Before(value = "myPointCut()")
    public void before(JoinPoint joinPoint){
        System.out.println("方法名:"+joinPoint.getSignature().getName());
        System.out.println("我是前置通知");
    }
    //后置通知
    //@AfterReturning(value = "myPointCut()",returning = "obj")
    public void afterReturning(JoinPoint joinPoint,Object obj){
        System.out.println("方法名:"+joinPoint.getSignature().getName()+",返回值:"+obj);
        System.out.println("我是后置通知");
    }
    //环绕通知
    //@Around(value = "myPointCut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("事务开启!");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("事务关闭!");
        System.out.println(proceed);
        return proceed;

    }
    //最终通知
    //@After(value ="myPointCut()")
    public void after(JoinPoint joinPoint){
        System.out.println("方法名:"+joinPoint.getSignature().getName());
        System.out.println("我是最终通知");
    }
    //异常通知
    @AfterThrowing(value = "myPointCut()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("方法名:"+joinPoint.getSignature().getName()+"异常:"+e.getMessage());
        System.out.println("我是异常通知");
    }
}
2.com.oracle.service包:
package com.oracle.service;

public interface UserService {
    public void addUser();
    public boolean deleteUser();
    public void updateUser();
}
3.com.oracle.serviceImpl包:
package com.oracle.serviceImpl;

import com.oracle.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
       System.out.println("添加。。。");
    }

    @Override
    public boolean deleteUser() {
        System.out.println("删除。。。");
        return false;
    }

    @Override
    public void updateUser() {
        //int a = 1/0;
        System.out.println("修改。。。");
    }
}
4.com.oracle.test包:
package com.oracle.test;

import com.oracle.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = {"classpath:config/applicationContext.xml"})
public class Demo {
    //    @Autowired
//    private UserService userService;
    @Test
    public void Testbefore(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = c.getBean(UserService.class);
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();

    }
    @Test
    public void TestafterReturning(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = c.getBean(UserService.class);
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();
    }
    @Test
    public void Testaround(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = c.getBean(UserService.class);
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();
    }
    @Test
    public void Testafter(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = c.getBean(UserService.class);
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();
    }
    @Test
    public void afterThrowing(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = c.getBean(UserService.class);
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();
    }
}
5.config包:


    
    
    
    



你可能感兴趣的:(java,服务器)