亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

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

    spring ioc注入的三種方式是:1、Setter方法注入,是容器通過(guò)調(diào)用無(wú)參構(gòu)造器或無(wú)參static工廠 方法實(shí)例化bean之后,調(diào)用該bean的setter方法。2、構(gòu)造方法注入。3、P命名空間注入。

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

    本教程操作環(huán)境:windows7系統(tǒng)、java8版本、Dell G3電腦。

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

    1、Setter方法注入

    Setter方法注入是容器通過(guò)調(diào)用無(wú)參構(gòu)造器或無(wú)參static工廠 方法實(shí)例化bean之后,調(diào)用該bean的setter方法,即實(shí)現(xiàn)了基于setter的依賴(lài)注入。

    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類(lèi)似于javaConfig中的@Bean注解;          用于創(chuàng)建bean的類(lèi)通過(guò)class屬性來(lái)指定,并且需要使用全限定的類(lèi)名。          通過(guò)id指定bean的ID。如果不顯示指定,默認(rèn)使用class的全限定名進(jìn)行命名。          eg:          com.jpeony.spring.common.HelloServiceImpl#0,其#0是一個(gè)計(jì)數(shù)器的形式,          用來(lái)區(qū)分相同類(lèi)型的其他bean。          使用自動(dòng)化命名很方便,但是沒(méi)有多少實(shí)際用處,還是建議自己給bean顯示設(shè)定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、構(gòu)造方法注入

    構(gòu)造器依賴(lài)注入通過(guò)容器觸發(fā)一個(gè)類(lèi)的構(gòu)造器來(lái)實(shí)現(xiàn)的,該類(lèi)有一系列參數(shù),每個(gè)參數(shù)代表一個(gè)對(duì)其他類(lèi)的依賴(lài)。

    package com.jpeony.spring.setter; import com.jpeony.spring.common.HelloServiceImpl;  public class HelloWord {     private HelloService helloService;       // 構(gòu)造方法注入     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類(lèi)似于javaConfig中的@Bean注解;          用于創(chuàng)建bean的類(lèi)通過(guò)class屬性來(lái)指定,并且需要使用全限定的類(lèi)名。          通過(guò)id指定bean的ID。如果不顯示指定,默認(rèn)使用class的全限定名進(jìn)行命名。          eg:          com.jpeony.spring.common.HelloServiceImpl#0,其#0是一個(gè)計(jì)數(shù)器的形式,          用來(lái)區(qū)分相同類(lèi)型的其他bean。          使用自動(dòng)化命名很方便,但是沒(méi)有多少實(shí)際用處,還是建議自己給bean顯示設(shè)定ID。      -->     <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/>       <!-- 構(gòu)造方法注入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;     //方法類(lèi)     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命名標(biāo)簽 -->        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標(biāo)簽注入bean -->     <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"       p:name="明明" p:age="24" p:helloService-ref="helloService"></bean>   </beans>

    P標(biāo)簽注入集合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;     //方法類(lèi)     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命名標(biāo)簽 -->        xmlns:p="http://www.springframework.org/schema/p"        <!-- 引入util命名標(biāo)簽 -->        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標(biāo)簽注入bean -->     <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"       p:name="明明" p:age="24" p:helloServices-ref="helloServices"></bean>   </beans>

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)