程序员

IOC注入

2017-09-09  本文已影响0人  常威爆打来福

一 属性注入介绍
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配置

    <!--使用有参构造注入属性-->
    <bean id="demo1" class="IOC.PropertyDemo1">
        <!-- 使用有参构造注入-->
    <constructor-arg name="username" value="小王"></constructor-arg></bean>

(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();
    }
}
结果显示

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配置

    <!--使用set方法注入属性-->
    <bean id="book" class="IOC.Book">
        <!-- 注入属性值
         name 属性值:类里面定义的属性名称
         value 属性值: 设置具体的值
        -->
        <property name="bookname" value="天龙八部"></property>
    </bean>

(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();
    }
}
运行结果

三 注入对象类型属性(重点)
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

   <bean id="userdao" class="IOC.UserDao"></bean>
    <bean id="userService" class="IOC.UserService">
        <!--注入dao对象
            name属性;service类里面属性名称
            ref属性(要注入哪个对象):dao配置bean标签中id值(不要写value属性,因为刚才是字符串,现在是对象)
        -->
        <property name="userDao" ref="userdao"></property>
    </bean>

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

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

运行结果

结果显示

四 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配置

    <!--p名称空间注入-->
    <bean id="person" class="IOC.Person" p:pname="Jack"></bean>

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();
    }
}
结果显示

五 注入复杂类型

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<String> list;
    private Map<String,String> map;
    private Properties properties;

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

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

    public void setMap(Map<String, String> 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配置

<bean id="person" class="IOC.Person">
        <!--数组-->
        <property name="arrs">
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>王麻子</value>
            </list>
        </property>
        <!--list-->
        <property name="list">
            <list>
                <value>张三list</value>
                <value>李四list</value>
                <value>王麻子list</value>
            </list>
        </property>
        <!--map-->
        <property name="map">
            <map>
                <entry key="1" value="张三map"></entry>
                <entry key="2" value="李四map"></entry>
                <entry key="3" value="王麻子map"></entry>
            </map>
        </property>
        <!--properties-->
        <property name="properties">
            <props>
                <prop key="driverclass">com.mysql.jdbc.Driver</prop>
                <prop key="username">root</prop>
            </props>
        </property>
    </bean>

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

上一篇 下一篇

猜你喜欢

热点阅读