①先來介紹一下spring的配置文件吧。
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean id="一個類的標識,相當於這個類的一個對象" class="該類的包名.類名">
<property name="對象所具有的屬性名與類中的屬性名一致" value="屬性的值">
<!--這是最普通的配置-->
</property>
</bean>
</beans>
一、set注入
建立一個computer類,再建立一個測試類。然後修改配置文件。這裏只有一個類,沒有產生相關性。
package entity;
public class Computer {
private String cpu;
private String hdd;//硬盤
private String mainborder;//主板
@Override
public String toString() {
return "Computer [cpu=" + cpu + ", hdd=" + hdd + ", mainborder=" + mainborder + "]";
}
public Computer() {
System.out.println("我是Computer的構造器");
}
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public String getHdd() {
return hdd;
}
public void setHdd(String hdd) {
this.hdd = hdd;
}
public String getMainborder() {
return mainborder;
}
public void setMainborder(String mainborder) {
this.mainborder = mainborder;
}
}
package controller;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Computer;
public class TestSpringIOC {
@Test
public void test01() {
//加載配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//1.new ClassPathXmlApplicationContext()時,創建這個容器
//2.然後加載配置文件,生成對象
//3.最後通過getBean(id)獲取對象。
//System.out.println(ac);
Object obj= ac.getBean("com");//com是applicationContext.xml中的computer類的id
System.out.println(obj);
}
}
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean id="com" class="entity.Computer">
<!--給對象的變量賦值的方式是set注入 -->
<!-- 這裏的name就是對象的參數屬性,value爲參數屬性的值。這種方法要求類中必須有set該參數的方法!!! -->
<property name="cpu" value="英特爾"></property>
<!-- 這裏的name和set方法的方法名相關,相關性爲name="set方法的首字母小寫的方法名", 例如Computer類中方法名爲setHdd,這裏的name就等於"hdd",沒有set只有Hdd的小寫hdd -->
</bean>
</beans>
上面的例子只有一個computer類,沒有產生相關性,現在在增加一個student類,讓這兩個類產生相關性。
computer的代碼還是和上面一樣就不在贅述了。
package entity;
public class Student {
private String name;
private Computer com;
@Override
public String toString() {
return "Student [name=" + name + ", com=" + com + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setCom(Computer com) {
this.com = com;
}
public Student() {
System.out.println("我是Student的構造器");
}
}
package controller;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Computer;
public class TestSpringIOC {
@Test
public void test01() {
//加載配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//1.new ClassPathXmlApplicationContext()時,創建這個容器
//2.然後加載配置文件,生成對象
//3.最後通過getBean(id)獲取對象。
//System.out.println(ac);
Object obj= ac.getBean("stu");//stu是applicationContext.xml中的student類的id
System.out.println(obj);
}
}
可以看出student類中有computer類的對象,也就是computer的對象在student類中做studen對象的一個屬性。這樣兩個類產生了相關性,因此配置文件需要做一個修改。注意<bean><bean/>中間的代碼,每個標籤的含義我都註釋在了代碼上。很詳細,包括標籤的屬性值。
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- class="包名.類名" id爲class的一個對象!! -->
<bean id="stu" class="entity.Student">
<property name="name" value="#{com.cpu}"></property>
<!-- #{com.cpu}是springEL表達式,#{id.屬性名} -->
<property name="com" ref="com"></property>
<!-- computer類的對象是student類的屬性,所以name=student類中"set方法的首字母小寫的方法名", ref="值",該值爲computer類在xml中對應的bean的id值。 -->
</bean>
<bean id="com" class="entity.Computer">
<!--給對象的變量賦值的方式是set注入 -->
<!-- 這裏的name就是對象的參數屬性,value爲參數屬性的值。這種方法要求類中必須有set該參數的方法!!! -->
<property name="cpu" value="英特爾"></property>
<!-- 這裏的name和set方法的方法名相關,相關性爲name="set方法的首字母小寫的方法名", 例如Computer類中方法名爲setHdd,這裏的name就等於"hdd",沒有set只有Hdd的小寫hdd -->
</bean>
<beans/>
一定要明白id,class,name,value,ref這幾個標籤的含義,只有弄得含義才能在代碼中靈活使用。
二、構造器注入
這個不是很常用,暫時先不補充了,等我用到了我在仔細補充