# Spring AOP + AspectJ XML 配置示例

> 原文： [https://howtodoinjava.com/spring-aop/spring-aop-aspectj-xml-configuration-example/](https://howtodoinjava.com/spring-aop/spring-aop-aspectj-xml-configuration-example/)

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

> **阅读更多内容**：[使用注解配置](https://howtodoinjava.com/spring/spring-aop/spring-aop-aspectj-example-tutorial-using-annotation-config/)的 Spring AOP AspectJ 示例

## 1）如何声明切面

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

```java
<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspectBean">
   ...
   </aop:aspect>
</aop:config>

<bean id="loggingAspectBean" class="com.howtodoinjava.demo.aop.EmployeeCRUDLoggingAspect" />

```

## 2）如何定义切入点

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

```java
<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()`方法执行。

> **阅读更多内容**：[Spring AOP AspectJ 切入点表达式以及示例](https://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/)

## 3）定义建议

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

```java
<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`

```java
<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`

```java
public class EmployeeDTO {

	private Integer id;
	private String firstName;
	private String lastName;

	//Setters and Getters
}

```

`EmployeeManager.java`

```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`

```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`

```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`

```java
public class EmployeeCRUDTransactionAspect
{
	public void getEmployeeById(JoinPoint joinPoint)
	{
		System.out.println("EmployeeCRUDTransactionAspect.getEmployeeById() : "
				+ joinPoint.getSignature().getName());
	}
}

```

`TestAOP.java`

```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

```

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

**祝您学习愉快！**