原文: https://howtodoinjava.com/spring-aop/spring-aop-aspectj-xml-configuration-example/

如果您是仍然对 JDK 1.4 感兴趣的极少数开发人员之一,或者您正在维护一个旧的 Spring 应用程序,其中 AOP 代码已用 XML 配置文件编写,那么此文章适合您。 在此处学习如何使用基于 xml 的配置来定义和使用 AspectJ 的 spring aop。

阅读更多内容使用注解配置的 Spring AOP AspectJ 示例

1)如何声明切面

使用<aop:aspect>元素声明一个切面,并使用ref属性引用该支持 bean,如下所示:

  1. <aop:config>
  2. <aop:aspect id="loggingAspect" ref="loggingAspectBean">
  3. ...
  4. </aop:aspect>
  5. </aop:config>
  6. <bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />

2)如何定义切入点

切入点有助于确定要使用不同建议执行的连接点。 切入点将定义如下:

  1. <aop:config>
  2. <aop:aspect id="loggingAspect" ref="loggingAspectBean">
  3. <aop:pointcut id="loggingOperation"
  4. expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
  5. <aop:pointcut id="transactionOperation"
  6. expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />
  7. </aop:aspect>
  8. </aop:config>
  9. <bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />

在上面的示例中,两个切入点(loggingOperationtransactionOperation)将与EmployeeManager类中定义的方法匹配。 其中loggingOperation切入点将匹配EmployeeManager中定义的所有方法,而transactionOperation仅匹配EmployeeManager.getEmployeeById()方法执行。

阅读更多内容Spring AOP AspectJ 切入点表达式以及示例

3)定义建议

您可以使用<aop:advise_name>元素在<aop:aspect>中声明五个建议中的任何一个,如下所示:

  1. <aop:config>
  2. <!-- Spring AOP Pointcut definitions -->
  3. <aop:pointcut id="loggingOperation"
  4. expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
  5. <aop:pointcut id="transactionOperation"
  6. expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />
  7. <!-- Spring AOP aspect -->
  8. <aop:aspect id="loggingAspect" ref="loggingAspectBean">
  9. <!-- Spring AOP advises -->
  10. <aop:before pointcut-ref="loggingOperation" method="logBefore" />
  11. <aop:after pointcut-ref="loggingOperation" method="logAfter" />
  12. </aop:aspect>
  13. </aop:config>

基于 XML 的 Spring AOP 配置的示例应用程序

在此示例中,我在EmployeeManager接口内定义的所有方法的日志建议之前和之后应用EmployeeManager.getEmployeeById()方法的事务建议。 完整的配置文件如下:

applicationContext.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/aop/
  7. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
  8. <!-- We don't need to this; This is required only in annotation based AOP support -->
  9. <!-- <aop:aspectj-autoproxy /> -->
  10. <aop:config>
  11. <!-- Spring AOP Pointcut definitions -->
  12. <aop:pointcut id="loggingOperation"
  13. expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
  14. <aop:pointcut id="transactionOperation"
  15. expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />
  16. <!-- Spring AOP aspect 1 -->
  17. <aop:aspect id="loggingAspect" ref="loggingAspectBean">
  18. <!-- Spring AOP advises -->
  19. <aop:before pointcut-ref="loggingOperation" method="logBefore" />
  20. <aop:after pointcut-ref="loggingOperation" method="logAfter" />
  21. </aop:aspect>
  22. <!-- Spring AOP aspect 2 -->
  23. <aop:aspect id="transactionAspect" ref="transactionAspectBean">
  24. <aop:before pointcut-ref="transactionOperation" method="getEmployeeById" />
  25. </aop:aspect>
  26. </aop:config>
  27. <!-- Spring AOP aspect instances -->
  28. <bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />
  29. <bean id="transactionAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDTransactionAspect" />
  30. <!-- Target Object -->
  31. <bean id="employeeManager" class="com.howtodoinjava.demo.aop.EmployeeManagerImpl" />
  32. </beans>

本示例中使用的其他文件是:

EmployeeDTO.java

  1. public class EmployeeDTO {
  2. private Integer id;
  3. private String firstName;
  4. private String lastName;
  5. //Setters and Getters
  6. }

EmployeeManager.java

  1. public interface EmployeeManager
  2. {
  3. public EmployeeDTO getEmployeeById(Integer employeeId);
  4. public List<EmployeeDTO> getAllEmployee();
  5. public void createEmployee(EmployeeDTO employee);
  6. public void deleteEmployee(Integer employeeId);
  7. public void updateEmployee(EmployeeDTO employee);
  8. }

EmployeeManagerImpl.java

  1. public class EmployeeManagerImpl implements EmployeeManager
  2. {
  3. public EmployeeDTO getEmployeeById(Integer employeeId) {
  4. System.out.println("Method getEmployeeById() called");
  5. return new EmployeeDTO();
  6. }
  7. public List<EmployeeDTO> getAllEmployee() {
  8. System.out.println("Method getAllEmployee() called");
  9. return new ArrayList<EmployeeDTO>();
  10. }
  11. public void createEmployee(EmployeeDTO employee) {
  12. System.out.println("Method createEmployee() called");
  13. }
  14. public void deleteEmployee(Integer employeeId) {
  15. System.out.println("Method deleteEmployee() called");
  16. }
  17. public void updateEmployee(EmployeeDTO employee) {
  18. System.out.println("Method updateEmployee() called");
  19. }
  20. }

EmployeeCRUDLoggingAspect.java

  1. public class EmployeeCRUDLoggingAspect
  2. {
  3. public void logBefore(JoinPoint joinPoint)
  4. {
  5. System.out.println("EmployeeCRUDAspect.logBefore() : " + joinPoint.getSignature().getName());
  6. }
  7. public void logAfter(JoinPoint joinPoint)
  8. {
  9. System.out.println("EmployeeCRUDAspect.logAfter() : " + joinPoint.getSignature().getName());
  10. }
  11. }

EmployeeCRUDTransactionAspect.java

  1. public class EmployeeCRUDTransactionAspect
  2. {
  3. public void getEmployeeById(JoinPoint joinPoint)
  4. {
  5. System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : "
  6. + joinPoint.getSignature().getName());
  7. }
  8. }

TestAOP.java

  1. public class TestAOP
  2. {
  3. @SuppressWarnings("resource")
  4. public static void main(String[] args)
  5. {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("com/howtodoinjava/demo/aop/applicationContext.xml");
  7. EmployeeManager manager = ( EmployeeManager ) context.getBean("employeeManager");
  8. manager.getEmployeeById(1);
  9. manager.createEmployee(new EmployeeDTO());
  10. manager.deleteEmployee(100);
  11. }
  12. }
  13. Output:
  14. EmployeeCRUDAspect.logBefore() : getEmployeeById
  15. EmployeeCRUDTransactionAspect.getEmployeeById() : getEmployeeById
  16. Method getEmployeeById() called
  17. EmployeeCRUDAspect.logAfter() : getEmployeeById
  18. EmployeeCRUDAspect.logBefore() : createEmployee
  19. Method createEmployee() called
  20. EmployeeCRUDAspect.logAfter() : createEmployee
  21. EmployeeCRUDAspect.logBefore() : deleteEmployee
  22. Method deleteEmployee() called
  23. EmployeeCRUDAspect.logAfter() : deleteEmployee

在下面的评论部分中给我任何评论/查询。

祝您学习愉快!