原文: 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,如下所示:
<aop:config>
<aop:aspect id="loggingAspect" ref="loggingAspectBean">
...
</aop:aspect>
</aop:config>
<bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />
2)如何定义切入点
切入点有助于确定要使用不同建议执行的连接点。 切入点将定义如下:
<aop:config>
<aop:aspect id="loggingAspect" ref="loggingAspectBean">
<aop:pointcut id="loggingOperation"
expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
<aop:pointcut id="transactionOperation"
expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />
</aop:aspect>
</aop:config>
<bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />
在上面的示例中,两个切入点(loggingOperation
和transactionOperation
)将与EmployeeManager
类中定义的方法匹配。 其中loggingOperation
切入点将匹配EmployeeManager
中定义的所有方法,而transactionOperation
仅匹配EmployeeManager.getEmployeeById()
方法执行。
3)定义建议
您可以使用<aop:advise_name>
元素在<aop:aspect>
中声明五个建议中的任何一个,如下所示:
<aop:config>
<!-- Spring AOP Pointcut definitions -->
<aop:pointcut id="loggingOperation"
expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
<aop:pointcut id="transactionOperation"
expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />
<!-- Spring AOP aspect -->
<aop:aspect id="loggingAspect" ref="loggingAspectBean">
<!-- Spring AOP advises -->
<aop:before pointcut-ref="loggingOperation" method="logBefore" />
<aop:after pointcut-ref="loggingOperation" method="logAfter" />
</aop:aspect>
</aop:config>
基于 XML 的 Spring AOP 配置的示例应用程序
在此示例中,我在EmployeeManager
接口内定义的所有方法的日志建议之前和之后应用EmployeeManager.getEmployeeById()
方法的事务建议。 完整的配置文件如下:
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop/
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!-- We don't need to this; This is required only in annotation based AOP support -->
<!-- <aop:aspectj-autoproxy /> -->
<aop:config>
<!-- Spring AOP Pointcut definitions -->
<aop:pointcut id="loggingOperation"
expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.*(..))" />
<aop:pointcut id="transactionOperation"
expression="execution(* com.howtodoinjava.demo.aop.EmployeeManager.getEmployeeById(..))" />
<!-- Spring AOP aspect 1 -->
<aop:aspect id="loggingAspect" ref="loggingAspectBean">
<!-- Spring AOP advises -->
<aop:before pointcut-ref="loggingOperation" method="logBefore" />
<aop:after pointcut-ref="loggingOperation" method="logAfter" />
</aop:aspect>
<!-- Spring AOP aspect 2 -->
<aop:aspect id="transactionAspect" ref="transactionAspectBean">
<aop:before pointcut-ref="transactionOperation" method="getEmployeeById" />
</aop:aspect>
</aop:config>
<!-- Spring AOP aspect instances -->
<bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />
<bean id="transactionAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDTransactionAspect" />
<!-- Target Object -->
<bean id="employeeManager" class="com.howtodoinjava.demo.aop.EmployeeManagerImpl" />
</beans>
本示例中使用的其他文件是:
EmployeeDTO.java
public class EmployeeDTO {
private Integer id;
private String firstName;
private String lastName;
//Setters and Getters
}
EmployeeManager.java
public interface EmployeeManager
{
public EmployeeDTO getEmployeeById(Integer employeeId);
public List<EmployeeDTO> getAllEmployee();
public void createEmployee(EmployeeDTO employee);
public void deleteEmployee(Integer employeeId);
public void updateEmployee(EmployeeDTO employee);
}
EmployeeManagerImpl.java
public class EmployeeManagerImpl implements EmployeeManager
{
public EmployeeDTO getEmployeeById(Integer employeeId) {
System.out.println("Method getEmployeeById() called");
return new EmployeeDTO();
}
public List<EmployeeDTO> getAllEmployee() {
System.out.println("Method getAllEmployee() called");
return new ArrayList<EmployeeDTO>();
}
public void createEmployee(EmployeeDTO employee) {
System.out.println("Method createEmployee() called");
}
public void deleteEmployee(Integer employeeId) {
System.out.println("Method deleteEmployee() called");
}
public void updateEmployee(EmployeeDTO employee) {
System.out.println("Method updateEmployee() called");
}
}
EmployeeCRUDLoggingAspect.java
public class EmployeeCRUDLoggingAspect
{
public void logBefore(JoinPoint joinPoint)
{
System.out.println("EmployeeCRUDAspect.logBefore() : " + joinPoint.getSignature().getName());
}
public void logAfter(JoinPoint joinPoint)
{
System.out.println("EmployeeCRUDAspect.logAfter() : " + joinPoint.getSignature().getName());
}
}
EmployeeCRUDTransactionAspect.java
public class EmployeeCRUDTransactionAspect
{
public void getEmployeeById(JoinPoint joinPoint)
{
System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : "
+ joinPoint.getSignature().getName());
}
}
TestAOP.java
public class TestAOP
{
@SuppressWarnings("resource")
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("com/howtodoinjava/demo/aop/applicationContext.xml");
EmployeeManager manager = ( EmployeeManager ) context.getBean("employeeManager");
manager.getEmployeeById(1);
manager.createEmployee(new EmployeeDTO());
manager.deleteEmployee(100);
}
}
Output:
EmployeeCRUDAspect.logBefore() : getEmployeeById
EmployeeCRUDTransactionAspect.getEmployeeById() : getEmployeeById
Method getEmployeeById() called
EmployeeCRUDAspect.logAfter() : getEmployeeById
EmployeeCRUDAspect.logBefore() : createEmployee
Method createEmployee() called
EmployeeCRUDAspect.logAfter() : createEmployee
EmployeeCRUDAspect.logBefore() : deleteEmployee
Method deleteEmployee() called
EmployeeCRUDAspect.logAfter() : deleteEmployee
在下面的评论部分中给我任何评论/查询。
祝您学习愉快!