Java之美[從菜鳥到高手演變]之spring架構初識

來源:互聯網
上載者:User

初識輕量級Java開源架構 --- Spring

作者:egg

微博:http://weibo.com/xtfggef

出處:http://blog.csdn.net/zhangerqing

spring是一個輕量級Java架構,其核心思想就是DI(Dependency Injection,即依賴注入)和IoC(Inversion of Control,即控制反轉),因為其開源、低侵入性,現在已經席捲了很大一部分市場,其最大競爭者乃是JavaEE架構EJB。EJB3.0以前,由於其笨重以及使用繁瑣,被人們無奈棄用,取而代之就是spring,但是spring也有它的局限性,就是spring對分布式的支援不好,但是EJB在這方面很有優勢,而且當前EJB3.0簡化了操作,使用不再繁瑣,競爭力進一步提升。所以,當前輕量級的JavaWeb項目,很多都是使用的spring,我們有必要把它學好。本章系Java之美[從菜鳥到高手演變]系列之spring架構初識,我希望大家和我一起,再次回顧下Spring的基礎知識,積極提出寶貴的意見和建議。本次學習使用的是Spring4.1 版本。 Modules



從這幅圖中我們可以看出來Spring的整體架構,主要分為六大模組 Core Container 核心部分分為4大塊,spring-core, spring-beans, spring-context, spring-expression. 其中core和bean是整個架構的核心,提供了基礎的DI和IoC功能。 Context建立在core和beans模組之上,提供一種類似JNDI且以架構的方式來操作對象的方式。Context模組從beans模組繼承它的功能同時增加了國際化支援,如資源綁定等,同時,Context模組也支援JavaEE功能,如EJB,JMX和基本的遠程調用。ApplicationContext介面是context模組的焦點。expression是一種很強大的expression language,支援在運行時查詢和操作對象的屬性,我們會在後面的文章中舉些例子來說明spring expression language的用法。 AOP and instrumentation

Aop模組提供了面向切面編程的實現,和AspectJ整合。 Messaging

Messaging是spring4新增加的模組,包含了一部分主要的基於message的應用的實現。 Data Access/Integration

Data access顧名思義,是spring對資料層提供的支援,是功能比較豐富的模組。提供了包括JDBC,事物,ORM,JMS等一系列實現。 Web

Web模組主要提供面向web的一些實現,例如多檔案上傳,servlet監聽器以及spring mvc方面的支援。 Test

Test模組主要是針對spring的各個模組做各種各樣的測試,包括單元測試、整合測試等等。


關於各個模組具體的解釋及作用我們會在後面的文章中一一提到,也請有興趣的讀者自行去查閱spring官方文檔,獲得更詳細的說明。


第一個Spring小程式

建立一個User類,一個UserDao類以及一個測試類別:

User.java

package com.adam.java.spring.po;public class User {private String name;private int age;private int score;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;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}public String toString(){return "tostring from user";}}
UserDao.java
package com.adam.java.spring.dao;import com.adam.java.spring.po.User;public class UserDao {private User user;public void add(){System.out.println("add from userdao");System.out.println(user.toString());}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
DITest.java
package com.adam.java.spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.adam.java.spring.dao.UserDao;public class DITest {@SuppressWarnings("resource")public static void main(String[] args) {ApplicationContext atx = new ClassPathXmlApplicationContext("beans.xml");UserDao userDao = (UserDao) atx.getBean("userDao");userDao.add();}}
一個設定檔beans.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user" class="com.adam.java.spring.po.User"/><bean id="userDao" class="com.adam.java.spring.dao.UserDao"><property name="user" ref="user"/></bean></beans>

執行測試類別,得出如下輸出:

add from userdaotostring from user


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.