重新来认识你的老朋友Spring框架
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开发之上帝之眼系列教程其他文章
本文资料来源:
勘误&感谢
本系列文章资料来源很多出自于互联网和在下本身的见解,受限于个人技术能力水平和其他相关知识的限制,相关见解错误或者资料引用错误请各位帮助留言校正!引用资料多来自于互联网,在下在引用前会遵循各位前辈或者博主的引用说明表示感谢,但互联网资料多是转发再转发或存在遗漏请原作者内信联系指正。
重新来认识你的老朋友Spring框架的更多相关文章
- Spring框架概述
Spring是最流行的Java企业级应用开发框架,全球数以百万的开发者在使用Spring框架创建高性能.易测试.可重用的代码. Spring框架的核心特性可以应用于任何Java应用,但扩展的JavaE ...
- 初识Spring框架实现IOC和DI(依赖注入)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的, IoC是 ...
- Spring 框架的架包分析、功能作用、优点,及jar架包简介
Spring 框架的架包详解 Spring的作用 Spring的优势 由于刚搭建完一个MVC框架,决定分享一下我搭建过程中学习到的一些东西.我觉得不管你是个初级程序员还是高级程序员抑或 ...
- 最新 Eclipse IDE下的Spring框架配置及简单实例
前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...
- spring框架学习(三)
一.Spring自动组件扫描 Spring 提供组件扫描(component scanning)功能.它能从指定的classpath里自动扫描.侦测和实例化具有特定注解的组件. 基本的注解是@Comp ...
- Spring框架学习(一)
一. spring概述 Spring 框架是一个分层架构,由 7 个定义良好的模块组成.Spring 模块构建在核心容器之上,核心容器定义了创建.配置和管理 bean 的方式,如图 1 所示. 图 1 ...
- Spring 系列: Spring 框架简介 -7个部分
Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...
- 使用 Spring Boot 快速构建 Spring 框架应用--转
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...
- 【Spring】浅析Spring框架的搭建
c目录结构: // contents structure [-] Spring是什么 搭建Spring框架 简单Demo 1,建立User类 2,建立Test类 3,建立ApplicationCont ...
随机推荐
- js匹配浏览器类型,收藏下
<script type="text/javascript">/** 智能机浏览器版本信息:**/ varbrowser={ versions:function ...
- 解决App can’t be opened because it is from an unidentified developer
关闭设置 打开终端 输入sudo spctl --master-disable
- imx6 读取CPU温度
imx6 读取CPU温度 cat /sys/class/thermal/thermal_zone0/temp Tony Liu 2017-2-11, Shenzhen
- Runtime是什么?
在看 RPC 的概念模型与实现解析 的时候,看到图片上有Runtime,又想到见过很多Runtime之类的东西,所以就想弄明白这到底是个什么东西. (因为是程序名,所以根本没想到代码的“编译-运行”~ ...
- Intellij IDEA 使用学习
Intellij中名词解释: Project,就是一个完整的项目,类似Eclipse中的WorkSet(虽然WorkSet是人为归类的). Module,是Project中的模块,类似Eclipse中 ...
- Python 出现错误 SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform.
报出SNIMissingWarning和InsecurePlatformWarning警告. 解决方法: 在cmd中输入: pip install pyopenssl ndg-httpsclient ...
- Python3.4下使用sqlalchemy
一. 1.用sudo apt-get install python3-numpy之后,会默认把numpy安装到 /usr/lib/python3/dist-packages目录下,而且版本比较低. ...
- 转载:【原译】Erlang性能的八个误区(Efficiency Guide)
转自:http://www.cnblogs.com/futuredo/archive/2012/10/16/2725770.html The Eight Myths of Erlang Perform ...
- html dom基本操作
//div出滚动条: <div id="discussion" style="height:500px;overflow:auto;"></d ...
- 感谢各位亲们的大力支持,免费的HTML5学习课程《HTML5网页开发实例具体解释》连载已经结束了!
感谢各位亲们的大力支持,免费的HTML5学习课程<HTML5网页开发实例具体解释>连载已经结束了. 有兴趣的读者能够看我的博客,也能够看以下的链接逐个学习: 当里个当.免费的HTML5连 ...