使用Spring AOP实现租客租到满意的房子(50分) 需求: 1、 编写房东类Landlord.java文件,包含一个属性房东的名字name和一个方法house。(10分) 2、 编写中介代理类Agent.java,包含签合同和完成租房两个方法。(10分) 3、 编写application-context.xml配置文件。(20分) 4、 编写相应的测试类,得到结果。(10分)
时间: 2024-03-01 20:51:36 浏览: 12
1、Landlord.java
```java
public class Landlord {
private String name;
public Landlord(String name) {
this.name = name;
}
public void house() {
System.out.println(name + "的房子正在出租中...");
}
}
```
2、Agent.java
```java
public class Agent {
public void signContract() {
System.out.println("签署租赁合同...");
}
public void rentHouse() {
System.out.println("完成租房...");
}
}
```
3、application-context.xml
```xml
<?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="landlord" class="com.example.Landlord">
<constructor-arg value="John"/>
</bean>
<bean id="agent" class="com.example.Agent"/>
<aop:config>
<aop:aspect ref="agent">
<aop:pointcut id="rentHouse" expression="execution(* com.example.Landlord.house())"/>
<aop:before pointcut-ref="rentHouse" method="signContract"/>
<aop:after pointcut-ref="rentHouse" method="rentHouse"/>
</aop:aspect>
</aop:config>
</beans>
```
4、测试类
```java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
Landlord landlord = (Landlord) context.getBean("landlord");
landlord.house();
}
}
```
输出结果:
```
签署租赁合同...
John的房子正在出租中...
完成租房...
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)