ref 的 parent 與 local 屬性java
<!-- 經過屬性注入 --> <bean id="user" class="com.laolang.sstudy.ioc.domain.User"> <property name="id" value="1001" /> <property name="age" value="23" /> <property name="name" value="laolang" /> </bean>
根據JavaBean關於屬性命名的特殊規範,變量的前兩個字母要麼所有大寫,要麼所有小寫spring
要提供相應的setter方法dom
public User(Long id, String name, Integer age) { this.id = id; this.name = name; this.age = age; }
<!-- 按類型匹配入參 --> <bean id="user2" class="com.laolang.sstudy.ioc.domain.User"> <constructor-arg type="java.lang.Long"> <value>1001</value> </constructor-arg> <constructor-arg type="java.lang.String"> <value>xiaodaima</value> </constructor-arg> <constructor-arg type="java.lang.Integer"> <value>23</value> </constructor-arg> </bean>
<!-- 按索引匹配入參 --> <bean id="user3" class="com.laolang.sstudy.ioc.domain.User"> <constructor-arg index="0" value="1001" /> <constructor-arg index="1" value="tianya" /> <constructor-arg index="2" value="23" /> </bean>
若是有兩個構造函數的入參數目相同,則能夠同時使用類型和索引匹配函數
public Student(Long id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Student(Long id, String name, Long number) { this.id = id; this.name = name; this.number = number; }
<!-- 聯合使用類型和索引匹配入參 --> <bean id="student" class="com.laolang.sstudy.ioc.domain.Student"> <constructor-arg index="0" type="java.lang.Long"> <value>1001</value> </constructor-arg> <constructor-arg index="1" type="java.lang.String"> <value>student</value> </constructor-arg> <constructor-arg index="2" type="java.lang.Integer"> <value>23</value> </constructor-arg> </bean> <bean id="student2" class="com.laolang.sstudy.ioc.domain.Student"> <constructor-arg index="0" type="java.lang.Long"> <value>1001</value> </constructor-arg> <constructor-arg index="1" type="java.lang.String"> <value>student</value> </constructor-arg> <constructor-arg index="2" type="java.lang.Long"> <value>14061001</value> </constructor-arg> </bean>
工廠this
package com.laolang.sstudy.ioc.factory; import com.laolang.sstudy.ioc.domain.Student; public class StudentFactory { public Student createStudent(){ Student student = new Student(); student.setId(1001L); student.setAge(23); student.setName("student by factory without param"); student.setNumber(14061001L); return student; } public Student createStudent( Long id , String name){ Student student = new Student(); student.setId(id); student.setName(name); return student; } }
xmlspa
<!-- 非靜態工廠方法 --> <!-- 工廠類 bean --> <bean id="studentFactory" class="com.laolang.sstudy.ioc.factory.StudentFactory" /> <!-- factory-bean : 指定工廠類bean factory-method : 指定建立 bean 的方法 --> <bean id="student3" factory-bean="studentFactory" factory-method="createStudent" /> <!-- 帶參數的工廠方法 --> <bean id="student4" factory-bean="studentFactory" factory-method="createStudent"> <constructor-arg index="0" value="1001" /> <constructor-arg index="1" value="student by factory with param" /> </bean>
工廠.net
package com.laolang.sstudy.ioc.factory; import com.laolang.sstudy.ioc.domain.Student; public class StudentStaticFactory { public static Student createStudent(){ Student student = new Student(); student.setId(1001L); student.setAge(23); student.setName("student by static factory without param"); student.setNumber(14061001L); return student; } public static Student createStudent( Long id , String name){ Student student = new Student(); student.setId(id); student.setName(name); return student; } }
xmlcode
<!-- 靜態方法工廠 無參--> <bean id="student5" class="com.laolang.sstudy.ioc.factory.StudentStaticFactory" factory-method="createStudent" /> <!-- 靜態方法工廠 帶參--> <bean id="student6" class="com.laolang.sstudy.ioc.factory.StudentStaticFactory" factory-method="createStudent" > <constructor-arg index="0" value="1001" /> <constructor-arg index="1" value="student by static factory with param" /> </bean>
<!-- 注入 date 類型 --> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean id="user4" class="com.laolang.sstudy.ioc.domain.User"> <property name="birthday"> <bean factory-bean="dateFormat" factory-method="parse"> <constructor-arg value="1991-12-06" /> </bean> </property> </bean>
參考:Sping注入Date類型的3種方式 orm
<!-- 注入 null 值 --> <bean id="user5" class="com.laolang.sstudy.ioc.domain.User"> <property name="id" value="1001" /> <property name="age" value="23" /> <property name="birthday"><null /></property> <property name="name"><null /></property> </bean>
ref 的 parent與local屬性xml
<!-- 引用其餘 bean --> <bean id="driver" class="com.laolang.sstudy.ioc.domain.Driver"> <property name="name" value="driver one" /> </bean> <bean id="car" class="com.laolang.sstudy.ioc.domain.Car"> <property name="brand" value="紅旗CA72" /> <property name="maxSpeed" value="200" /> <property name="price" value="123.456" /> <property name="driver" ref="driver" /> </bean>
<!-- 內部 bean --> <bean id="car2" class="com.laolang.sstudy.ioc.domain.Car"> <property name="brand" value="桑塔納2000" /> <property name="maxSpeed" value="200" /> <property name="price" value="456.123" /> <property name="driver"> <bean class="com.laolang.sstudy.ioc.domain.Driver"> <property name="name" value="driver two" /> </bean> </property> </bean>
外部配置文件
<!-- 導入配置文件 --> <!-- 導入單個配置文件 --> <!--<context:property-placeholder location="classpath:user.properties" />--> <!-- 導入多個配置文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 容許JVM參數覆蓋 --> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <!-- 忽略沒有找到的資源文件 --> <property name="ignoreResourceNotFound" value="true" /> <!-- 配置資源文件 --> <property name="locations"> <list> <value>classpath:user.properties</value> </list> </property> </bean>