1.通過set方法設值注入
設置值的時候用set,獲取值的時候用get
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("========");
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
System.out.println("--------");
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
<bean id="myStudent" class="com.di01.Student">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
</bean>
@Test
public void test01() {
// 加載Spring配置文件,創建Spring容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/di01/applicationContext.xml");
// 從容器中獲取指定的bean對象
Student student = (Student) ac.getBean("myStudent");
System.out.println(student);
}
導入頭文件,可以走windows-preference-xmlfiles-editor-templates-add添加,記得復制原碼
<bean id="myStudent" class="com.di01.Student">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
<property name="school" ref="mySchool"></property>
</bean>
<bean id="mySchool" class="com.di01.School">
<property name="sname" value="林遠鏢局"></property>
</bean>
2.構造注入(不需要調無參構造器,但是最好還是帶上)
public class Student {
private String name;
private int age;
private School school;// 域屬性
public Student() {
System.out.println("---------"); // 無參構造器
}
public Student(String name, int age, School school) {
super(); // 有參構造器
this.name = name;
this.age = age;
this.school = school;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
}
}
<bean id="myStudent" class="com.di02.Student"> // 構造注入的方法,使用index,name,或者直接value只要保證順序和構造器的一致即可,選擇合適的就行
<constructor-arg index="0" value="linda"></constructor-arg>
<constructor-arg index="1" value="20"></constructor-arg>
<constructor-arg index="2" ref="mySchool"></constructor-arg>
</bean>
<bean id="mySchool" class="com.di02.School">
<property name="sname" value="林遠鏢局"></property>
</bean>
3.集合屬性注入
private School[] schools; //每個元素都是對象引用
private List<String> myList;
private Set<String> mySet;
private Map<String, Object> myMap;
private Properties myPros;// 健和值都是字符串
<bean id="school1" class="com.di03.School">
<property name="sname" value="南京大學"></property>
</bean>
<bean id="school2" class="com.di03.School">
<property name="sname" value="東南大學"></property>
</bean>
<bean id="some" class="com.di03.Some">
<property name="schools">
<array>
<ref bean="school1" />
<ref bean="school2" />
</array>
</property>
<property name="myList">
<list>
<value>lucy</value>
<value>lily</value>
</list>
</property>
<property name="mySet">
<set>
<value>天涯</value>
<value>海角</value>
</set>
</property>
<property name="myMap"> //map是鍵值對輸入
<map>
<entry key="av1" value="武藤蘭"></entry>
<entry key="av2" value="小澤瑪莉亞"></entry>
</map>
</property>
<property name="myPros">
<props>
<prop key="聯系方式">深圳</prop>
<prop key="具體說明">東莞一條龍</prop>
</props>
</property>
</bean>
<property name="arrs" value="abc,def"></property>
<property name="myList" value="張三,李四" />
<property name="mySet" value="天涯,海角" /> //對于字符串可以直接寫,對于鍵值對和數組無法使用。
4.對于域屬性的自動注入
<bean id="myStudent" class="com.di05.Student" autowire="byName">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
</bean>
<bean id="school" class="com.di05.School">
<property name="sname" value="林遠鏢局"></property>
</bean>
<bean id="myStudent" class="com.di06.Student" autowire="byType">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
</bean>
5.使用SPEL注入
Spring EL表達式語言,SPEL以#開頭,后跟一對大括號,用法: <bean id="" value="#{}"/>
public int computeAge() {
return page > 25 ? 25 : page;
}
<bean id="myPerson" class="com.di07.Person">
<property name="pname" value="lucy"></property>
<property name="page" value="#{T(java.lang.Math).random()*50}"></property>
</bean>
<bean id="myStudent" class="com.di07.Student">
<property name="name" value="#{myPerson.pname}"></property>
<property name="age" value="#{myPerson.computeAge()}"></property>
</bean>
要有get方法,可以在類中定義方法,在Bean中進行調用
6.內部匿名Bean
<bean id="myStudent" class="com.di06.Student" autowire="byType">
<property name="name" value="林平之"></property>
<property name="age" value="21"></property>
<property name="school">
<bean class="com.di06.School">
<property name="sname" value="daxue"/>
</bean>
</property>
</bean>
同類抽象Bean
遇到相同的內容,會想到繼承關系,可以使用parent屬性,格式如 <bean id="" parent="">異常抽象Bean
不是同類使用abstract,格式 <bean id="base" abstract="true">
9.為應用指定多個Spring配置文件(平等關系)
目錄下有好幾個bean文件,格式相同,比如spring-beans,spring-base,
可以在test寫成di/spring-*.xml
格式不相同,把每個列出來,資源需要。
applicationContext ac=new classpathxmlapplicationContext(r1,r2);
也可放入數組中進行配置
String[] resources={r1,r2};
(包含關系)