通过注解实现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(); } }