zoukankan      html  css  js  c++  java
  • Spring(三)—— 依赖注入

    一、IOC

    Inverse Of  Control 控制反转,就是将原本在程序中手动创建UserService对象的控制权,交由Spring框架管理,简单说,就是创建UserService对象控制权被反转到了Spring框架。

    二、DI(Dependency Injection)

    依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件。

    例子:

    在UserService中提供一个get/set name的方法,我们希望属性有值,以前需要set方法设置值。在beans.xml中通过property去注入,这种方法就是依赖注入。

     三、案例代码

    IUserService.java

    1 package com.gyf.service;
    2 
    3 public interface IUserService {
    4     public void add();
    5 }
    View Code

    UserServiceImpl.java

     1 package com.gyf.service;
     2 
     3 public class UserServiceImpl implements IUserService {
     4 
     5     private String name;
     6 
     7     public String getName() {
     8         return name;
     9     }
    10 
    11     public void setName(String name) {
    12         this.name = name;
    13     }
    14 
    15     @Override
    16     public void add() {
    17         System.out.println("创建用户。。。" + name);
    18     }
    19 }
    View Code

    beans.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="
     5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7     <!--配置一个bean-->
     8     <bean id="userService" class="com.gyf.service.UserServiceImpl">
     9         <!--依赖注入数据,调用属性的set方法-->
    10         <property name="name" value="zhansan"></property>
    11     </bean>
    12 
    13 </beans>
    View Code

    测试类Lesson02.java

     1 package com.gyf.test;
     2 
     3 import com.gyf.service.IUserService;
     4 import com.gyf.service.UserServiceImpl;
     5 
     6 import org.junit.Test;
     7 import org.springframework.context.ApplicationContext;
     8 import org.springframework.context.support.ClassPathXmlApplicationContext;
     9 
    10 public class Lesson02 {
    11     @Test
    12     public void test1(){
    13         //方法一:以前用service
    14         IUserService userService = new UserServiceImpl();
    15         userService.add();
    16     }
    17 
    18     @Test
    19     public void test2(){
    20         //方法二:现在使用UserService方式从Spring容器获取
    21         //1.加载bean.xml这个Spring配置文件,内部就会创建对象
    22         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    23         //2.从Spring容器获取userService对象,通过id去找
    24         IUserService userService = (IUserService) context.getBean("userService");
    25         userService.add();
    26     }
    27 
    28     @Test
    29     public void test3(){
    30         //方法二:现在使用UserService方式从Spring容器获取
    31         //1.加载bean.xml这个Spring配置文件,内部就会创建对象
    32         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    33         //2.从Spring容器获取userService对象,通过id去找
    34         IUserService userService1 = (IUserService) context.getBean("userService");
    35         userService1.add();
    36 
    37         IUserService userService2 = (IUserService) context.getBean("userService");
    38         userService2.add();
    39 
    40         System.out.println(userService1);
    41         System.out.println(userService2);
    42         //结果,userService1和UserService2是同一个对象
    43     }
    44 }
    View Code
  • 相关阅读:
    linux中WDCP的日志彻底删除技巧
    jquery中页面Ajax方法$.load的功能
    PHP与MYSQL中UTF8 中文排序例子
    mysql获取group by的总记录行数方法
    jquery获取radio值
    python中数组与多维数组用法介绍
    php开启与关闭错误提示
    asp.net url址址中中文汉字参数传递乱码解决方法
    js事件之event.preventDefault()与(www.111cn.net)event.stopPropagation()用法区别
    css中position:fixed实现div居中
  • 原文地址:https://www.cnblogs.com/upyang/p/13560028.html
Copyright © 2011-2022 走看看