06 Spring Ioc学习

spring IoC练习包括两部分:1.IoC/DI学习 2.AOP

IoC容器的作用:管理所有的bean及其相互间的依赖关系

配置有两种格式:.xml和注解

反射:(1)概念:主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(2.)反射解析

注入方式:(1.)构造器注入用 (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 hobbys;
public Boss() {
}
public Boss(String name, String company, Car car, List 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 getHobbys() {
    return hobbys;
}
public void setHobbys(List 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 Bosses;

public Meeting() {
}

public String getTheme() {
    return theme;
}

@Override
public String toString() {
    return "Meeting{" +
            "theme='" + theme + '\'' +
            ", Bosses=" + Bosses +
            '}';
}

public List getBosses() {
    return Bosses;
}

public void setBosses(List bosses) {
    Bosses = bosses;
}

public void setTheme(String theme) {
    this.theme = theme;
}

public Meeting(String theme, List bosses) {
    this.theme = theme;
    Bosses = bosses;
}

}

配置






    
    
    



    
    
    



    
    
    
    
        
        baskball
        football
        
    



    
    
    
    
        
        跑步
        打球
        
    



    
    
    
    
        
            跑步
            羽毛球
        
    


    
    
        
        
        
        
        
    


    
    


    


    
    


主类

image

运行结果

image

你可能感兴趣的:(06 Spring Ioc学习)