Spring IoC容器与Bean管理 作业3-4
来源:3-4 自由编程
rudtjd
2023-03-02 17:01:17
package com.imooc.spring.ioc.entity;
public class Clothes {
private String style;
private String color;
public Clothes() {
}
public Clothes(String style, String color) {
this.style = style;
this.color = color;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Clothes{" +
"style='" + style + '\'' +
", color='" + color + '\'' +
'}';
}
}package com.imooc.spring.ioc.entity;
public class Person {
private String name;
private Clothes clothes;
public Person() {
}
public Person(String name, Clothes clothes) {
this.name = name;
this.clothes = clothes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Clothes getClothes() {
return clothes;
}
public void setClothes(Clothes clothes) {
this.clothes = clothes;
}
public void dress(){
System.out.println(name + "爱穿" + clothes.getColor() + clothes.getStyle());
}
}<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="Cat" class="com.imooc.spring.ioc.entity.Cat">--> <!-- <property name="name" value="喵喵"></property>--> <!-- <property name="age" value="2"></property>--> <!-- </bean>--> <!-- <bean id="Dog" class="com.imooc.spring.ioc.entity.Dog">--> <!-- <property name="name" value="旺旺"></property>--> <!-- <property name="age" value="5"></property>--> <!-- </bean>--> <bean id="skirt" class="com.imooc.spring.ioc.entity.Clothes"> <property name="color" value="红色"></property> <property name="style" value="连衣裙"></property> </bean> <bean id="suit" class="com.imooc.spring.ioc.entity.Clothes"> <property name="color" value="蓝色"></property> <property name="style" value="小西装"></property> </bean> <bean id="girl" class="com.imooc.spring.ioc.entity.Person"> <property name="name" value="女孩"></property> <property name="clothes" ref="skirt"></property> </bean> <bean id="boy" class="com.imooc.spring.ioc.entity.Person"> <property name="name" value="男孩"></property> <property name="clothes" ref="suit"></property> </bean> </beans>
package com.imooc.spring.ioc;
import com.imooc.spring.ioc.entity.Cat;
import com.imooc.spring.ioc.entity.Dog;
import com.imooc.spring.ioc.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
Person girl = context.getBean("girl", Person.class);
Person boy = context.getBean("boy", Person.class);
girl.dress();
boy.dress();
}
}1回答
同学你好,已完成练习,继续加油!
祝学习愉快!
相似问题