Spring是一個開源免費的框架,爲了解決企業應用開發的複雜性而建立。Spring框架是一個輕量級的解決方案,能夠一站式地構建企業級應用。Spring是模塊化的,因此能夠只使用其中須要的部分。能夠在任何web框架上使用控制反轉(IoC),也能夠只使用Hibernate集成代碼或JDBC抽象層。它支持聲明式事務管理、經過RMI或web服務實現遠程訪問,並可使用多種方式持久化數據。它提供了功能全面的MVC框架,能夠透明地集成AOP到軟件中。java
Spring被設計爲非侵入式的,這意味着你的域邏輯代碼一般不會依賴於框架自己。在集成層(好比數據訪問層),會存在一些依賴同時依賴於數據訪問技術和Spring,可是這些依賴能夠很容易地從代碼庫中分離出來。web
Spring框架是基於Java平臺的,它爲開發Java應用提供了全方位的基礎設施支持,而且它很好地處理了這些基礎設施,因此你只須要關注你的應用自己便可。spring
Spring可使用POJO(普通的Java對象,plain old java objects)建立應用,而且能夠將企業服務非侵入式地應用到POJO。這項功能適用於Java SE編程模型以及所有或部分的Java EE。數據庫
一句話總結:Spring是一個輕量級的控制反轉(IoC)和麪向切面(AOP)的容器(框架)。編程
1.方便解耦,簡化開發設計模式
2.AOP編程的支持安全
3.聲明式事務的支持服務器
4.方便程序的測試架構
5.方便集成各類優秀框架app
6.下降Java EE API的使用難度
7.Java 源碼是經典學習範例
Spring 框架是一個分層架構,由 7 個定義良好的模塊組成。Spring 模塊構建在覈心容器之上,核心容器定義了建立、配置和管理 bean 的方式
組成 Spring 框架的每一個模塊(或組件)均可以單獨存在,或者與其餘一個或多個模塊聯合實現。每一個模塊的功能以下:
BeanFactory
,它是工廠模式的實現。BeanFactory
使用控制反轉(IOC) 模式將應用程序的配置和依賴性規範與實際的應用程序代碼分開。Spring 框架的功能能夠用在任何 J2EE 服務器中,大多數功能也適用於不受管理的環境。Spring 的核心要點是:支持不綁定到特定 J2EE 服務的可重用業務和數據訪問對象。毫無疑問,這樣的對象能夠在不一樣 J2EE 環境 (Web 或 EJB)、獨立應用程序、測試環境之間重用。
Spring是一個開源的框架,如今的Spring框架構成了一個體系平臺,經過Spring的官方網站http://www.springsource.org能夠了解到,圍繞着Spring框架自己,還有許多其餘優秀的項目:
SpringFramework(Core):核心項目
Spring Web Flow:工做流項目
Spring Security:安全項目
Spring Batch:批量數據處理項目
Spring Android:Android系統支持項目
Spring Social:社交項目
最核心的 jar 包:
最核心的接口是 BeanFactory
,它用來描述 IOC 容器
它很乾淨,很純粹,最主要的方法是 getBean
用來給調用方返回一個實例化好的對象。
在實際運用中,須要一些周邊功能,好比加載資源/國際化/等等,Spring 爲此提供了 ApplicatinContext
接口。它自己是 BeanFactory 的一個實現:
能夠看到,ApplicationContext 除了實現了 BeanFactory,還實現了其餘一些實用的接口。所以,它是在 Spring 中操做一切的核心。
這是門面模式的一種典型使用。
這種方式,充分利用了 xml 文件的優點:
因此,最開始的時候,描述工廠裏 bean 聲明的方式,選用的就是 xml
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="bs" class="com.learning.spring.BookServiceImpl"></bean> </beans>
可是:
因此,就產生了不少其餘的叛逆的想法
package com.learning.spring.configuration; import com.learning.spring.BookDAO; import com.learning.spring.BookService; import com.learning.spring.BookServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SuibianSpringConfig { @Bean public BookService bs(){ System.out.println(bookDAO()); if(Math.random() > 0.5){ return new BookServiceImpl(); }else{ return new BookServiceImpl2(); } } @Bean public BookDAO bookDAO(){ return new BookDAO(); } }
Java Style 中混入 XML Style:
package com.learning.spring.configuration; import com.learning.spring.BookDAO; import com.learning.spring.BookService; import com.learning.spring.BookServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource(locations = "learning/spring/my-spring.xml") public class SuibianSpringConfig2 { @Bean public BookService bs1() { System.out.println(bookDAO()); if(Math.random() > 0.5){ return new BookServiceImpl(); }else{ return new BookServiceImpl2(); } } @Bean public BookDAO bookDAO () { return new BookDAO(); } }
XML style 中混入 Java Style:
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="bs" class="com.learning.spring.BookServiceImpl"></bean> <bean class="com.learning.spring.configuration.SuibianSpringConfig2" /> </beans>
建立應用對象之間協做關係的行爲,一般稱爲裝配 (Wiring)。這是依賴注入 (DI) 的本質。
裝配的基礎,是使用配置文件對 Bean 的關係進行聲明。
總結起來,在 Spring 中,聲明 Bean 一共有三種方式:
<bean />
節點@Bean
註解@Component/@Controller/@Service/@Repository
裝配:
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--有參構建--> <bean class="com.learning.components.Goal" id="goal"> <property name="name" value="多利"/> <property name="countOfLegs" value="4"/> <property name="aliases"> <ref bean="xx1"/> </property> </bean> <!--集合--> <util:list id="xx1"> <value>value1</value> <value>value2</value> <value>value3</value> </util:list> <!--設置別名--> <alias name="bookDAO" alias="bookDao"/> <alias name="bookDAO" alias="bookdao"/> <!--引入外部xml文件--> <import resource="dierge.xml"/> </beans>
另外:
Goal類代碼以下:
package com.learning.components; import java.util.List; public class Goal { private String name; private int countOfLegs; private List<String> aliases; public List<String> getAliases() { return aliases; } @Override public String toString() { return "Goal{" + "name='" + name + '\'' + ", countOfLegs=" + countOfLegs + ", aliases=" + aliases + '}'; } public void setAliases(List<String> aliases) { this.aliases = aliases; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCountOfLegs() { return countOfLegs; } public void setCountOfLegs(int countOfLegs) { this.countOfLegs = countOfLegs; } }
XML文件代碼以下:
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="bs" class="com.learning.spring.BookServiceImpl"></bean> <bean class="com.learning.spring.configuration.SuibianSpringConfig2" /> <!--有參構建--> <bean class="com.learning.components.Goal" id="goal"> <property name="name" value="多利"/> <property name="countOfLegs" value="4"/> <property name="aliases"> <ref bean="xx1"/> </property> </bean> <!--集合--> <util:list id="xx1"> <value>value1</value> <value>value2</value> <value>value3</value> </util:list> <!--設置別名--> <alias name="bookDAO" alias="bookDao"/> <alias name="bookDAO" alias="bookdao"/> <!--引入外部xml文件--> <import resource="dierge.xml"/> </beans>
Main類代碼以下:
package com.learning.components; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("learning/spring/my-spring.xml"); Goal goal = (Goal) applicationContext.getBean("goal"); System.out.println(goal); } }
運行結果: