《Spring in action》之Spring之旅
Spring框架作用是简化java开发的复杂性。下面是spring in action 对spring初步介绍。
一、主要有4种关键策略:
1. 基于POJO的轻量级和最小侵入性编程 。
2. 通过依赖注入和面向接口实现松耦合。
3. 基于切面和惯例进行声明式编程 。
4.通过切面和模板减少样板式代码。
public class People {
private Play play;
/**
* play 接口注入进构造器
* @param play
*/
public People(Play play) {
this.play = play;
}
public void doSomething(){
play.playSomeThing();
}
}
现在有一个实现了Play接口的实现类 PlayBasketBall 。
public class PlayBasketBall implements Play {
public void playSomeThing() {
System.out.println("play basketball。。。。。。。");
}
}
我们怎么把PlayBasketBall交给People呢?
这个问题就涉及到另一个行为:装配。装配就是创建组件之间协作的行为。
Spring有多种装配方式:XML方式,java注解方式等等。
我们用xml方式装配,新建services.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="people" class="com.spring.di.People">
<constructor-arg ref="playBasketBall"/>
</bean> <bean id="playBasketBall" class="com.spring.di.PlayBasketBall">
</bean>
</beans>
对象的创建和组装
public class PlayMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("services.xml");
People people= context.getBean(People.class);
//People people= (People) context.getBean("people");
people.doSomething();
}
}
以上就是spring依赖注入的一个认识。
2. AOP面向切面编程
AOP:通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
如上面的People我们可以对doSomething方法前后动态加入一些操作。
列如加入切面:
public class PlayUtil {
public void playBefore(){
System.out.println("play before do.........");
}
public void playAfter(){
System.out.println("play after do.........");
}
}
配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="people" class="com.spring.di.People">
<constructor-arg ref="playBasketBall"/>
</bean> <bean id="playBasketBall" class="com.spring.di.PlayBasketBall">
</bean> <!-- 切面 -->
<bean id="playUtil" class="com.spring.aop.PlayUtil"/> <aop:config>
<aop:aspect ref="playUtil">
<!-- 切点 expression表达式AspectJ切点表达式语言-->
<aop:pointcut id="doSomething" expression="execution(* *.doSomething(..))"/>
<!-- 前置通知-->
<aop:before pointcut-ref="doSomething" method="playBefore"/>
<!-- 后置通知 -->
<aop:after pointcut-ref="doSomething" method="playAfter"/>
</aop:aspect>
</aop:config> </beans>
这样的方式可以把某些功能分离出来形成可重用的组件。
3.使用模板消除样板式代码
例如jdbc访问数据库代码。如果每个操作数据库的地方都写重复的连接数据库、关闭连接等操作那就太低效了。
spring的JdbcTemplate就避免了传统的样板式代码。
4.Spring容器
这是Spring框架的核心。负责创建对象、装配对象、配置对象并管理他们的整个生命周期。Spring的容器归为两类。一类是bean工厂、另一类是应用上下文。其实应用上下文也是基于BeanFactory构建。
a)应用上下文:
ClassPathXmlApplicationContext:在所有的类路径(包含jar文件)下查找xml.
FileSystemXmlApplicationContext:在指定文件系统路径下查找xml.
上下文就绪后、我们就可以通过上下文的getBean()方法从容器中获取bean. b)bean的生命周期
1、Spring对bean进行实例化
2、Spring将值和bean的引用注入到bean对应的属性中
3、如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBeanName()方法
4、如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入
5、如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext方法,将bean所在应用上下文的引用传入进来
6、如果ean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization()方法
7、如果bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet()方法,类似,如bean使用init-methon声明初始方法,该方法会被调用
8、如果bean实现BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization()方法
9、此时、bean已经准备就绪。他们将一直驻留在应用上下文中、直到该上下文被销毁
10、如果bean实现了DisposableBean接口,Spring将调用其destory()接口方法;同样、如果有指定destroy-method声明了销毁方法,该方法也会被调用
《Spring in action》之Spring之旅的更多相关文章
- 1、Spring In Action 4th笔记(1)
Spring In Action 4th笔记(1) 2016-12-28 1.Spring是一个框架,致力于减轻JEE的开发,它有4个特点: 1.1 基于POJO(Plain Ordinary Jav ...
- spring in action 4th --- quick start
读spring in action. 环境搭建 quick-start依赖注入 面向切面 1.环境搭建 jdk1.8 gradle 2.12 Intelij idea 2016.2.1 1.1创建一个 ...
- ssh整合随笔(注解方式,Spring 管理action)
Web.xml<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- Spring in action记录
最近一段时间重新学习了一遍SPRING,现在对这些笔记整理一下,一来算是对之前的学习有一个交代,二来当是重新学习一次,三来可以留下备份 这次学习中以SPRING IN ACTION 4这学习资料,整书 ...
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
- Spring in Action 4th 学习笔记 之 AOP
前提:本文中的AOP仅限于Spring AOP. 先说说为什么需要AOP 最简单的一个例子就是日志记录,如果想记录一些方法的执行情况,最笨的办法就是修改每一个需要记录的方法.但这,真的很笨... 好的 ...
- spring in action 8.1 使用Spring web flow
一.说明 Spring Web Flow是spring MVC的扩展,它支持基于流程的应用程序,他将流程的定义和实现流程行为的类和视图分离开来. 1.1 spring中配置web flow,目前需要在 ...
- 学习spring in action 第一天
这段时间,开始学习java吧,因为C sharp 学习了java的大量语法格式,所以,留意下,就不会错了,java 有的c sharp也有,而且之前我也学习过java的桌面开发,但是一下子上来就要自己 ...
- spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。
在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...
随机推荐
- c语言 error C4996: 'strupr': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
问题: 在使用visual studio 2013,进行调试执行代码时,出现如下错误: error C4996: 'strupr': The POSIX name for this item is d ...
- [LOJ#10132]异象石
Description Adera 是 Microsoft 应用商店中的一款解谜游戏. 异象石是进入 Adera 中异时空的引导物,在 Adera 的异时空中有一张地图.这张地图上 有 N 个点,有 ...
- 构造 Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!
题目传送门 /* 构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值. 另外,最多len-1次循环 */ #include <cstdio&g ...
- EditText(4)常用属性详解
常用的属性: 显示密码 通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码. editText.setTransformationMethod( ...
- SqlServer数据库(可疑)解决办法
-- 当数据库发生这种操作故障时,可以按如下操作步骤可解决此方法,打开数据库里的Sql 查询编辑器窗口,运行以下的命令. --1.修改数据库为紧急模式 ALTER DATABASE Zhangxing ...
- 为什么我的对象被 IntelliJ IDEA 悄悄的修改了?
背景 最近,在复习JUC的时候调试了一把ConcurrentLinkedQueue的offer方法,意外的发现Idea在debug模式下竟然会 "自动修改" 已经创建的Ja ...
- POJ_1018_(dp)
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28273 Accepted: ...
- vue项目中添加百度地图功能及解决遇到的问题详解
第一步,在百度地图开放平台 申请密钥 (如果有密钥可以省略此步骤,朋友有也可以借) 地址:http://lbsyun.baidu.com/ 第二步,创建应用并填写表单(下面链接可参考) http:// ...
- DeepCloneObjects 和 DeepClone
ARX AcDbDatabase 中的方法 deepCloneObjects() 和 wblock() 区别以及和 AcDbObject 方法 clone() 和 deepClone() 的关系 Ac ...
- PHP爬数据 QueryList
QueryList官方文档:https://www.querylist.cc/docs/guide/v3 因为php版本使用5.6,所以使用QueryList v3版本,php7可以使用 v4版本 v ...