Spring总结

IOC-反转控制

Inversion of control 反转控制。 比如以前创建一个对象,需要自己主动new 一个对象,通过IOC,对象的创建交由Spring框架 创建,开发人员直接使用已经创建好的对象。

传统的方式:
通过new 关键字主动创建一个对象
IOC方式
对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是反转控制 (Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。

打个比喻:
传统方式:相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上花椒,酱油,烤制,经过各种工序之后,才可以食用。
用 IOC:相当于去馆子(Spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管吃就行了。

DI-依赖注入

Dependency Injection 依赖注入。 通过IOC创建对象的时候,可以注入字符串甚至其他对象。 比如DAO就会注入session factory。简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可

通常IOC和DI是紧密结合,一起使用的

AOP-切面编程

把功能划分为核心业务功能和其他的周边辅助功能,比如日志,性能统计,事务等等。

其他的周边辅助功能可以都看作切面功能。

核心功能和切面功能分别独立开发,通过面向切面编程,可以有机的把核心业务功能和切面功能根据需求结合在一起。 比如增加操作可以和事务切面结合在一起,查询操作可以和性能统计切面结合在一起。
在配置方面,要配置切面,切点,并且通过aspect:config 把切面和切点结合起来。

AOP的目的

AOP能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任(例如事务处理、日志管理、权限控制等)封装起来,便于减少系统的重复代码降低模块间的耦合度,并有利于未来的可拓展性和可维护性

AOP 当中的概念

  • 切入点(Pointcut)
    在哪些类,哪些方法上切入(where
  • 通知(Advice)
    在方法执行的什么实际(when:方法前/方法后/方法前后)做什么(what:增强的功能)
  • 切面(Aspect)
    切面 = 切入点 + 通知,通俗点就是:在什么时机,什么地方,做什么增强!
  • 织入(Weaving)
    把切面加入到对象,并创建出代理对象的过程。(由 Spring 来完成)

举例

在上面的例子中,包租婆的核心业务就是签合同,收房租,那么这就够了,灰色框起来的部分都是重复且边缘的事,交给中介商就好了,这就是 AOP 的一个思想:让关注点代码与业务代码分离!

代码

1.在 Package【pojo】下新建一个【Landlord】类(包租婆的英文):

1
2
3
4
5
6
7
8
9
10
11
12
13
package pojo;

import org.springframework.stereotype.Component;

@Component("landlord")
public class Landlord {

public void service() {
// 仅仅只是实现了核心的业务功能
System.out.println("签合同");
System.out.println("收房租");
}
}

2.在 Package【aspect】下新建一个中介商【Broker】类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
class Broker {

@Before("execution(* pojo.Landlord.service())")
public void before(){
System.out.println("带租客看房");
System.out.println("谈价格");
}

@After("execution(* pojo.Landlord.service())")
public void after(){
System.out.println("交钥匙");
}
}

3.在 applicationContext.xml 中配置自动注入,并告诉 Spring IoC 容器去哪里扫描这两个 Bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package="aspect" />
<context:component-scan base-package="pojo" />

<aop:aspectj-autoproxy/>
</beans>

4.在 Package【test】下编写测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Landlord;

public class TestSpring {

public static void main(String[] args) {

ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Landlord landlord = (Landlord) context.getBean("landlord", Landlord.class);
landlord.service();

}
}