《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#方法的一些规则
C# 方法 一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块.每一个 C# 程序至少有一个带有 Main 方法的类. 要使用一个方法,您需要: 定义方法 调用方法 下面是方法的各个元素: ...
- Android 性能优化(14)网络优化( 10)Determining and Monitoring the Connectivity Status
Determining and Monitoring the Connectivity Status This lesson teaches you to Determine if you Have ...
- jquery实现文字自动向上滚动,鼠标放上去停止,移开继续滚动代码...
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- win7任务计划提示”该任务映像已损坏或已篡改“怎么处理
https://jingyan.baidu.com/article/e75057f2038e2febc91a8915.html 在命令行窗口(cmd)执行命令:schtasks /query /v ...
- python程序展现图片遇到的坑
使用cv2展示图片的时候遇到了问题,提示:TypeError: Required argument 'mat' (pos 2) not found 给定的图片路径是没得问题的,代码如下: 使用open ...
- [译]Customizing Operations
Customizing Operations定制操作 There is an ongoing development today where more and more protocols are b ...
- 简单的win7-cmd命令提示符
在win7打开cmd窗口 有两个路径:(1)开始 -->所有程序 --> 附件 --> 命令提示 (2)开始 -->在搜索框输入 “cmd” 指令 作用 对文件夹的操作 ...
- 数据库恢复挂起解决办法【MSSQL】
新建查询输入如下代码运行 - -把test改成你需要修复的数据库名 USE master GO ALTER DATABASE test SET SINGLE_USER GO ALTER DATABAS ...
- dubbo面试题
40 道 Dubbo 面试题及答案:https://blog.csdn.net/BinshaoNo_1/article/details/83024303 (原地址奉上:https://mp.weixi ...
- Android 百度地图 android.view.InflateException: Binary XML file line Error inflating class com.baidu.mapapi.map.MapView
android.view.InflateException: Binary XML file line Error inflating class com.baidu.mapapi.map.MapVi ...