spring-IOC注入

**spring-IOC注入 ** xml格式 簡答的構造注入 先在src目錄下建立一個xml文件 , 最好取名爲applicationContext.xml ,而後在xml文件中複製spring配置文件所須要的約束spring

<?xml version="1.0" encoding="UTF-8"?>app

<beans xmlns="http://www.springframework.org/schema/beans"code

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xml

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>對象

而後把你寫程序過程當中所須要調用的類 ,而後在裏面聲明類的bean , 聲明就是把類的路徑 , id,name等信息告訴spring容器blog

<bean class="com.sxt.pojo.Cat" id="cat"></bean>

而後初始化容器get

public static void main(String[] args) {it

//初始化Spring容器,當Spring容器初始化時,會自動加載配置文件,而後根據配置文件中的內容初始化Beanio

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");class

//經過getBean(在配置文件中設置的id或者name)方法獲取到配置文件裏所需類

Cat cat = ac.getBean("cat");

System.out.println(cat);

}

這樣就獲取到了cat類

上面時直接獲取到類對象 , 那麼怎麼在經過spring容器獲取對象的時候同時給要獲取到的屬性賦值呢

能夠經過構造注入

構造注入時 ,注入幾個屬性 ,那麼該類就應該有所對應的構造方法 ,提供有參構造方法 , 同時必須提供無參構造的

<bean class="com.sxt.pojo.Cat" id="cat1">

//構造注入經過bean裏的constructor-arg來注入
         <constructor-arg  name="id" value="1" />
         <constructor-arg name="name" value="安安" />
     </bean>

1)構造注入能夠再構造器中決定依賴關係的注入順序,有限依賴的優先注入。例如,組件中其它依賴關係的注入,經常須要依賴於Datasource的注入。採用構造注入,能夠在代碼中清晰地決定注入順序。 2)對於依賴關係無需變化的Bean,構造注入更加有用。由於沒有setter方法,全部的依賴關係所有在構造器內設定。所以,無需擔憂後續代碼對依賴關係的破壞。 3)依賴關係只能在構造器中設定,則只有組建的建立者才能改變組建的依賴關係。隊組建的調用者而言,組件內部的依賴關係徹底透明,更符合高內聚的原則。

設置注入

須要類有無慘構造方法 , 和構造注入不一樣 , 設置注入能夠任意的爲任意的屬性設置 ,可是必需要有setter方法

<bean class="com.sxt.pojo.Cat" id="cat1">

//property是設置注入的關鍵字
         <property name="id" value="1" />
         <property name="name" value="安安" />
     </bean>

靜態工廠注入

public class StaticFactory {
    
    public static User getUser(){
        return new User();
    }
}

<bean class="com.staticFactory.StaticFactory" factory-method="getUser" id ="s" />

動態工廠注入

public class DynamicFactory {
    public  User getUser(String name){
        return new User(name);
    }

}

<bean class="com.dynamicFactory.DynamicFactory"  id ="d" />
 <bean factory-bean="d" factory-method="getUser"   id="dm"></bean>

還有對象注入 ,集合注入 ,map注入等

<bean class="com.sxt.pojo.Cat" id="cat1">
         <property name="id" value="1" />
         <property name="name" value="安安" />
     </bean>
     
     <bean class="com.sxt.pojo.User" id="user">
         <property name="all">
             <array> 
                 <value>1</value>
                 <value>8</value>
                 <value>23</value>
             </array>
         </property>
         
             <property name="cat">
             <array>
                 <ref bean="cat1"/>
                 <bean class="com.sxt.pojo.Cat">
                     <property name="id" value="2" />
                     <property name="name" value="wangwu" />
                 </bean>
             </array>
         </property>
         
         <property name="list">
             <list>
                 <ref bean="cat1"/>
                 <bean class="com.sxt.pojo.Cat">
                     <property name="id" value="3" />
                     <property name="name" value="list1" />
                 </bean>
                 <bean class="com.sxt.pojo.Cat">
                     <property name="id" value="4" />
                     <property name="name" value="list2" />
                 </bean>
             </list>
         </property>
         
         <property name="map">
             <map>
                 <entry key="key1" value="value1" />
                 <entry key="key2" value="value2" />
             </map>
         </property>
     </bean>

@org.junit.Test     public void test() {      ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");      User user =  ac.getBean("user",User.class);      int[] all = user.getAll();      for (int i : all) {          System.out.println(i);     }                Cat[] cats =  user.getCat();     for (Cat cat : cats) {         System.out.println(cat);     }          List<Cat> list = user.getList();     for (Cat cat : list) {         System.out.println(cat);     }               Map<String, String> map = user.getMap();     Set<String> set = map.keySet();     for (String s : set) {         System.out.println(s+":"+map.get(s));     }          }

相關文章
相關標籤/搜索