• Spring DI使用详解


    Spring DI使用详解

    一、介绍

    • DI的定义:依赖注入,为类里面的属性设值。例如,我们之前的setName方法就是在为name属性设值。
    • IOC与DI的关系:IOC进行对象的创建,DI进行值的注入。二者共同管理JavaBean,但DI是在IOC的基础上存在的,它不能单独存在。

    二、代码演示

    DI依赖注入也有两种方式,即配置文件注入和注解注入

    一、配置文件注入

    属性须知:

    • type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
    • index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值,索引的位置从 0 开始
    • name:用于指定给构造函数中指定名称的参数赋值(一般用这个)
    • value:用于提供基本类型和String类型的数据
    • ref:用于指定其他的bean类型数据,即bean的id

    前期代码准备:

    //Dao.Class文件
    public class Dao {
        private String testDI;
        public Dao(String testDI){
            this.testDI=testDI;
        }
    }
    //Service.Class文件
    public class Service {
        private Dao dao;
        private String test;
        private Map<String,String> map;
        private Properties properties;
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
        public void setDao(Dao dao) {
            this.dao = dao;
        }
        public void setDao(Dao dao) {
            this.dao = dao;
        }
        private String[] args;
        private List<String> list;
    
        public void setArgs(String[] args) {
            this.args = args;
        }
        public void setList(List<String> list) {
            this.list = list;
        }
    }
    

    applicationContext.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"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
       
    </beans>
    

    配置文件注入又分三种 :

    1. 使用有参构造注入(以Dao类为例)

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      
          <bean id="dao"  class="com.testWeb.dao.impl.Dao">
              <constructor-arg name="testDI" value="测试DI"></constructor-arg>
          </bean>
      </beans>
      
    2. 使用set方法注入(以Service类为例,注意:set方法注入为常用方法,且注入对象也较为重要,请牢牢掌握

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      
           <bean id="dao"  class="com.testWeb.dao.impl.Dao">
              <constructor-arg name="testDI" value="测试DI"></constructor-arg>
          </bean>
          <bean id="service" class="com.testWeb.service.Service">
              <property name="dao" ref="dao"></property>
          </bean>
      </beans>
      
    3. P名称空间注入

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
          <bean id="dao"  class="com.testWeb.dao.impl.Dao">
              <constructor-arg name="testDI" value="测试DI"></constructor-arg>
          </bean>
          <bean id="service" class="com.testWeb.service.Service" p:dao-ref="dao" p:test="测试"></bean>
      </beans>
      

    4.复杂属性的注入

    这里复杂属性的注入其实属于set注入,但由于代码量原因,就另起一点了。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--
            id:用于SpringIOC调用,可以为任意
            class:类的全路径
            -->
        <bean id="user"  class="com.testWeb.daomain.User"></bean>
    
        <!--开启注解扫描-->
        <context:component-scan base-package="com.testWeb"></context:component-scan>
        <bean id="dao"  class="com.testWeb.dao.impl.Dao">
            <constructor-arg name="testDI" value="测试DI"></constructor-arg>
        </bean>
        <bean id="service" class="com.testWeb.service.Service" p:dao-ref="dao" p:test="测试">
    		<!--数组-->
            <property name="args">
                <list>
                    <value>测试1</value>
                    <value>测试2</value>
                    <value>测试3</value>
                </list>
            </property>
    		<!-- List-->
            <property name="list">
                <list>
                    <value>测试1</value>
                    <value>测试2</value>
                    <value>测试3</value>
                </list>
            </property>
            <!--Map-->
            <property name="map">
                <map>
                    <entry key="name" value="LiMing"></entry>
                    <entry key="class" value="Class1"></entry>
                    <entry key="hoby" value="PingPang"></entry>
                </map>
            </property>
            <!--properties-->
            <property name="properties">
                <props>
                    <prop key="driverclass">com.mysql.jdbc.Driver</prop>
                </props>
            </property>
        </bean>
    </beans>
    

    二、注解注入

    第一步、开启注解包扫描

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--开启注解扫描-->
        <context:component-scan base-package="com.testWeb"></context:component-scan>
    </beans>
    

    第二步、利用注解创建对象并注入属性

    //Dao.class文件
    @Service(value = "dao")
    public class Dao {
    
        public void test(){
            System.out.println("test");
        }
    }
    //Service.class文件
    @Service(value = "service")
    public class Service {
        //得到dao对象
        //在dao属性上利用注解直接注入,使用注解不用set方法
        @Autowired    //自动装配
        private Dao dao;
        //name中注解创建对象的Value值
        @Resource(name="dao")
        private Dao dao1;
    }
    

    小节,一般在实际开发中,对JavaBean的管理一般是,配置文件进行对象的创建,注解进行属性的注入

  • 相关阅读:
    vector的erase函数
    结构体定义容易混淆的地方
    JavaScript重点知识
    JS中预解析案例分析
    浏览器console控制台不显示编译错误/警告
    强烈推荐一款强大的公式编辑器软件AxMath
    DIV+CSS布局
    CSS-常见属性
    CSS-定义样式表
    CSS-使用CSS样式的方式
  • 原文地址:https://www.cnblogs.com/xiaoyiStudent/p/12879277.html
Copyright © 2020-2023  润新知