IOC注入

一 属性注入介绍
1 创建对象时候,向类里面属性设置值。
2 Java设置属性的三种方法
(1) 使用set方法注入

public class User {
 private  String name;
 public void setName(String name){
     this.name = name;
 }
}


User user = new User();
user.setName("abcd");

(2) 有参构造注入

public class User {
 private  String name;
 public User(String name){
     this.name = name;
 }
}

User user = new User("Jack");

(3) 使用接口注入

public interface User {
 public void delete(String name);
}


public class UserImpl implements User{
    private String name;
    public void delete(String name) {
    this.name = name;
    }
}

3 在Spring框架里面,支持前两种方式
(1)set方法注入(重点)
(2)有参构造注入
二 Spring框架属性注入
1 有参构造注入
(1) 有参构造

package IOC;

/**
 * Created by pc on 2017/9/9.
 */
public class PropertyDemo1 {
    private String username;

    public PropertyDemo1(String username) {
        this.username = username;
    }
    public void test1(){
        System.out.println("demo.........."+username);
    }
}

(2).xml配置

  • constructor-arg
    • name属性值:类里面定义的属性名称
    • value属性值: 设置具体的值
    
    
        
    

(3)测试

import Bean.User;
import IOC.PropertyDemo1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by pc on 2017/9/7.
 */
public class TextIOC {
    public static void main(String[] args) {
        //加载Spring配置文件,并创建对象
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
        //得到配置的对象
        PropertyDemo1 demo1 = (PropertyDemo1) context.getBean("demo1");
        demo1.test1();
    }
}
IOC注入_第1张图片
结果显示

2 set方法注入
(1)测试setBook类

package IOC;

/**
 * Created by pc on 2017/9/9.
 */
public class Book {
    private String bookname;

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public void demobook(){
        System.out.println("book.........."+bookname);
    }
}

(2).xml配置

  • property
    • name属性值:类里面定义的属性名称
    • value属性值: 设置具体的值
    
    
        
        
    

(3)测试代码

import Bean.User;
import IOC.Book;
import IOC.PropertyDemo1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by pc on 2017/9/7.
 */
public class TextIOC {
    public static void main(String[] args) {
        //加载Spring配置文件,并创建对象
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
        //得到配置的对象
        Book book = (Book) context.getBean("book");
        book.demobook();
    }
}
IOC注入_第2张图片
运行结果

三 注入对象类型属性(重点)
1 UserDao

package IOC;
public class UserDao {
    public void dao(){
        System.out.println("dao.................");
    }
}

2 UserService

package IOC;
public class UserService {
    //1.定义dao类型属性
    private UserDao userDao;
    //2.生成set方法
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("servie.................");
        userDao.dao();
    }
}

3 .xml

   
    
        
        
    

注释:
(1)先配置UserDAO和UserService两个对象
(2)在要被注入的UserService配置中,配置注入UserDao

  • name属性;service类里面属性名称
  • ref属性(要注入哪个对象):dao配置bean标签中id值(不要写value属性,因为刚才是字符串,现在是对象)
    4 测试
import Bean.User;
import IOC.Book;
import IOC.PropertyDemo1;
import IOC.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TextIOC {
    public static void main(String[] args) {
        //加载Spring配置文件,并创建对象
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
        //得到配置的对象
        UserService us = (UserService) context.getBean("userService");
        us.add();
    }
}

运行结果

IOC注入_第3张图片
结果显示

四 P名称空间注入
1 Person

package IOC;
public class Person {
    private String pname;

    public void setPname(String pname) {
        this.pname = pname;
    }
    public void test(){
        System.out.println("person........."+pname);
    }
}

2 .xml配置

    
    

3 测试

import Bean.User;
import IOC.Book;
import IOC.Person;
import IOC.PropertyDemo1;
import IOC.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TextIOC {
    public static void main(String[] args) {
        //加载Spring配置文件,并创建对象
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
        //得到配置的对象
       Person person= (Person) context.getBean("person");
       person.test();
    }
}
IOC注入_第4张图片
结果显示

五 注入复杂类型

  • 数组
  • list集合
  • map集合
  • properties
    1 Person
package IOC;

import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by pc on 2017/9/9.
 */
public class Person {
    private String pname;
    private String arrs[];
    private List list;
    private Map map;
    private Properties properties;

    public void setArrs(String[] arrs) {
        this.arrs = arrs;
    }

    public void setList(List list) {
        this.list = list;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }
    public void test(){
        System.out.println("person........."+pname);
        System.out.println("arrs...."+arrs);
        System.out.println("list...."+list);
        System.out.println("map...."+map);
        System.out.println("properties......"+properties);
    }
}

2 .xml配置


        
        
            
                张三
                李四
                王麻子
            
        
        
        
            
                张三list
                李四list
                王麻子list
            
        
        
        
            
                
                
                
            
        
        
        
            
                com.mysql.jdbc.Driver
                root
            
        
    

3 测试

import Bean.User;
import IOC.Book;
import IOC.Person;
import IOC.PropertyDemo1;
import IOC.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TextIOC {
    public static void main(String[] args) {
        //加载Spring配置文件,并创建对象
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
        //得到配置的对象
      Person person= (Person) context.getBean("person");
      person.test();
    }
}
IOC注入_第5张图片
结果显示

六 IOC和DI区别
1 IOC:控制反转,把对象创建交给spring进行配置
2 DI:依赖注入,向类里面的属性中设置值
3 关系:依赖注入不能单独存在,需要在IOC基础之上完成操作

你可能感兴趣的:(IOC注入)