《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层的组件的 ...
随机推荐
- codechef: BINARY, Binary Movements
非常有毛病的一道题,我一个一个读字符死活过不去,改成整行整行读就 A 了... 做法就是...最小点覆盖... 我们发现可以把一个点向上跳看做被吃掉了,然后最顶层的点是无法向上跳所以不能被吃掉,然后被 ...
- HDU 1879(最小生成树)
#include "iostream" #include "algorithm" #include "cstdio" using names ...
- [C++ STL] 各容器简单介绍
什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...
- java 分解整数 【个 十 百】(数组案例)
求一个数两位数的个位数,十位数,百位数及千位: int num = 53; int g = (num / 1) % 10; //个位 int s = (num / 10) % 10; //十位 in ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- RabbitMQ调用
添加 gradle依赖complie("com.rabbitmq:amqp-client:5.0.0") Hello, World Working Queues Publish/S ...
- linux环境下为php7装phpredis扩展
phpredis在php7.php5下都有不同的版本,装岔了可能会编译报错,所以在安装之前请先看下自己的php是啥版本. 我的redis装的是redis3.2.3版本. 用phpinfo()查看安装的 ...
- Codeforces_768_D_(概率dp)
D. Jon and Orbs time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- java 动态代理 和动态编程
概述 代理分两种技术,一种是jdk代理(机制就是反射,只对接口操作),一种就是字节码操作技术.前者不能算技术,后者算是新的技术.未来将有大的动作或者较为广泛的应用和变革,它可以实现代码自我的编码(人工 ...
- pycharm中将ui文件转换成py文件
方法一:直接使用命令行 python -m PyQt5.uic.pyuic xx.ui -o xx.py 方法二:直接使用命令 先进到C:\python\pkgs\pyqt-5.9.2-py37h65 ...