背景
JavaEE 应用框架
基于IOC和AOP的结构J2EE系统的框架
IOC(反转控制):即创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象,是Spring的基础
DI(依赖注入):拿到的对象的属性,已经被注入好相关值了,直接使用即可
AOP(Aspect Oriented Program 面向切面编程)
功能分为核心业务功能(登录、增删改查)和周边功能(性能统计、日志、事务管理)
周边功能在Spring的面向切面编程AOP思想里,即被定义为切面
面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能 "编织" 在一起
IOC/DI
新建项目
添加依赖jar包
准备 pojo
配置 applicationContext.xml
Catagory.java
1 packagecom.how2java.pojo;2
3 importorg.springframework.stereotype.Component;4
5 @Component("c")6 public classCategory {7
8 public intgetId() {9 returnid;10 }11 public void setId(intid) {12 this.id =id;13 }14 publicString getName() {15 returnname;16 }17 public voidsetName(String name) {18 this.name =name;19 }20 private intid;21 private String name = "catagory 1";22 }
View Code
Product.java
1 packagecom.how2java.pojo;2
3 importorg.springframework.beans.factory.annotation.Autowired;4 importorg.springframework.stereotype.Component;5
6 @Component("p")7 public classProduct {8 private intid;9 private String name = "product 1";10
11 @Autowired12 privateCategory category;13 public intgetId() {14 returnid;15 }16 public void setId(intid) {17 this.id =id;18 }19 publicString getName() {20 returnname;21 }22 public voidsetName(String name) {23 this.name =name;24 }25 publicCategory getCategory() {26 returncategory;27 }28 public voidsetCategory(Category category) {29 this.category =category;30 }31 }
View Code
TestSpring.java
1 packagecom.how2java.test;2
3 importorg.springframework.context.ApplicationContext;4 importorg.springframework.context.support.ClassPathXmlApplicationContext;5
6 importcom.how2java.pojo.Product;7
8 public classTestSpring {9
10 public static voidmain(String[] args) {11 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml"});12
13 Product p = (Product) context.getBean("p");14 System.out.println(p.getName());15 System.out.println(p.getCategory().getName());16 }17 }
View Code
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="6 http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8 http://www.springframework.org/schema/aop
9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
10 http://www.springframework.org/schema/tx
11 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12 http://www.springframework.org/schema/context
13 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
14
15
16
17
18
19
20
21
22
23
24
View Code
AOP
辅助功能和核心业务功能彼此独立开发
“登录”功能没有“性能统计”和“日志输出”也可以正常运行
如果有需要就把“日志输出”和“登录”功能编制在一起,实现登录时看到日志输出
辅助功能叫做切面,这种选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫AOP
ProductService.java
1 packagecom.how2java.service;2
3 public classProductService {4 public voiddoSomeService() {5 System.out.println("doSomeService");6 }7 }
View Code
LoggerAspect.java
1 packagecom.how2java.aspect;2
3 importorg.aopalliance.intercept.Joinpoint;4 importorg.aspectj.lang.ProceedingJoinPoint;5
6 public classLoggerAspect {7 public Object log(ProceedingJoinPoint joinPoint) throwsThrowable{8 System.out.println("start log:" +joinPoint.getSignature().getName());9 Object object =joinPoint.proceed();10 System.out.println("end log:" +joinPoint.getSignature().getName());11 returnobject;12 }13 }
View Code
TestSpring.java
1 packagecom.how2java.test;2
3 importorg.springframework.context.ApplicationContext;4 importorg.springframework.context.support.ClassPathXmlApplicationContext;5
6 importcom.how2java.service.ProductService;7
8 public classTestSpring {9 public static voidmain(String[] args) {10 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml"});11 ProductService s = (ProductService) context.getBean("s");12 s.doSomeService();13 }14 }
View Code
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:context="http://www.springframework.org/schema/context"
7 xsi:schemaLocation="8 http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd10 http://www.springframework.org/schema/aop11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd12 http://www.springframework.org/schema/tx13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd14 http://www.springframework.org/schema/context15 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
16
17
18
19
20
21
22
23
24 expression=25 "execution(* com.how2java.service.ProductService.*(..))"/>
26
27
28
29
30
31
32
View Code
通过xml文件配置的方式,把日志记录功能(切面)和核心业务编织在了一起,而TestSpring代码没有发生任何变化(解耦)
参考
Spring DI原理
Spring的IOC原理
步骤