Spring DI使用詳解

Spring DI使用詳解

1、介紹

  • DI的定義:依賴注入,爲類裏面的屬性設值。例如,咱們以前的setName方法就是在爲name屬性設值。
  • IOC與DI的關係:IOC進行對象的建立,DI進行值的注入。兩者共同管理JavaBean,但DI是在IOC的基礎上存在的,它不能單獨存在。

2、代碼演示

DI依賴注入也有兩種方式,即配置文件注入和註解注入java

1、配置文件注入

屬性須知:mysql

  • type:用於指定要注入的數據的數據類型,該數據類型也是構造函數中某個或某些參數的類型
  • index:用於指定要注入的數據給構造函數中指定索引位置的參數賦值,索引的位置從 0 開始
  • name:用於指定給構造函數中指定名稱的參數賦值(通常用這個)
  • value:用於提供基本類型和String類型的數據
  • ref:用於指定其餘的bean類型數據,即bean的id

前期代碼準備:spring

//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配置文件sql

<?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類爲例)app

    <?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.複雜屬性的注入this

這裏複雜屬性的注入其實屬於set注入,但因爲代碼量緣由,就另起一點了。code

<?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>

2、註解注入

第一步、開啓註解包掃描

<?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的管理通常是,配置文件進行對象的建立,註解進行屬性的注入

相關文章
相關標籤/搜索