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

  • 相关阅读:
    自我介绍
    constructor
    arrow function、function.apply
    for each...in
    类与对象与结构体的区别
    编程语言的发展史
    正则表达式中pw、IDCard和EM匹配
    正则表达式速记
    js中用户名的正则(字符,数字,下划线,减号)
    DOM与document的区别
  • 原文地址:https://www.cnblogs.com/Arno-vc/p/13387076.html
Copyright © 2011-2022 走看看