Spring 框架
Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。
- 目的:解决企业应用开发的复杂性
- 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
- 范围:任何Java应用
Spring是一个轻量级控制反转(IOC)和面向切面编程(AOP)的容器框架。
所以呢,spring中比较重要的两个概念就是 控制反转IOC 和 面向切面编程AOP。
控制反转IOC
控制反转的意思就是以前的控制权在自己手中,现在是把控制权交给了spring。比如获取对象,传统的方式是通过new关键字创建一个对象,而在spring框架中,对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。
其中最常见的方式叫做 依赖注入(Dependency Injection,简称DI),简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。
通过代码解释一下什么是控制反转IOC和依赖注入DI
1.在IDEA中创建一个spring项目(详细步骤自己百度)
2.在src目录下新建一个Category类
package com.pojo;
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.在src目录下新建applicationContext.xml文件,对Category对象注入name属性。
<?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">
<bean name="c" class="com.pojo.Category">
<property name="name" value="category 1"></property>
</bean>
</beans>
4.在src目录下新建测试文件TestSpring,演示通过spring获取Category对象,以及该对象被注入的name属性。
package com.test;
import com.pojo.Category;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Category c = (Category) context.getBean("c");
System.out.println(c.getName());
}
}
5.运行测试类,查看结果
项目整体目录
面向切面编程AOP
AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等
周边功能在Spring的面向切面编程AOP思想里,即被定义为切面
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能 “编织” 在一起,这就叫AOP
通过代码解释一下什么是面向切面编程AOP
1.在上面的基础上,同样在src目录下准备业务类 ProductService
package com.service;
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
2.在applicationContext.xml中声明业务对象
<bean name="s" class="com.service.ProductService">
</bean>
3.在引入切面之前,调用该业务类进行测试
package com.test;
import com.service.ProductService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
ProductService s = (ProductService) context.getBean("s");
s.doSomeService();
}
}
运行结果
4.准备日志切面 LoggerAspect
package com.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggerAspect {
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
5.在applicationContext.xml中添加以下内容,将把切面和核心业务功结合在一起
<aop:config>
<aop:pointcut id="loggerCutpoint"
expression="execution(* com.service.ProductService.*(..)) "/>
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
6.运行测试,可以发现在编织之后,业务方法运行之前和之后分别会打印日志
项目整体目录
以上就是spring中的两个比较重要的概念 控制反转IOC(Inversion Of Control ) 和 面向切面编程AOP(Aspect Oriented Program)
(项目中用到一些额外的JAR包,这里就不详细介绍了,想要自己动手完成项目的朋友可以参考http://how2j.cn/k/spring/spring-ioc-di/87.html中的教程)