spring IoC练习包括两部分:1.IoC/DI学习 2.AOP
IoC容器的作用:管理所有的bean及其相互间的依赖关系
配置有两种格式:.xml和注解
反射:(1)概念:主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(2.)反射解析
注入方式:(1.)构造器注入用<constructor-arg> (2.) setter注入用property
spring常用的注解:
1、@Controller
2、@RequestMapping
3、@Resource和@Autowired
4、@ModelAttribute和 @SessionAttributes
5、@PathVariable
6、@requestParam
7、@ResponseBody
8、@Component
9、@Repository
10.@Data
依赖注入的例子
书P42到44
IoC综合练习:Boss ,Car ,Meeting
Boss类
package com.spring;
import java.util.List;
public class Boss {
private String name;
private String company;
private Car car;
private List<String> hobbys;
public Boss() {
}
public Boss(String name, String company, Car car, List<String> hobbys) {
this.name = name;
this.company = company;
this.car = car;
this.hobbys = hobbys;
}
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 Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
@Override
public String toString() {
return "Boss{" +
"name='" + name + '\'' +
", company='" + company + '\'' +
", car=" + car +
", hobbys=" + hobbys +
'}';
}
}
Car类
package com.spring;
public class Car {
private String brand;
private String color;
private String parameer;
public Car(String brand, String color, String parameer) {
this.brand = brand;
this.color = color;
this.parameer = parameer;
}
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 getParameer() {
return parameer;
}
public void setParameer(String parameer) {
this.parameer = parameer;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
", parameer='" + parameer + '\'' +
'}';
}
}
Meeting类
package com.spring;
import java.util.List;
public class Meeting {
private String theme;
private List<Boss> Bosses;
public Meeting() {
}
public String getTheme() {
return theme;
}
@Override
public String toString() {
return "Meeting{" +
"theme='" + theme + '\'' +
", Bosses=" + Bosses +
'}';
}
public List<Boss> getBosses() {
return Bosses;
}
public void setBosses(List<Boss> bosses) {
Bosses = bosses;
}
public void setTheme(String theme) {
this.theme = theme;
}
public Meeting(String theme, List<Boss> bosses) {
this.theme = theme;
Bosses = bosses;
}
}
配置
<bean id="car1" class="com.spring.Car">
<constructor-arg name="brand" value="奔腾"/>
<constructor-arg name="color" value="blue"/>
<constructor-arg name="parameer" value="2.0"/>
</bean>
<bean id="car2" class="com.spring.Car">
<constructor-arg name="brand" value="奥迪"/>
<constructor-arg name="color" value="black"/>
<constructor-arg name="parameer" value="1.8T"/>
</bean>
<bean id="car3" class="com.spring.Car">
<constructor-arg name="brand" value="大众"/>
<constructor-arg name="color" value="red"/>
<constructor-arg name="parameer" value="8.5"/>
</bean>
<bean id="bosses1" class="com.spring.Boss">
<property name="name" value="杨幂"/>
<property name="company" value="嘉年华"/>
<property name="car" ref="car1"/>
<property name="hobbys">
<list>
<value>baskball</value>
<value>football</value>
</list>
</property>
</bean>
<bean id="bosses2" class="com.spring.Boss">
<property name="name" value="马云"/>
<property name="company" value="阿里巴巴"/>
<property name="car" ref="car2"/>
<property name="hobbys">
<list>
<value>跑步</value>
<value>打球</value>
</list>
</property>
</bean>
<bean id="bosses3" class="com.spring.Boss">
<property name="name" value="朱一龙"/>
<property name="company" value="办公室"/>
<property name="car" ref="car3"/>
<property name="hobbys">
<list>
<value>跑步</value>
<value>羽毛球</value>
</list>
</property>
</bean>
<bean id="meeting" class="com.spring.Meeting">
<property name="theme" value="时间"/>
<property name="Bosses">
<list>
<ref bean="bosses1"/>
<ref bean="bosses2"/>
<ref bean="bosses3"/>
</list>
</property>
</bean>
<bean id="max" class="com.spring.Max">
<constructor-arg name="a" value="5"/>
<constructor-arg name="b" value="3"/>
</bean>
<bean id="userLogin" class="experise.UserLogin">
<constructor-arg name="user" ref="User"></constructor-arg>
</bean>
<bean id="User" class="experise.User">
<constructor-arg name="account" value="admin"/>
<constructor-arg name="password" value="111"/>
</bean>
网友评论