Spring之依赖注入

IoC和DI的概念

  • IoC —–Inverse of Control,控制反转,将对象的创建权反转给Spring
  • DI—-Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中,

DI(依赖注入)

  • 例如:如果UserServiceImpl的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注入把该属性的值传入进来
  • 具体的配置如下:
  • <bean id = "us" class = "com.xd.demo1.UserServiceImpl">
    <property name= "uname" value="小王"/>
    </bean>

演示1

applicationContext.xml配置文件


  <!--使用bean标签-->
    <!--Class是接口的实现类的全路径。id自取一个唯一的名称-->
  <bean id="userService" class="cn.zst.test.UserServiceImpl">
    <property name="name" value="小王"></property>
  </bean>

UserService接口

public interface UserService {
     public void sayHello();
}

UserServiceImpl实现类

public class UserServiceImpl implements UserService {
    private String name;

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

    public void sayHello(){
        System.out.println("hello spring "+name);
    }


}

测试类

public class Demo2 {
    /*
    *
    * 原来的方式
    * */
    @Test
    public void run1(){
        UserServiceImpl us = new UserServiceImpl();
        us.setName("小江");
        us.sayHello();
    }

    /*
    * 依赖注入的方式
    *
    * */
    @Test
    public void run3(){
        //创建工厂,加载核心配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从工厂中获取到对象
        UserService us = (UserService)ac.getBean("userService");
        //调用对象的方法执行
        us.sayHello();
    }

    @Test
    public void run2(){
        //使用Spring的工厂
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过工厂获得类,此处参数为id值
        UserService userService =(UserService) applicationContext.getBean("userService");
        userService.sayHello();

    }
}

演示2

applicationContext.xml配置文件

<!--演示的依赖注入-->

  <bean id="customerDao" class="cn.zst.demo2.CustomerDaoImpl"/>


  <bean id="customerService" class="cn.zst.demo2.CustomerServiceImpl">
    <property name="customerDao" ref="customerDao"/>
  </bean>

CustomerDaoImpl类

public class CustomerDaoImpl {
    public void save(){

        System.out.println("持久层的Dao...");

    }
}

CustomerServiceImpl类

public class CustomerServiceImpl {
    private CustomerDaoImpl customerDao;

    public void setCustomerDao(CustomerDaoImpl customerDao) {
        this.customerDao = customerDao;
    }

    public void save(){
        System.out.println("业务层的service。。。");
        //原来方式
       // new CustomerDaoImpl().save();
       //注入方式
        customerDao.save();
    }
}

测试类

public class Demo3 {
    /*
    * 原始的方式
    * */
    @Test
    public void run1(){
        CustomerServiceImpl cs = new CustomerServiceImpl();
        cs.save();
    }

    /**
     * Spring 方式
     */
    @Test
    public void run2(){
    // 创建工厂,加载配置文件,CustomerDaoImpl创建了,CustomerServiceImpl被创创建了

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
         CustomerServiceImpl cs = (CustomerServiceImpl)ac.getBean("customerService");
        cs.save();

    }

}

猜你喜欢

转载自blog.csdn.net/u011301372/article/details/81385667