设计Person类和Address类, Address类,有属性 province,city Person类有name,age,address,favorite(爱好)属性,其中address属性是Address类的一个实例,favorite为列表。给出以下各个要求的配置文档和测试文档 1:请使用构造器注入,注册并创建一个(小明,21岁,福建福州,[足球、听歌、旅行])的bean实例,singleton模式: 2:请使用属性注入,注册并创建一个(小李,20岁,江苏南京,[跳舞、读书、看电影])的bean实例,prototype模式 3:请使用注解注入,注册并创建一个(小王,23岁,江西南昌,[跳舞、唱歌、旅游])的bean实例, 该实例创建时需打印出:“我创建了”,销毁前打印“我销毁了”
时间: 2023-09-23 07:11:06 浏览: 39
配置文档:beans.xml
```xml
<beans>
<!-- 小明,构造器注入,singleton模式 -->
<bean id="xiaoming" class="com.example.Person" scope="singleton">
<constructor-arg value="小明"/>
<constructor-arg value="21"/>
<constructor-arg>
<bean class="com.example.Address">
<property name="province" value="福建"/>
<property name="city" value="福州"/>
</bean>
</constructor-arg>
<property name="favorite">
<list>
<value>足球</value>
<value>听歌</value>
<value>旅行</value>
</list>
</property>
</bean>
<!-- 小李,属性注入,prototype模式 -->
<bean id="xiaoli" class="com.example.Person" scope="prototype">
<property name="name" value="小李"/>
<property name="age" value="20"/>
<property name="address">
<bean class="com.example.Address">
<property name="province" value="江苏"/>
<property name="city" value="南京"/>
</bean>
</property>
<property name="favorite">
<list>
<value>跳舞</value>
<value>读书</value>
<value>看电影</value>
</list>
</property>
</bean>
<!-- 小王,注解注入 -->
<bean id="xiaowang" class="com.example.Person" init-method="init" destroy-method="destroy">
<property name="name" value="小王"/>
<property name="age" value="23"/>
<property name="address">
<bean class="com.example.Address">
<property name="province" value="江西"/>
<property name="city" value="南昌"/>
</bean>
</property>
<property name="favorite">
<list>
<value>跳舞</value>
<value>唱歌</value>
<value>旅游</value>
</list>
</property>
</bean>
</beans>
```
测试文档:Test.java
```java
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 小明
Person xiaoming = (Person) context.getBean("xiaoming");
System.out.println(xiaoming.getName() + " " + xiaoming.getAge() + "岁,来自" + xiaoming.getAddress().getProvince() + xiaoming.getAddress().getCity() + ",爱好:" + xiaoming.getFavorite());
// 小李
Person xiaoli = (Person) context.getBean("xiaoli");
System.out.println(xiaoli.getName() + " " + xiaoli.getAge() + "岁,来自" + xiaoli.getAddress().getProvince() + xiaoli.getAddress().getCity() + ",爱好:" + xiaoli.getFavorite());
// 小王
Person xiaowang = (Person) context.getBean("xiaowang");
System.out.println(xiaowang.getName() + " " + xiaowang.getAge() + "岁,来自" + xiaowang.getAddress().getProvince() + xiaowang.getAddress().getCity() + ",爱好:" + xiaowang.getFavorite());
}
}
```
Address.java
```java
package com.example;
public class Address {
private String province;
private String city;
public Address() {
}
public Address(String province, String city) {
this.province = province;
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
```
Person.java
```java
package com.example;
import java.util.List;
public class Person {
private String name;
private int age;
private Address address;
private List<String> favorite;
public Person() {
}
public Person(String name, int age, Address address, List<String> favorite) {
this.name = name;
this.age = age;
this.address = address;
this.favorite = favorite;
}
public void init() {
System.out.println("我创建了");
}
public void destroy() {
System.out.println("我销毁了");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<String> getFavorite() {
return favorite;
}
public void setFavorite(List<String> favorite) {
this.favorite = favorite;
}
}
```