zoukankan      html  css  js  c++  java
  • spring——通过xml文件配置IOC容器

    1. 创建相关的类(这里是直接在之前类的基础上进行修改)

      package com.guan.dao;
      
      public interface Fruit {
          String getFruit();
      }
      
      package com.guan.dao;
      
      public class FruitImpl implements Fruit {
          public String getFruit() {
              return "Buy Some fruit";
          }
      }
      
      package com.guan.service;
      
      import com.guan.dao.Fruit;
      
      public interface UserService {
          String buyFruit();
          void setFruit(Fruit fruit);
      }
      
      package com.guan.service;
      
      import com.guan.dao.Apple;
      import com.guan.dao.Fruit;
      import com.guan.dao.FruitImpl;
      
      public class UserServiceImpl implements UserService{
          Fruit fruit;
      
          public UserServiceImpl() {
              fruit = new FruitImpl();
          }
      
          public String buyFruit() {
              return fruit.getFruit();
          }
      
          public void setFruit(Fruit fruit){
              this.fruit = fruit;
          }
          
      }
      
    2. 在resources目录下添加配置文件:beans.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              https://www.springframework.org/schema/beans/spring-beans.xsd">
      
          <bean id="fruit" class="com.guan.dao.FruitImpl"></bean>
          <bean id="apple" class="com.guan.dao.Apple"></bean>
      
          <bean id="userService" class="com.guan.service.UserServiceImpl">
              <property name="fruit" ref="fruit"></property>
          </bean>
      
      </beans>
      

      注:

      (1).即便没有属性需要初始化也需要通过<bean>来对其进行实例化,而不再需要new

      (2).<property>的注入需要类中存在set方法

      (3).对于属性的赋值的两种方式

      • 对于基本类型的赋值可以用value属性
      • 对于对象类型可以用ref(reference)属性以及<bean>中的id初始化
    3. 创建并使用实例

      import com.guan.dao.Fruit;
      import com.guan.service.UserService;
      import com.guan.service.UserServiceImpl;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class MyTest {
      
          public static void main(String[] args) {
              //create and configure beans
              ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
              //retrieve configured instance
              UserService userService = (UserService) context.getBean("userService");
              System.out.println(userService.buyFruit());
          }
      }
      
    4. 其它相关配置

    5. alias:给bean起别名,可以用多个不同的别名取出同一个类的实例

    6. bean

      (1).id:bean的唯一标识符

      (2).class:bean对象所应的全限定名(包名+类名)

      (3).name:也是别名,可以取同时取多个别名

    7. import:用于团队开发使用,可以将多个配置文件导入合并为一个.那么在调用时只要导入一个配置文件即可

  • 相关阅读:
    C/C++程序员必备的15个编辑器和集成开发环境
    天猫浏览型应用的CDN静态化架构演变
    实用技巧:如何用 CSS 做到完全垂直居中
    JavaScript 字符串操作:substring, substr, slice
    Hybrid App 开发初探:使用 WebView 装载页面
    引领潮流!最顶尖的24个获奖网页作品
    HTML5编程之旅系列一:HTML5 Geolocation 初探
    JavaScript 秘密花园——对象的使用和属性操作
    提高效率!15款最好的 Bug 跟踪应用程序
    最常用的8款 PHP 调试工具,你用过吗?
  • 原文地址:https://www.cnblogs.com/Arno-vc/p/13387076.html
Copyright © 2011-2022 走看看