Spring IoC练习包括两部分:1.IoC/DI学习 2.AOP
IoC容器的作用:管理所有的bean及其相互间的依赖关系
配置有两种格式:.xml和注解
反射:(1)概念:主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(2.)反射解析
注入方式:(1.)构造器注入用<constructor-arg> (2.) setter注入用property
依赖注入的例子
- 书P42到44
IoC综合练习:Boss ,Car ,Meeting
-
Car:属性有brand、color、parameter
-
Boss:属性有name、company、car、hobbies
-
Meeting:属性theme、bosses
综合采用所学内容,设计并完成以上综合练习
Boss类
package com.spring;
import java.util.List;
public class Boss {
private String name;
private String company;
private List<Car> car;
private List<String> hobbies;
public Boss(String name, String company, List<Car> car, List<String> hobbies) {
this.name = name;
this.company = company;
this.car = car;
this.hobbies = hobbies;
}
public Boss() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public List<Car> getCar() {
return car;
}
public void setCar(List<Car> car) {
this.car = car;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "Boss{" +
"name='" + name + '\'' +
", company='" + company + '\'' +
", car=" + car +
", hobbies=" + hobbies +
'}';
}
}
Car类
package com.spring;
public class Car {
private String brand;
private String color;
private String parameter;
public Car(String brand, String color, String paramter) {
this.brand = brand;
this.color = color;
this.parameter = parameter;
}
public Car() {
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
", parameter='" + parameter + '\'' +
'}';
}
}
Meeting类
package com.spring;
import java.util.List;
public class Meeting {
private String theme;
private List<Boss> boss;
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public List<Boss> getBoss() {
return boss;
}
public void setBoss(List<Boss> boss) {
this.boss = boss;
}
public Meeting(String theme, List<Boss> boss) {
this.theme = theme;
this.boss = boss;
}
public Meeting() {
}
@Override
public String toString() {
return "Meeting{" +
"theme='" + theme + '\'' +
", boss=" + boss +
'}';
}
}
配置
- spring.xml
<bean id="car1" class="com.spring.Car">
<property name="brand" value="兰博基尼"/>
<property name="color" value="蓝色"/>
<property name="parameter" value="4.1"/>
</bean>
<bean id="car2" class="com.spring.Car">
<property name="brand" value="奔驰"/>
<property name="color" value="红色"/>
<property name="parameter" value="4.1"/>
</bean>
<bean id="car3" class="com.spring.Car">
<property name="brand" value="宝马"/>
<property name="color" value="白色"/>
<property name="parameter" value="4.1"/>
</bean>
<bean id="boss1" class="com.spring.Boss">
<property name="name" value="梁"/>
<property name="company" value="电子科技公司"/>
<property name="car" ref="car1"/>
<property name="hobbies">
<list>
<value>阅读</value>
<value>听音乐</value>
</list>
</property>
</bean>
<bean id="boss2" class="com.spring.Boss">
<property name="name" value="李"/>
<property name="company" value="科技公司"/>
<property name="car" ref="car2"/>
<property name="hobbies">
<list>
<value>打乒乓球</value>
<value>读书</value>
</list>
</property>
</bean>
<bean id="boss3" class="com.spring.Boss">
<property name="name" value="萌"/>
<property name="company" value="文化公司"/>
<property name="car" ref="car3"/>
<property name="hobbies">
<list>
<value>听音乐</value>
<value>阅读</value>
</list>
</property>
</bean>
<bean id="meeting" class="com.spring.Meeting">
<property name="theme" value="经济"/>
<property name="boss">
<list>
<ref bean="boss1"/>
<ref bean="boss2"/>
<ref bean="boss3"/>
</list>
</property>
</bean>
主类
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MeetingPrinter {
public static void main(String[] args) {
@SuppressWarnings("resources")
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Meeting meeting = context.getBean(Meeting.class);
System.out.println(meeting);
}
}
网友评论