使用原始xml文件方式完成 spring AOP项目的搭建;完成各种通知
时间: 2024-05-14 19:12:08 浏览: 78
1. 创建项目
首先,我们需要创建一个基于Maven的Spring项目。在pom.xml文件中添加以下依赖项:
```xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
</dependencies>
```
2. 创建目录结构
在src/main目录下创建以下目录结构:
```
src/main/java
└── com
└── example
└── aop
├── aspect
│ ├── AfterReturningAspect.java
│ ├── AfterThrowingAspect.java
│ ├── AroundAspect.java
│ ├── BeforeAspect.java
│ └── LoggingAspect.java
├── service
│ ├── CustomerService.java
│ └── CustomerServiceImpl.java
└── MainApp.java
```
3. 编写Java类
3.1 CustomerService接口
```java
public interface CustomerService {
void addCustomer();
String addCustomerReturnValue();
void addCustomerThrowException() throws Exception;
void addCustomerAround(String name);
}
```
3.2 CustomerServiceImpl实现类
```java
public class CustomerServiceImpl implements CustomerService {
private String name;
private String url;
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public void addCustomer() {
System.out.println("addCustomer() is running ");
}
public String addCustomerReturnValue() {
System.out.println("addCustomerReturnValue() is running ");
return "abc";
}
public void addCustomerThrowException() throws Exception {
System.out.println("addCustomerThrowException() is running ");
throw new Exception("Generic Error");
}
public void addCustomerAround(String name){
System.out.println("addCustomerAround() is running, args : " + name);
}
}
```
3.3 日志切面类
```java
public class LoggingAspect {
public void beforeAdvice(JoinPoint joinPoint){
System.out.println("Going to setup student profile.");
}
public void afterAdvice(JoinPoint joinPoint){
System.out.println("Student profile has been setup.");
}
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() );
}
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString());
}
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Around advice : Before method execution");
proceedingJoinPoint.proceed();
System.out.println("Around advice : After method execution");
}
}
```
3.4 AspectJ切面类
```java
public class BeforeAspect {
public void beforeAdvice(JoinPoint joinPoint){
System.out.println("Before method:" + joinPoint.getSignature());
}
}
```
```java
public class AfterReturningAspect {
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() );
}
}
```
```java
public class AfterThrowingAspect {
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString());
}
}
```
```java
public class AroundAspect {
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Around advice : Before method execution");
proceedingJoinPoint.proceed();
System.out.println("Around advice : After method execution");
}
}
```
4. 编写配置文件
4.1 applicationContext.xml
```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">
<!-- bean definition -->
<bean id="customerService" class="com.example.aop.service.CustomerServiceImpl">
<property name="name" value="Cnblogs" />
<property name="url" value="http://www.cnblogs.com" />
</bean>
<!-- AspectJ Config -->
<bean id="loggingAspect" class="com.example.aop.aspect.LoggingAspect"></bean>
<bean id="beforeAspect" class="com.example.aop.aspect.BeforeAspect"></bean>
<bean id="afterReturningAspect" class="com.example.aop.aspect.AfterReturningAspect"></bean>
<bean id="afterThrowingAspect" class="com.example.aop.aspect.AfterThrowingAspect"></bean>
<bean id="aroundAspect" class="com.example.aop.aspect.AroundAspect"></bean>
<!-- AOP Config -->
<aop:config>
<aop:aspect id="logging" ref="loggingAspect">
<aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/>
<aop:before pointcut-ref="service" method="beforeAdvice"/>
<aop:after pointcut-ref="service" method="afterAdvice"/>
<aop:after-returning pointcut-ref="service" returning="retVal" method="afterReturningAdvice" />
<aop:after-throwing pointcut-ref="service" throwing="ex" method="AfterThrowingAdvice" />
<aop:around pointcut-ref="service" method="aroundAdvice" />
</aop:aspect>
<aop:aspect id="before" ref="beforeAspect">
<aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/>
<aop:before pointcut-ref="service" method="beforeAdvice"/>
</aop:aspect>
<aop:aspect id="afterReturning" ref="afterReturningAspect">
<aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/>
<aop:after-returning pointcut-ref="service" returning="retVal" method="afterReturningAdvice" />
</aop:aspect>
<aop:aspect id="afterThrowing" ref="afterThrowingAspect">
<aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/>
<aop:after-throwing pointcut-ref="service" throwing="ex" method="AfterThrowingAdvice" />
</aop:aspect>
<aop:aspect id="around" ref="aroundAspect">
<aop:pointcut expression="execution(* com.example.aop.service.*.*(..))" id="service"/>
<aop:around pointcut-ref="service" method="aroundAdvice" />
</aop:aspect>
</aop:config>
</beans>
```
5. 编写启动类
```java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.aop.service.CustomerService;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService customerService = (CustomerService) context.getBean("customerService");
customerService.addCustomer();
System.out.println("------------------------------");
customerService.addCustomerReturnValue();
System.out.println("------------------------------");
try {
customerService.addCustomerThrowException();
} catch (Exception e) {
}
System.out.println("------------------------------");
customerService.addCustomerAround("w3cschool.cc");
}
}
```
6. 运行结果
```
Going to setup student profile.
addCustomer() is running
Student profile has been setup.
------------------------------
Going to setup student profile.
addCustomerReturnValue() is running
Returning:abc
Student profile has been setup.
------------------------------
Going to setup student profile.
addCustomerThrowException() is running
There has been an exception: java.lang.Exception: Generic Error
Student profile has been setup.
------------------------------
Around advice : Before method execution
addCustomerAround() is running, args : w3cschool.cc
Around advice : After method execution
```
阅读全文