AOP-annotation

通过注解实现AOP

第一步 创建实体类Student.java

public class Student {
    private Integer id;
    private String name;
 }

第二步创建StudentDao.java类并添加注解

在类中创建persist方法,用来添加一个student

@Repository
@Scope("singleton")
public class StudentDao {
    public void persist(Student s){
        System.out.println("保存{"+s.getId()+":"+s.getName()+"}");
    }
}

@Repository注解等同于<bean id="studentDao" class="com.itlaobing.aop.annotation.dao.StudentDao" scope="singleton" />

@Scope("singleton")表示只能创建一个对象

第三步创建StudentService.java

在类中创建StudentDao.java类的对象,

@Service("studentService")
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    public void save(Student s){
        studentDao.persist(s);
    }

    public StudentDao getStudentDao() {
        return studentDao;
    }

    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
}

@Service("studentService")等同于

<bean id="studentService" class="com.itlaobing.aop.annotation.dao.StudentDao" scope="singleton" />

@Autowired自动装配

第四步创建StudentController.java

@Controller
public class StudentController {
    private StudentService studentService;
    public void signUp(Student s){
        studentService.save(s);
    }

    public StudentService getStudentService() {
        return studentService;
    }
@Autowired
@Qualifier("studentService")
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
}

@Controller等同于

<bean id="studentController" class="com.itlaobing.aop.annotation.dao.StudentDao" scope="singleton" />

@Autowired自动装配

第五步创建ServiceAspect.java

创建切点,创建切面

public class ServiceAspect {
    @Pointcut("execution(* com..service.*.save*(..))")
    private void servicePoint(){
    }
    @Before("servicePoint()")
    public void before(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"方法即将执行");
    }
    @AfterReturning(pointcut = "servicePoint()",returning = "xxx")
    public void afterResult(JoinPoint joinPoint,Object xxx){
        System.out.println(joinPoint.getSignature().getName()+"执行后的结果是:"+"【"+xxx+"】");
    }
    @AfterThrowing(pointcut = "servicePoint()",throwing = "ex")
    public void afterThrow(JoinPoint joinPoint,Throwable ex){
        System.out.println(joinPoint.getSignature().getName()+"执行时抛出了"+"【"+ex+"】");
    }
    @After("servicePoint()")
    public void after(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"方法执行结束");
    }
}

@Pointcut("execution(* com..service.*.save*(..))")确定切点

第六步提供配置文件aop-annotation.xml配置文件

<!-- 扫描 dao 包中所有的 所有的类  -->
<context:component-scan base-package="com.itlaobing.aop.annotation.dao"/>
<!-- 扫描 service 包中所有的 所有的类  -->
<context:component-scan base-package="com.itlaobing.aop.annotation.service"/>
<!-- 扫描 controller 包中所有的 所有的类  -->
<context:component-scan base-package="com.itlaobing.aop.annotation.controller"/>
<!--  扫描 aspect 包-->
<context:component-scan base-package="com.itlaobing.aop.annotation.aspect"/>
<!-- 告知容器需要为 注解 提供 自动代理支持  -->
<aop:aspectj-autoproxy/>

第七步测试创建AnnotationBasedAopTest.java

public class AnnotationBasedAopTest {
    public static void main(String[] args) {
        AbstractApplicationContext container=new ClassPathXmlApplicationContext("classpath*:com/**/annotation/aop-annotation.xml");
        StudentService studentService=container.getBean("studentService",StudentService.class);
        System.out.println(studentService.getClass());
        Student student=new Student();
        student.setId(1001);
        student.setName("琪琪");
        studentService.save(student);
        container.close();


    }
}

 

 

 

 

转载于:https://my.oschina.net/u/4008495/blog/2988118

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值