Spring的起源和根本使命
Spring如何簡化Java開發
- 基於POJO的輕量級和最小侵入性編程
- 通過依賴注入和面向接口實現松耦合
- 基於切面和慣例進行聲明式編程
- 通過切面和模板減少樣版式代碼
1. 基於POJO的輕量級和最小侵入性編程
/** * @Author:jimisun * @Description: * @Date:Created in 20:32 2018-09-26 * @Modified By: */ public class HelloSpringBean { public String sayHello() { return "Hello Spring!!!"; } }
你可以看到,這就是一個POJO(簡單的JAVA類),沒有任何地方表明它是Spring組件,Spring非侵入式編程模型意味着這個類在Spring應用和非Spring應用中都可以發揮同樣的作用。盡管看起來很簡單;但Spring通過IOC(Inversion of Control)管理這個POJO,然后通過DI(Dependency Inject)來注入他們,這個POJO就變的魔力十足;那么DI是如何幫助應用對象彼此之間保持松耦合的呢?
2. 通過依賴注入和面向接口實現松耦合
/** * @Author:jimisun * @Description: * @Date:Created in 07:44 2018-09-27 * @Modified By: */ public class BeautifulGirl implements Gril { private EatAction eat;
public BeautifulGirl() { this.eat = new EatAction(); } @Override public void action() { eat.action(); } }
在BeautifulGirl(可愛的小女孩)這個類中,在構造方法中創建一個EatAction(吃飯動作)。這樣就極大限制了BeautifulGirl(可愛的小女孩)的能力;如果現在小女孩需要去玩耍呢?或者需要去睡覺呢?真是太抱歉了,BeautifulGirl(可愛的小女孩)只會吃東西這個動作。這是什么原因呢?這就是BeautifulGirl(可愛的小女孩)和EatAction(吃飯動作)這兩個類緊緊耦合在了一起!緊密耦合同時會造成代碼的難以測試,難以服用,難以理解,並且典型地表現出"打地鼠“式的Bug特性(修復一個Bug,將會出現一個或多個新Bug),所以我們可以知道耦合是必須的,但必須謹慎管理耦合,但是怎么才算是謹慎處理耦合關系呢?
/** * @Author:jimisun * @Description: * @Date:Created in 07:44 2018-09-27 * @Modified By: */ public class BeautifulGirl implements Gril { private Action action; public BeautifulGirl(Action action) { this.action = action; } @Override public void action() { action.action(); } }
從上面實例代碼中可以看到BeautifulGirl本身並沒有創建任何的動作,而是通過構造方法傳入一個實現了Action(動作)接口的實現類即可,也就是說BeautifulGirl可以完成任意實現了Action接口的動作(睡覺啦...玩耍啦...旅行啦....)。這里的要點是BeautifulGirl沒有與任何特定的Action發生耦合。BeautifulGirl只需要的是一個實現Action接口就行,對象本身只是通過接口(而非具體實現或初始化過程)來表明依賴關系,那么這種依賴就能夠在BeautifulGirl不知情的情況下替換不同的具體動作。好了我們現在明白了DI進行依關系解耦的原理了,下面我們看一下如何在Spring中應用DI。 example4實例源碼下載
<?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-4.0.xsd"> <bean class="com.jimisun.spring.example4.BeautifulGirl" id="beautifulGirl"> <constructor-arg ref="action"/> </bean> <bean class="com.jimisun.spring.example4.SleepAction" id="action"></bean> </beans>
/** * @Author:jimisun * @Description: * @Date:Created in 07:53 2018-09-27 * @Modified By: */ public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); BeautifulGirl beautifulGirl = (BeautifulGirl) context.getBean("beautifulGirl"); beautifulGirl.action(); context.close(); } }
這樣執行Main方法,從Context中獲取BeautifulGirl實例執行action方法。當然Spring提供了基於Java的配置,可作為XML配置文件的代替方案 example5實例源碼下載
/** * @Author:jimisun * @Description: * @Date:Created in 08:40 2018-09-27 * @Modified By: */ @Configuration public class SpringConfig { @Bean public SleepAction sleepAction() { return new SleepAction(); } @Bean public BeautifulGirl beautifulGirl() { return new BeautifulGirl(sleepAction()); } }
/** * @Author:jimisun * @Description: * @Date:Created in 07:53 2018-09-27 * @Modified By: */ public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class); SleepAction action = applicationContext.getBean(SleepAction.class); action.action(); } }
3. 基於切面和慣例進行聲明式編程
/** * @Author:jimisun * @Description: * @Date:Created in 09:32 2018-09-27 * @Modified By: */ public class Parent { public void check() { System.out.println("檢查動作是否安全......."); } }
非常簡單!Parent(家長類)只有一個方法就是check,那么現在就讓Parent對BeautifulGirl的執行動作進行檢查吧。
<bean class="com.jimisun.spring.example6.BeautifulGirl" id="beautifulGirl"> <constructor-arg ref="action"/> <constructor-arg ref="parent"/> </bean> <bean class="com.jimisun.spring.example6.SleepAction" id="action"></bean> <bean class="com.jimisun.spring.example6.Parent" id="parent"></bean> </beans>
/** * @Author:jimisun * @Description: * @Date:Created in 07:44 2018-09-27 * @Modified By: */ public class BeautifulGirl implements Girl { private Action action; private Parent parent; public BeautifulGirl(Action action, Parent parent) { this.action = action; this.parent = parent; } @Override public void action() { parent.check(); action.action(); } }
- 管理Parent家長的check動作真的是美麗的小女孩的職責嗎?
- 將Parent家長注入到美麗的小女孩類中不是將代碼復雜化了嗎?
- 我們需不需要一個不需要家長注入的美麗的小女孩呢?
- 如果注入的Parent為NULL我們是否應該在美麗的小女孩中進行校驗呢?
<!--聲明Bean--> <bean class="com.jimisun.spring.example7.Parent" id="parent"></bean> <!--聲明切面--> <aop:config> <aop:aspect ref="parent"> <aop:pointcut id="girlAction" expression="execution(* com.jimisun.spring.example7.Action.*(..))"/> <aop:before pointcut-ref="girlAction" method="check"/> </aop:aspect> </aop:config>
/** * @Author:jimisun * @Description: * @Date:Created in 07:44 2018-09-27 * @Modified By: */ public class BeautifulGirl implements Girl { private Action action; public BeautifulGirl(Action action) { this.action = action; } @Override public void girlAction() { action.action(); } }
4. 通過切面和模板減少樣版式代碼
/** * @Author:jimisun * @Description: * @Date:Created in 11:13 2018-09-27 * @Modified By: */ public class Main { public static void main(String[] args) { JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.execute("select * from user"); } }
Java開發之上帝之眼系列教程其他文章
本文資料來源:
勘誤&感謝
本系列文章資料來源很多出自於互聯網和在下本身的見解,受限於個人技術能力水平和其他相關知識的限制,相關見解錯誤或者資料引用錯誤請各位幫助留言校正!引用資料多來自於互聯網,在下在引用前會遵循各位前輩或者博主的引用說明表示感謝,但互聯網資料多是轉發再轉發或存在遺漏請原作者內信聯系指正。