一、Spring IOC(依賴注入的三種方式):
1、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>
二、Spring IOC(依賴注入的常用數據類型):
1、注入直接量(基本數據類型、字符串)
<bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="name" value="明明"></property> <property name="age" value="24"></property> </bean>
2、引用其他Bean組件(面向接口編程)
使用ref屬性:
<bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="helloService" ref="helloService"></property> </bean>
使用ref標簽:
<bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="helloService"> <ref bean="helloService" /> </property> </bean>
使用P命名空間:
<!-- 頭文件加上這句 --> xmlns:p="http://www.springframework.org/schema/p" <bean id="helloService" class="com.jpeony.spring.common.HelloServiceImpl"/> <bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord" p:helloService-ref="helloService"></bean>
3、使用內部Bean
<bean id="HelloWord" class="com.jpeony.spring.setter.HelloWord"> <property name="helloService"> <bean class="com.jpeony.spring.common.HelloServiceImpl" /> </property> </bean>
4、集合類型的屬性
// 對應的getter setter public class ALLCollection { private List listElement; private String[] arrayElement; private Set setElement; private Map mapElement; private Properties propsElement; public void setListElement (List listElement) { this.listElement = listElement; } public List getListElement () { return listElement; } public void setArrayElement (String[] arrayElement) { this.arrayElement= arrayElement; } public String[] getArrayElement () { return arrayElement; } public void setSetElement (Set setElement) { this.setElement= setElement; } public Set getSetElement () { return setElement; } public void setMapElement (Map mapElement) { this.mapElement= mapElement; } public Map getMaptElement () { return mapElement; } public void setPropsElement (Properties propsElement) { this.propsElement= propsElement; } public Properties getpropsElement () { return propsElement; } }
<bean id="collection" class="com.zxf.DO.ALLCollection"> <property name="listElement"> <list> <value>list蘋果</value> <value>list香蕉</value> </list> </property> <property name="arrayElement"> <array> <value>array蘋果</value> <value>array香蕉</value> </array> </property> <property name="setElement"> <set> <value>set蘋果</value> <value>set香蕉</value> </set> </property> <property name="mapElement"> <map> <entry> <key><value>map1</value></key> <value>map蘋果</value> </entry> <entry> <key><value>map2</value></key> <value>map香蕉</value> </entry> </map> </property> <property name="propsElement"> <props> <prop key="prop1">prop蘋果</prop> <prop key="porp2">prop香蕉</prop> </props> </property> </bean>
文章整合至:https://blog.csdn.net/joe18576558921/article/details/80973385、https://www.cnblogs.com/DDiamondd/p/11398678.html