6、Spring LOC练习

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

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

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

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

注入方式:(1.)构造器注入用 (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;
    private List hobbies;

    public Boss(String name, String company, List car, List 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 getCar() {
        return car;
    }

    public void setCar(List car) {
        this.car = car;
    }

    public List getHobbies() {
        return hobbies;
    }

    public void setHobbies(List 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;

    public String getTheme() {
        return theme;
    }

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

    public List getBoss() {
        return boss;
    }

    public void setBoss(List boss) {
        this.boss = boss;
    }

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

    public Meeting() {
    }

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

配置

  • spring.xml
 
        
        
        
    
    
        
        
        
    
    
        
        
        
    
    
        
        
        
        
            
                阅读
                听音乐
            
        
    
    
        
        
        
        
            
                打乒乓球
                读书
            
        
    
    
        
        
        
        
            
                听音乐
                阅读
            
        
    
    
        
        
            
                
                
                
            
        
    

主类

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);
    }
}

运行结果

image

你可能感兴趣的:(6、Spring LOC练习)