站長資訊網
最全最豐富的資訊網站

spring ioc注入的三種方式是什么

spring ioc注入的三種方式是:1、Setter方法注入,是容器通過調用無參構造器或無參static工廠 方法實例化bean之后,調用該bean的setter方法。2、構造方法注入。3、P命名空間注入。

spring ioc注入的三種方式是什么

本教程操作環境:windows7系統、java8版本、Dell G3電腦。

Spring IOC(依賴注入的三種方式):

1、Setter方法注入

Setter方法注入是容器通過調用無參構造器或無參static工廠 方法實例化bean之后,調用該bean的setter方法,即實現了基于setter的依賴注入。

package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl;public class HelloWord { private HelloService helloService;       // setter方式注入Bean     public void setHelloService(HelloService helloService) {         this.helloService = helloService;     }       @Override     public void selfIntroduction() {         // 向大家打招呼         helloService.sayHello("大家好!");     }   }
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">     <!--        Bean聲明:          該bean類似于javaConfig中的@Bean注解;          用于創建bean的類通過class屬性來指定,并且需要使用全限定的類名。          通過id指定bean的ID。如果不顯示指定,默認使用class的全限定名進行命名。          eg:          com.jpeony.spring.common.HelloServiceImpl#0,其#0是一個計數器的形式,          用來區分相同類型的其他bean。          使用自動化命名很方便,但是沒有多少實際用處,還是建議自己給bean顯示設定ID。      -->     <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>       <!-- setter注入bean -->     <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">         <property name="helloService" ref="helloService"/>     </bean>   </beans>

2、構造方法注入

構造器依賴注入通過容器觸發一個類的構造器來實現的,該類有一系列參數,每個參數代表一個對其他類的依賴。

package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl;  public class HelloWord {     private HelloService helloService;       // 構造方法注入     public HelloWord (HelloService helloService) {         this.helloService = helloService;     }   }
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">     <!--        Bean聲明:          該bean類似于javaConfig中的@Bean注解;          用于創建bean的類通過class屬性來指定,并且需要使用全限定的類名。          通過id指定bean的ID。如果不顯示指定,默認使用class的全限定名進行命名。          eg:          com.jpeony.spring.common.HelloServiceImpl#0,其#0是一個計數器的形式,          用來區分相同類型的其他bean。          使用自動化命名很方便,但是沒有多少實際用處,還是建議自己給bean顯示設定ID。      -->     <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>       <!-- 構造方法注入bean -->     <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord">         <constructor-arg><ref bean="helloService"/></constructor-arg>     </bean>   </beans>

3、P命名空間注入

package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl;  public class HelloWord {     //名字     private String name;     //年齡     private String age;     //方法類     private HelloService helloService;       public void setName (String name) {         this.name = name;     }          public void setAge (String age) {         this.age = age;     }          public void setHelloService(HelloService helloService) {         this.helloService = helloService;     }       @Override     public void selfIntroduction() {         // 向大家打招呼         helloService.sayHello("我叫"+ name + ",今年" + age + "歲,大家好!");     }   }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        <!-- 引入p命名標簽 -->        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">          <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>       <!-- p標簽注入bean -->     <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"       p:name="明明" p:age="24" p:helloService-ref="helloService"></bean>   </beans>

P標簽注入集合bean

package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl; import java.util.List;  public class HelloWord {     //名字     private String name;     //年齡     private String age;     //方法類     private List<HelloService> helloServices;       public void setName (String name) {         this.name = name;     }          public void setAge (String age) {         this.age = age;     }          public void setHelloServices(List<HelloService> helloServices) {         this.helloServices = helloServices;     }       @Override     public void selfIntroduction() {         // 向大家打招呼         helloServices[0].sayHello("我叫"+ name + ",今年" + age + "歲,大家好!");     }   }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        <!-- 引入p命名標簽 -->        xmlns:p="http://www.springframework.org/schema/p"        <!-- 引入util命名標簽 -->        xmlns:util="http://www.springframework.org/schema/util"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">          <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>      <bean id="helloService2" class="com.jpeony.spring.common.HelloServiceImpl">     ...........     </bean>       <util:list id="helloServices">         <ref bean="helloService"/>         <ref bean="helloService2"/>     </util:list>      <!-- p標簽注入bean -->     <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"       p:name="明明" p:age="24" p:helloServices-ref="helloServices"></bean>   </beans>

贊(0)
分享到: 更多 (0)
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
亚洲精品国产专区91在线| 国模吧一区二区三区精品视频 | 九九久久国产精品| 国产精品久久久香蕉| 国产午夜精品一区理论片飘花| 少妇AV射精精品蜜桃专区| 久久精品一区二区三区资源网| 日韩在线一区二区| 亚洲精品国产精品| 无码日韩人妻精品久久蜜桃| 亚洲第一区精品日韩在线播放| 精品剧情v国产在线麻豆| 91久久精品一区二区| 国产系列高清精品第一页| 国产精品免费小视频| 亚洲精品中文字幕乱码| 窝窝午夜色视频国产精品东北| 日韩人妻无码精品无码中文字幕| 亚洲国产成人精品青青草原| 中文字幕日韩精品无码内射 | 国精品午夜福利视频不卡| 日韩有码在线视频| 精品亚洲视频在线| 亚洲欧洲精品在线| 精品久久久久久久久中文字幕| 亚洲?V无码成人精品区日韩| 国产精品亚洲天堂| 亚洲av无码成人精品国产 | 亚洲一级Av无码毛片久久精品| 国产精品久久久久一区二区| 99RE6热在线精品视频观看| 亚洲综合国产精品| 精品一区二区三区四区| 久久国产精品一区| 牛牛在线精品免费视频观看| 国产91精品久久久久999| 九九99久久精品国产| 999久久久无码国产精品| 99久久国产综合精品1尤物| 久久精品岛国av一区二区无码| 国产偷久久久精品专区|