上一篇文章中,我們入門了第一個Spring程序,但是卻不太理解為什么?我們先不從源碼來講(小編也不太熟悉Spring源代碼)。在此僅做簡單介紹!
先看“糧草”:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<bean id="student" class="com.wen.vo.Student">
<property name="name" value="wen"></property>
<property name="age" value="20"></property>
</bean>
</beans>
以上的配置文件是入門程序的配置文件,如果不懂的同學請看上一篇文章!
寫配置文件基本少不了它了。
bean里有個id,指的是唯一標識,且必須符合xml規則。不懂就理解為就像我們每個人的身份證號碼一樣,不可以重復的。當然你也可以直接用name來指定,無所謂的。但是name這個屬性一般不這樣用。
class是指哪個類,就是將寫的實體類或其他類,將他們的全限定名放到這個里就可以了(包名+類名)
屬性的意思。就是在這里可以為實體類的屬性賦值之類的操作。
里面有name屬性對應的Student類中的變量名字,value屬性是在這里為變量賦值!
當然還有個ref屬性是干嘛的?這個就是引用另一個對象 。
總的來說,可以理解spring容器是個杯子架。每個bean就是一個杯子,id就是杯子對應使用的人,class就是杯子放的位置。找到這個杯子了。想喝什么?property就是杯子里要放什么?喝茶就是放茶葉等等。任君選擇。當然放的前提是要有這個東西,沒有就會報錯!
在創建一個實體類Curriculum課程信息類
package com.wen.test;public class Curriculum { private String curriculumName; private int curriculumId; public String getCurriculumName() { return curriculumName; } public void setCurriculumName(String curriculumName) { this.curriculumName = curriculumName; } public int getCurriculumId() { return curriculumId; } public void setCurriculumId(int curriculumId) { this.curriculumId = curriculumId; } @Override public String toString() { return "Curriculum [curriculumName=" + curriculumName + ", curriculumId=" + curriculumId + "]"; }}
在Student實體類中添加依賴的課程信息類
package com.wen.vo;import com.wen.test.Curriculum;/** * 實體類,實體有名字和年齡 * @author wen * */public class Student { private String name; private int age; private Curriculum curriculum; public Curriculum getCurriculum() { return curriculum; } public void setCurriculum(Curriculum curriculum) { this.curriculum = curriculum; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
對xml添加依賴引用curriculum類
<bean id="curriculum" class="com.wen.test.Curriculum"> <property name="curriculumName" value="Spring入門"></property> <property name="curriculumId" value="1001" ></property> </bean> <bean id="student" class="com.wen.vo.Student"> <property name="name" value="wen"></property> <property name="age" value="20" ></property> <property name="curriculum" ref="curriculum"></property> </bean>
測試代碼如下:
package com.wen.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wen.vo.Student;public class SpringTest { public static void main(String[] args) { ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext-test.xml"); Student student=(Student) applicationContext.getBean("student"); System.out.println("年齡"+student.getAge()+" "+"成績"+student.getName()); System.out.println("課程信息"+student.getCurriculum()); }}
測試結果:
總結:bean屬性里還有很多東西,在這就不一一闡述,在這2篇文章中,大家是不是感覺到少了點什么?就是new關鍵字見不到 了。以前我們都是要使用只好new一個對象,但是用spring之后,會發現程序中很少見到new這個關鍵字。因為spring容器已經幫你搞定了。你只需要要什么去拿了就可以了。極大改變了我們的編碼風格。還避免了程序的耦合度高的問題。
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。