<!--創建bean的第一種方式:使用默認無參構造函數 在默認情況下: 它會根據默認無參構造函數來創建類對象。如果 bean 中沒有默認無參構造函數,將會創建失敗--> <bean id="service" class="service.Impl.ServiceImpl" scope="prototype"></bean> <!--第二種方式,使用普通工廠中的方法創建對象(使用某個類中的方法創建對象並且使用容器)--> <bean id="factory" class="factory.InstanceFactory"></bean> <bean id="factoryService" factory-bean="factory" factory-method="getService"></bean> <!--第三種方法,使用工廠中的靜態方法創建對象(使用某個類中的靜態方法創建對象,並存入Spring容器)--> <bean id="staticFactory" class="factory.StaticInstance" factory-method="getService"></bean>
spring依賴注入的三種方式
1.使用構造函數提供(使用較少)
<?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"> <!--注入的方式: 1.使用構造函數提供 2.使用set方法提供(使用較多) 3.使用注解提供--> <!--======================== 1.使用構造函數提供(使用較少)=============================--> <bean id="service" class="service.Impl.ServiceImpl"> <!--constructor-arg 里面的屬性 type:用於要注入的數據的類型,該類型也是構造函數中某個或者某個的參數類型 index:用於要注入的數據給構造函數中指定索引位置色參數賦值,索引的位置是從0開始 name: 用於指定給構造函數中指定名稱的參數復制(建議使用) =============以上三個用於指定給哪個參數復制================= value:用於提供基本數據類型和string的數據 ref:用於指定其他bean類型數據.它指的是在spring的ioc核心容器中出現過的bean對象 注意!因為是構造提供所以必須要給value賦值,即使不用到也得提供參數 --> <constructor-arg name="name" value="------測試"></constructor-arg> <constructor-arg name="age" value="19"></constructor-arg> <constructor-arg name="birthday" ref="now"> </constructor-arg><!--constructor標簽不可以編譯日期--> <!--ref--> </bean> <!--配置日期--> <bean id="now" class="java.util.Date"></bean> </beans>
2.使用set方法提供(使用較多)
<?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"> <!--注入的方式: 1.使用構造函數提供(使用較少) 2.使用set方法提供(使用較多) 3.使用注解提供--> <!--========================使用set方法提供(使用較多)=============================--> <bean id="service" class="service.Impl.ServiceImpl"> <!--property標簽 bean的內部 1.name:用於指定注入的所調用的set方法名稱 --> <property name="name" value="test"/> <property name="age" value="24"/> <property name="birthday" ref="now"/> </bean> <bean id="now" class="java.util.Date"/> </beans>
2.1.復雜依賴注入
<?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"> <!--注入的方式: 1.使用構造函數提供(使用較少) 2.使用set方法提供(使用較多) 3.復雜注入 //3.使用注解提供--> <!--======================復雜注入===============================--> <bean id="ser" class="service.Impl.ServiceImpl"> <property name="myStrs"> <array> <value>aaa</value> <value>bbb</value> </array> </property> <property name="myList"> <list> <value>list1</value> <value>2</value> </list> </property> <property name="myMap"> <map> <entry key="1" value="啊啊啊"></entry> <entry key="2"><value>啊啊啊</value></entry> </map> </property> <property name="myProps"> <props> <prop key="測試標簽體">測試標簽體</prop> </props> </property> </bean> <!--用於給Map結構注入的標簽 map標簽和props結構相同可以互換 用於給list結構集合注入的標簽 list array set 都可以使用 --> </beans>
3.注解依賴注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--基於注解--> <!-- 告知spring框架在,讀取配置文件,創建容器時,掃描注解,依據注解創建對象,並存入容器中 --> <context:component-scan base-package="com"/> </beans>