spring是一個框架,這個框架可以干很多很多的事情。感覺特別吊。但是,對於初學者來說,很難理解spring到底是干什么的。我剛開始的時候也不懂,后來就跟着敲,在后來雖然懂了,但是依然說不明白它到底是干啥的。看了好多的老師的視頻,發現也都不適合小白。於是就想寫一篇適合小白看的spring入門,也許可以幫助一部分心學習spring的同學吧。
————扯淡完成
spring入門案例
spring到底是個什么東西,這個是我們先放一放,首先,spring是一個可以把我們的對象自動實例化的一個框架,我們今天先演示下這個。我們知道,在我們程序執行的過程中,所有的代碼最后運行完都會在內存中有體現的。比如說,我寫了如下代碼
public class User{
private String userName;
public void setUserName(String userName){
this.userName = userName;
}
public String getUserName(){
return this.userName;
}
}
User user = new User();
當new這句代碼執行的時候,我們的user類就加載到內存中去了。而在我們的spring框架中,你只需要引入spring的核心包(百度一下,網上就可以下載),然后在主目錄下創建一個applicationContext.xml文件。內容如下:
<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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
</beans>
然后我們就在里面將我們的User類配置進去。這里的bean的Id就是我們的對象名稱。class就是我們的類的全路徑。UserName對應的是User類中的屬性名稱,張三是我們要給它賦的值。代碼如下:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/mvc 8 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 15 <bean id ="user" class="com.spring.User"> 16 <property name="userName" value="張三" /> 17 </bean> 18 19 20 </beans>
我們可以這么理解:在spring框架加載后,會自動在我們的spring框架加載的時候,它就會將我們配置的類自動創建出一個對象,然后將對象放入內存。然后調用對象的setUserName的方法,將張三的值,賦值給這個對象。
那么它是怎么實現的呢?其實就是通過我們的反射機制去實現的。通過類的全路徑讀取到User的class文件,接下來就啥都可以了。
配置完以后,我們就需要使用spring來完成我們的對象自動實例化,然后得到這個對象。怎么做呢?
首先我們要得到一個對象,可以為我們提供我們配置的所有對象。這個對象就是spring的容器對象。在它的構造方法中,可以將我們剛才配置的文件以字符串的形式傳入進去,然后得到一個新的對象。通過新得到的這個對象,將我們的User對象獲取。代碼如下:
1 public static void main(String[] args){ 2 //這里是為了獲取application對象,這個對象可以產生我們自己配置的對象。 3 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml"); 4 //得到我們自己的對象 5 User user = (User)applicationContext.getBean("user"); 6 //得到用戶名 7 String userName =user.getUserName(); 8 //打印 9 System.out.println(userName); 10 11 }
然后運行代碼,得到結果為:張三。說明我們的入門程序成功。
