1、Spring框架簡介
Spring是一個開源框架,Spring是在2003年興起的一個輕量級的開源框架,由Rod johnson創(chuàng)建。主要對JavaBean的生命周期進(jìn)行管理的輕量級框架,Spring給JavaEE帶來了春天。
2、Spring框架特點(diǎn)
√ 輕量級:不是說他的文件大小很小,指的Spring是非侵入性。
知識點(diǎn):輕量級框架和重量級框架的區(qū)別
輕量級和重量級的框架是以啟動程序所需要的資源所決定,比如EJB在啟動程序的時候需要消耗大量的資源,內(nèi)存和CPU,所以是重量級。
√ 依賴注入:IOC(DI)和面向切面編程:AOP
√ 容器:Spring是一個容器,因為它包含并且管理應(yīng)用對象的生命周期
√ 框架:Spring實(shí)現(xiàn)了使用簡單的組件配置組合成一個復(fù)雜的應(yīng)用。
√ 一站式:在IOC和AOP的基礎(chǔ)上可以整合各種企業(yè)應(yīng)用的開源框架和優(yōu)秀的第三方類庫
3、Spring架構(gòu)
Test:Spring支持Junit單元測試
Core Container(核心容器):里面能看到有4個核心的內(nèi)容,也就是如果需要使用Spring,這4個jar包是絕對不能少的。
?、?spring-beans-4.3.6.RELEASE.jar (Beans):Bean工廠,創(chuàng)建對象
?、?spring-core-4.3.6.RELEASE.jar (Core):核心的基礎(chǔ)
?、?spring-context-4.3.6.RELEASE.jar(Context):上下文ApplicationContext
④ spring-expression-4.3.6.RELEASE.jar(SpEL:Spring的EL表達(dá)式)
知識點(diǎn):還有一個jar包也是必須的,因為Spring使用了它
⑤ commons-logging-1.2.jar
AOP:面向切面編程
Transactions:聲明式事務(wù)
ORM、JDBC:可以整合hibernate和mybatis
Web、Servlet:可以整合Stucus2和Spring MVC
4、創(chuàng)建一個HelloWorld項目(博客園最純凈、最簡單版本)
下載spring的jar包:http://repo.spring.io/release/org/springframework/spring
為了更好的理解Spring的jar包依賴,我們不用maven創(chuàng)建。我們也不用web.xml文件來初始化加載spring容器,完全原始打造。主要目的就是為了理解一下最純凈最簡單的spring項目是什么樣子的。
①、首先創(chuàng)建引入jar包,上面5個jar包進(jìn)入即可
看到了嗎?5個jar就可以創(chuàng)建了。是創(chuàng)建helloworld的最小基本單元了。再少就不能運(yùn)行了。
②、創(chuàng)建一個HelloWorld.class
1 public class HelloWorld { 2 private String name; 3 public void setName(String name) { 4 System.out.println("調(diào)用了設(shè)置屬性"); 5 this.name = name; 6 } 7 public HelloWorld(){ 8 System.out.println("初始化構(gòu)造器"); 9 }10 public void hello(){11 System.out.println("Hello: " + name);12 }13 }
③ 創(chuàng)建一個Spring的配置文件applicationContext.xml
1 <bean id="helloworld" class="HelloWorld">2 <property name="name" value="Spring"></property>3 </bean>
④ 創(chuàng)建一個Main.class(主函數(shù)入口)
1 public static void main(String[] args) {2 //1、創(chuàng)建Spring的IOC容器的對象3 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");4 //2、從IOC的容器中獲取Bean的實(shí)例5 HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld");6 //3、調(diào)用hello方法7 helloWorld.hello();8 }
輸出結(jié)果:
初始化構(gòu)造器
調(diào)用了設(shè)置屬性
Hello: Spring
思考一下,如果上面main里的方法我把獲取Bean的實(shí)例和調(diào)用hello方法的都注釋掉(即把2和3的全注釋掉),那么結(jié)果應(yīng)該是什么?
輸出結(jié)果:
初始化構(gòu)造器
調(diào)用了設(shè)置屬性
說明了在創(chuàng)建SpringIOC容器的時候,就已經(jīng)對類進(jìn)行了實(shí)例化并對屬性進(jìn)行了賦值
---------------------------------------------------------------------------------
跟著剛哥學(xué)習(xí)Spring框架系列:
跟著剛哥學(xué)習(xí)Spring框架--創(chuàng)建HelloWorld項目(一)
跟著剛哥學(xué)習(xí)Spring框架--Spring容器(二)
跟著剛哥學(xué)習(xí)Spring框架--通過XML方式配置Bean(三)
跟著剛哥學(xué)習(xí)Spring框架--通過注解方式配置Bean(四)
跟著剛哥學(xué)習(xí)Spring框架--AOP(五)
跟著剛哥學(xué)習(xí)Spring框架--JDBC(六)
跟著剛哥學(xué)習(xí)Spring框架--事務(wù)配置(七)
http://www.cnblogs.com/hzg110/p/6760653.html