回顾Spring框架
Spring框架:
传统JavaEE解决企业级应用问题时的“重量级”架构体系,使它的开发效率,开发难度和实际的性能都令人失望。Spring是以一个
救世主的身份降临在广大的程序员面前。Spring致力于JavaEE应用的各种解决方案,而不是仅仅专注于某一层的方案也可以说Spring
是一个企业级应用开发的一站式选择。Spring贯穿表现层,业务层,持久层。然而,Spring并不是为了取代他们而出现而是以高度的
开放性与它们无缝整合。
Spring核心:
IOC控制反转(Inversion of Control )也被称为依赖注入(Dependency Injection ,DI)是面向对象编程中的一种设计理念,用来减低
程序代码之间的耦合度
AOP面向切面编程(Aspect Oriented Programming,AOP)是软件编程思想发展到一定阶段的产物是面向对象编程(Object Oriented Programming)
的有益补充。AOP一般适用于有横切逻辑的场合,如访问控制,事务管理,性能检测等。
AOP Aspect Oriented Programming 面向切面编程
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
面向对象编程是从【静态角度】考虑程序的结构,而面向切面编程是从【动态角度】考虑程序运行过程。
AOP底层,就是采用【动态代理】模式实现的。采用了两种代理:JDK动态代理和CGLIB动态代理。
基本术语(一些名词):
(1)切面(Aspect)
切面泛指[*交叉业务逻辑*]。事务处理和日志处理可以理解为切面。常用的切面有通知(Advice)与顾问(Advisor)。实际就是对主业务逻辑的一种增强。
(2)织入(Weaving)
织入是指将切面代码插入到目标对象的过程。代理的invoke方法完成的工作,可以称为织入。
(3) 连接点(JoinPoint)
连接点是指可以被切面织入的方法。通常业务接口的方法均为连接点
(4)切入点(PointCut)
切入点指切面具体织入的方法
注意:被标记为final的方法是不能作为连接点与切入点的。因为最终的是不能被修改的,不能被增强的。
(5)目标对象(Target)
目标对象指将要被增强的对象。即包含主业务逻辑的类的对象。
(6)通知(Advice)
通知是切面的一种实现,可以完成简单的织入功能。通知定义了增强代码切入到目标代码的时间点,是目标方法执行之前执行,还是执行之后执行等。切入点定义切入的位置,通知定义切入的时间。
(7)顾问(Advisor)
顾问是切面的另一种实现,能够将通知以更为复杂的方式织入到目标对象中,是将通知包装为更复杂切面的装配器。
6.AOP是一种思想,而非实现
AOP是基于OOP,而又远远高于OOP,主要是将主要核心业务和交叉业务分离,交叉业务就是切面。例如,记录日志和开启事务。
=====通知Advice
7.前置通知 MethodBeforeAdvice
定义前置通知,需要实现MethodBeforeAdvice接口,该接口中有一个方法before(),会在目标方法执行之前执行。
特点:
1.在目标方法之前执行
8.后置通知 AfterReturningAdvice
9.环绕通知 MethodInterceptor
10.异常通知 ThrowsAdvice
定义异常通知,需要实现接口ThrowAdvice接口 。该接口的主要作用是,在目标方法抛出异常后,根据异常的不同做出相应的处理。当该接口处理完异常后,会简单地将异常再次抛出给目标方法。
=====顾问Advisor
11.通知Advice是Spring提供的一种切面(Aspect)。但其功能过于简单,只能
将切面织入到目标类的所有目标方法中,无法完成将切面织入到指定目标方法中。
顾问Advisor是Spring提供的另一种切面。其可以完成更为复杂的切面织入功能。PointcutAdvisor是顾问的一种,可以指定具体的切入点。顾问将通知进行了包装,会根据不同的通知类型,在不同的时间点,将切面织入到不同的切入点。
PointcutAdvisor接口有两个较为常用的实现类:
*:NameMatchMethodPointcutAdvisor 名称匹配方法切入点顾问
*:RegexpMethodPointcutAdvisor 正则表达式匹配方法切入点顾问
<property name="pattern" value=".*do.*"></property> 表示方法全名(包名,接口名,方法名)
运算符 名称 意义
. 点号 表示任意单个字符
+ 加号 表示前一个字符出现一次或者多次
* 星号 表示前一个字符出现0次或者多次
=====默认Advisor自动代理生成器
DefaultAdvisorAutoProxyCreator
=====BeanName自动代理生成器
BeanNameAutoProxyCreator
=====AspectJ对AOP的实现
对于AOP这种编程思想,很多框架都进行了实现。Spring就是其中之一,可以完成面向切面编程。然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且还支持注解式开发。所以,Spring又将AspectJ对于AOP的实现也引入到了自己的框架中。
在Spring中使用AOP开发时,一般使用AspectJ的实现方式。

IOC和AOP使用扩展
使用多种方式实现依赖注入:
第一步创建出Car类
package cn.happy.day01.entity;
public class Car {
private String color;
@Override
public String toString() {
return "Car [color=" + color + "]";
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
第二步创建出一个实体类Student
package cn.happy.day01.entity;
public class Student {
private String name;
private int age;
private Car myCar;
public Student() {
System.out.println("init student");
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", myCar=" + myCar
+ "]";
}
public Student(String name, int age, Car myCar) {
System.out.println("i am has args constructor");
this.name = name;
this.age = age;
this.myCar = myCar;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getMyCar() {
return myCar;
}
public void setMyCar(Car myCar) {
this.myCar = myCar;
}
}
接下来就就是编写applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
"> <bean id="mmCar" class="cn.happy.day01.entity.Car">
<property name="color" value="green color"></property>
</bean> <!-- <bean id="stu1" class="cn.happy.day01.entity.Student">
<property name="name" value="微冷的雨"></property>
<property name="age" value="18"></property>
<property name="myCar" ref="mmCar"></property>
</bean> --> <!--01.构造器注入 -->
<bean id="stu2" class="cn.happy.day01.entity.Student">
<constructor-arg index="0" value="微冷的雨"></constructor-arg>
<constructor-arg index="1" value="18"></constructor-arg>
<constructor-arg index="2" ref="mmCar"></constructor-arg>
</bean> <!--02.p命名空间注入 -->
<bean id="stu3" class="cn.happy.day01.entity.Student" p:name="国庆放假买手机" p:age="18" p:myCar-ref="mmCar"> </bean> <!--05.各种类型注入之 集合 之 List-->
<bean id="list" class="cn.happy.day01.entity.CollectionBean">
<property name="list">
<list>
<value>方言</value>
<value>李小龙</value>
</list>
</property>
</bean>
<!--06.各种类型注入之 集合 之 Set-->
<bean id="set" class="cn.happy.day01.entity.CollectionBean">
<property name="set">
<set>
<value>方言</value>
<value>李小龙</value>
</set>
</property>
</bean> <!--07.各种类型注入之 集合 之 Map-->
<bean id="map" class="cn.happy.day01.entity.CollectionBean">
<property name="map">
<map>
<entry key="fy">
<value>方言</value>
</entry>
<entry key="lxl">
<value>李小龙</value>
</entry>
</map>
</property>
</bean> </beans>
关于上面注入集合的内容创建一个集合的类即可:
package cn.happy.day01.entity; import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; /**
* 集合Bean
* @author Happy
*
*/
public class CollectionBean {
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private String [] empName;//数组
private Properties pp;//Properties的使用 public String[] getEmpName() {
return empName;
} public void setEmpName(String[] empName) {
this.empName = empName;
} public Properties getPp() {
return pp;
} public void setPp(Properties pp) {
this.pp = pp;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public Set<String> getSet() {
return set;
} public void setSet(Set<String> set) {
this.set = set;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} }
最后我们编写测试类测试即可
package cn.happy.day01.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.happy.day01.entity.CollectionBean;
import cn.happy.day01.entity.Student;
public class Spring_01Test {
public static void main(String[] args) {
testCollectionMap();
}
/**
* 1.1 spring容器创建的时候,会将所有配置的bean对象创建
*/
/*@Test
public void testOne(){
//容器一旦生成,bean都被加载到内存
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student)ctx.getBean("stu1");
System.out.println(stu); Student stu = (Student)ctx.getBean("stu1");
Student stu2 = (Student)ctx.getBean("stu1");
System.out.println(stu);
System.out.println(stu2); }*/ static void testCollectionMap(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean bean=(CollectionBean)ctx.getBean("map");
System.out.println(bean.getMap());
} //every type injection of Collection Set
static void testCollectionSet(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean bean=(CollectionBean)ctx.getBean("set");
System.out.println(bean.getSet());
} //every type injection of Collection List
static void testCollectionList(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean bean=(CollectionBean)ctx.getBean("list");
System.out.println(bean.getList());
} //04.域属性的注入
static void testJavaBean(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu=(Student)ctx.getBean("stu1");
System.out.println(stu);
} //03.普通属性注入
static void testUseful(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); } //02.P命名空间注入
static void testPInjection(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student)ctx.getBean("stu3");
System.out.println(stu);
}
//01.1构造注入
static void testConstructorInjection2(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student)ctx.getBean("stu2");
System.out.println(stu); } //01.构造注入
static void testConstructorInjection(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student)ctx.getBean("stu2");
System.out.println(stu); } }
回顾Spring框架的更多相关文章
- Spring框架(4)---AOP讲解铺垫
AOP讲解铺垫 不得不说,刚开始去理解这个Aop是有点难理解的,主要还是新的概念比较多,对于初学者一下子不一定马上能够快速吸收,所以我先对什么事Aop做一个解释: 首先说明:本文不是自己所写 ...
- spring框架总结(04)----介绍的是Spring中的JDBC模板
1.1 Jdbc模板概述 它是spring框架中提供的一个对象,是对原始Jdbc API对象的简单封装.spring框架为我们提供了很多的操作模板类,入下图所示: 我们今天的主角在spring-jd ...
- spring框架的一些技术总结
纵观现在互联网行业,java的地位依然不可动摇,因为java拥有着的众多开发人员和适用性广,技术与解决技术大部分开源等特点,因此java依然是众多众多开发行业作为互联网开发的首选,而说到开发,我们就不 ...
- Spring框架源码阅读之Springs-beans(一)容器的基本实现概述(待续)
去年通过实际框架代码的阅读,以及结合<Spring源码深度解析>和<Spring技术内幕>的阅读,对Spring框架内Bean模块有了一个整体性的认识.对此进行的总结性整理和回 ...
- Spring框架的第四天(整合ssh框架)
## Spring框架的第四天 ## ---------- **课程回顾:Spring框架第三天** 1. AOP注解方式 * 编写切面类(包含通知和切入点) * 开启自动代理 2. JDBC模板技术 ...
- Spring框架第二天
## Spring框架第二天 ## ---------- **课程回顾:Spring框架第一天** 1. 概述 * IOC和AOP 2. 框架的IOC的入门 * 创建applicationContex ...
- Spring框架的第三天
## Spring框架的第三天 ## ---------- **课程回顾:Spring框架第二天** 1. IOC的注解方式 * @Value * @Resource(name="" ...
- spring 框架整合mybatis的源码分析
问题:spring 在整合mybatis的时候,我们是看不见sqlSessionFactory,和sqlsession(sqlsessionTemplate 就是sqlsession的具体实现)的,这 ...
- Spring框架的演变
什么是Spring 如果想要解释Spring,那么最难的部分就是对其进行分类.通常情况下,Spring被描述为构建Java应用程序的轻量级框架,但这种描述带来了两个有趣的观点. 首先,与许多其他框架( ...
随机推荐
- Entity Framework 第八篇 结构优化
在之前的文章里,业务层直接调用一个包装的仓储类入口,忽略了DAL层,在业务层绕过DAL直接调用仓储类似乎也没什么大的问题,但是这样做有一个很大的弊端,就是无法做到DAL层的原子操作的复用.假如多个业务 ...
- IntelliJ IDEA使用(1)——IDEA配置JDK
提前安装jdk,配置环境变量 一.配置jdk 1.依次点开File -->Project Structure,点击左侧标签页,点击SDKs 2.点击+号,选SDK 3.在弹出框选择jdk路径(我 ...
- Hadoop笔记HDFS(1)
环境:Hadoop2.7.3 1.Benchmarking HDFS 1.1测试集群的写入 运行基准测试是检测HDFS集群是否正确安装以及表现是否符合预期的好方法.DFSIO是Hadoop自带的一个基 ...
- JDBC读取新插入Oracle数据库Sequence值的5种方法
Oracle的sequence实现非常灵活,所以也带来一些易用性问题,如何取到新插入记录生成的sequence值与其它数据库有较大差别,本文详国介绍了5种实现读取新插入记录sequence值的方法. ...
- html5获取经纬度,百度api获取街区名,并使用JS保存进cookie
引用js<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak= ...
- CentOS 7 中设置启动模式
1.命令行模式systemctl set-default multi-user.target2.图形模式systemctl set-default graphical.target
- android应用锁之监听应用前后台切换方式
今天在做技术总结,顺便就把知识共享,个人崇尚分享. 通过以下方式来监听是不是发生了应用的前后台切换: 1. android api 10 – 15 通过ActivityManager register ...
- spring4+mybatis3+maven
简介 在上一篇博文中,我们搭建了maven环境,现在我们就用maven搭个ssm框架,废话不多说,直接开始吧 代码下载地址 链接:http://pan.baidu.com/s/1nvg42EH 密码: ...
- BSD Apache GPL LGPL MIT
当Adobe.Microsoft.Sun等一系列巨头开始表现出对”开源”的青睐时,”开源”的时代即将到来! 最初来自:sinoprise.com/read.php?tid-662-page-e-fpa ...
- Minimum Path Sum [LeetCode]
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...