1。属性注入
属性注入式通过setXxx()方法注入Bean属性值或依赖对象
1 package com.asm; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.beans.factory.BeanFactory; 5 import org.springframework.beans.factory.BeanFactoryAware; 6 import org.springframework.beans.factory.BeanNameAware; 7 import org.springframework.beans.factory.DisposableBean; 8 import org.springframework.beans.factory.InitializingBean; 9 10 public class Car { 11 private String brand; 12 private String color; 13 private int maxSpeed; 14 public String getBrand() { 15 return brand; 16 } 17 public void setBrand(String brand) { 18 this.brand = brand; 19 } 20 public String getColor() { 21 return color; 22 } 23 public void setColor(String color) { 24 this.color = color; 25 } 26 public int getMaxSpeed() { 27 return maxSpeed; 28 } 29 public void setMaxSpeed(int maxSpeed) { 30 this.maxSpeed = maxSpeed; 31 } 32 33 34 }
2.
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 17 <bean id="car" class="com.asm.Car"> 18 <property name="maxSpeed"><value>200</value></property> 19 <property name="brand"><value>红旗</value></property> 20 <property name="color"><value>red</value></property> 21 </bean> 22 23 </beans> 24
<property name="maxSpeed"><value>200</value></property>
仅要求类中必须有setMaxSpeed()方法
2.构造函数注入
2.1按类型匹配入参
1 package com.asm; 2 3 import org.springframework.beans.BeansException; 4 import org.springframework.beans.factory.BeanFactory; 5 import org.springframework.beans.factory.BeanFactoryAware; 6 import org.springframework.beans.factory.BeanNameAware; 7 import org.springframework.beans.factory.DisposableBean; 8 import org.springframework.beans.factory.InitializingBean; 9 10 public class Car { 11 private String brand; 12 private String color; 13 private int maxSpeed; 14 15 public Car(String brand, String color) { 16 super(); 17 this.brand = brand; 18 this.color = color; 19 } 20 public String getBrand() { 21 return brand; 22 } 23 public void setBrand(String brand) { 24 this.brand = brand; 25 } 26 public String getColor() { 27 return color; 28 } 29 public void setColor(String color) { 30 this.color = color; 31 } 32 public int getMaxSpeed() { 33 return maxSpeed; 34 } 35 public void setMaxSpeed(int maxSpeed) { 36 this.maxSpeed = maxSpeed; 37 } 38 39 40 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 17 <bean id="car" class="com.asm.Car"> 18 <constructor-arg type="java.lang.String"> 19 <value>红旗AAAAA</value> 20 </constructor-arg> 21 <constructor-arg type="java.lang.String"> 22 <value>red</value> 23 </constructor-arg> 24 </bean> 25 70 71 </beans>
2.通过索引入参
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 17 <bean id="car" class="com.asm.Car"> 18 <constructor-arg index="0"> 19 <value>红旗AAAAA</value> 20 </constructor-arg> 21 <constructor-arg index="1"> 22 <value>red</value> 23 </constructor-arg> 24 </bean> 25 26 27 28 </beans>
3.联合使用类型和索引入参
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 17 <bean id="car" class="com.asm.Car"> 18 <constructor-arg index="0" type="java.lang.String"> 19 <value>红旗AAAAA</value> 20 </constructor-arg> 21 <constructor-arg index="1" type="java.lang.String"> 22 <value>red</value> 23 </constructor-arg> 24 </bean> 25 26 </beans> 27
4.通过自身反射入参
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 16 17 <bean id="car" class="com.asm.Car"> 18 <constructor-arg index="0" type="java.lang.String"> 19 <value>红旗AAAAA</value> 20 </constructor-arg> 21 <constructor-arg index="1" type="java.lang.String"> 22 <value>red</value> 23 </constructor-arg> 24 <constructor-arg index="2" > 25 <ref bean="boss"/> 26 </constructor-arg> 27 </bean> 28 29 <bean id="boss" class="com.asm.Boss"></bean> 30 31 </beans>