控制台输出:你好!我是xx,我的学号是xx,我的年龄是xx。 2、输出你的爱好,并同时在业务层输出:这是service层,正在执行xxx方法;在数据访问层输出:这是dao层,正在执行xxx方法; 3、配置前置通知、后置通知、返回通知及环绕通知,对上述两题进行增强。
时间: 2024-02-22 15:00:08 浏览: 79
好的,根据你的要求,以下是完整的代码实现和配置:
1. 实体类Student.java
```java
public class Student {
private String name;
private String studentNo;
private int age;
private String hobby;
// 省略getter和setter方法
}
```
2. 数据访问层(StudentDao)和业务层(StudentService)
- StudentDao.java
```java
public interface StudentDao {
Student getStudent();
}
```
- StudentDaoImpl.java
```java
public class StudentDaoImpl implements StudentDao {
@Override
public Student getStudent() {
System.out.println("这是dao层,正在执行getStudent方法");
Student student = new Student();
student.setName("Tom");
student.setStudentNo("20210001");
student.setAge(20);
student.setHobby("看电影");
return student;
}
}
```
- StudentService.java
```java
public interface StudentService {
Student getStudent();
}
```
- StudentServiceImpl.java
```java
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public Student getStudent() {
System.out.println("这是service层,正在执行getStudent方法");
Student student = studentDao.getStudent();
System.out.println("我的爱好是" + student.getHobby());
return student;
}
}
```
3. AOP配置文件(aop.xml)
```xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置StudentDao和StudentService的bean实例 -->
<bean id="studentDao" class="com.example.dao.StudentDaoImpl"/>
<bean id="studentService" class="com.example.service.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
<!-- 配置增强 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="studentServiceGetStudent"
expression="execution(* com.example.service.StudentService.getStudent(..))"/>
<!-- 配置前置通知 -->
<aop:before pointcut-ref="studentServiceGetStudent"
method="beforeAdvice"/>
<!-- 配置后置通知 -->
<aop:after pointcut-ref="studentServiceGetStudent"
method="afterAdvice"/>
<!-- 配置返回通知 -->
<aop:after-returning pointcut-ref="studentServiceGetStudent"
method="returningAdvice"
returning="result"/>
<!-- 配置环绕通知 -->
<aop:around pointcut-ref="studentServiceGetStudent"
method="aroundAdvice"/>
</aop:config>
<!-- 前置通知 -->
<bean id="beforeAdviceBean" class="com.example.advice.BeforeAdvice"/>
<!-- 后置通知 -->
<bean id="afterAdviceBean" class="com.example.advice.AfterAdvice"/>
<!-- 返回通知 -->
<bean id="returningAdviceBean" class="com.example.advice.ReturningAdvice"/>
<!-- 环绕通知 -->
<bean id="aroundAdviceBean" class="com.example.advice.AroundAdvice"/>
</beans>
```
4. 增强实现类
- BeforeAdvice.java
```java
public class BeforeAdvice {
public void before() {
System.out.println("执行前置通知");
}
}
```
- AfterAdvice.java
```java
public class AfterAdvice {
public void after() {
System.out.println("执行后置通知");
}
}
```
- ReturningAdvice.java
```java
public class ReturningAdvice {
public void returning(Object result) {
System.out.println("执行返回通知,返回值为:" + result.toString());
}
}
```
- AroundAdvice.java
```java
public class AroundAdvice {
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("执行环绕通知前");
Object result = pjp.proceed();
System.out.println("执行环绕通知后");
return result;
}
}
```
5. Main方法
```java
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
StudentService studentService = context.getBean(StudentService.class);
Student student = studentService.getStudent();
System.out.println("你好!我是" + student.getName() + ",我的学号是" + student.getStudentNo() + ",我的年龄是" + student.getAge() + "。");
}
}
```
在上面的代码中,我们通过在AOP配置文件中配置切入点和增强,实现了前置通知、后置通知、返回通知及环绕通知的相关功能。
需要注意的是,这里的aop.xml是AOP的配置文件,需要在其中配置StudentDao、StudentService的bean实例,以及增强实现类的bean实例。
阅读全文
相关推荐















