Spring IoC練習(xí)包括兩部分:1.IoC/DI學(xué)習(xí) 2.AOP
IoC容器的作用:管理所有的bean及其相互間的依賴關(guān)系
配置有兩種格式:.xml和注解
反射:(1)概念:主要是指程序可以訪問码泞、檢測和修改它本身狀態(tài)或行為的一種能力(2.)反射解析
注入方式:(1.)構(gòu)造器注入用<constructor-arg> (2.) setter注入用property
依賴注入的例子
- 書P42到44
IoC綜合練習(xí):Boss ,Car ,Meeting
Car:屬性有brand、color、parameter
Boss:屬性有name饼暑、company婚被、car狡忙、hobbies
Meeting:屬性theme、bosses
綜合采用所學(xué)內(nèi)容址芯,設(shè)計并完成以上綜合練習(xí)
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="經(jīng)濟"/>
<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);
}
}