Spring框架:
不属于mvc架构任何一层,不是单纯的代替某一种开发技术,它的主要作用就是提供容器,进行对象管理。
开源的 轻量级的 非侵入式的 控制反转 面向切面 容器框架
开源的:免费的
轻量级的:占用空间小
非侵入式的:在使用框架过程中,几乎不需要使用它的API来编码,这样的框架就是非侵入式的。好处就是程序易于维护,易于扩展
控制反转(IOC):Spring最核心的功能
把程序中对象管理的控制反转给Spring容器(Spring核心配置文件)去做处理
A B C
A: B b = new B(); b.fun(); 耦合度
B: C c = new C(); c.fun(); 耦合度
高内聚 低耦合
IOC是一种设计思想,Spring框架对IOC思想有比较好的实现,所以我们都选用Spring框架的IOC来解决程序中对象管理的问题。
IOC的作用:尽可能降低程序的耦合度,提成程序的可维护性,可扩展性。
面向切面(AOP):面向切面的编程
把程序中要实现的功能进行分类:
横切关注点:和业务不相关,比较公共的功能点(日志管理 安全性检查…)
垂直关注点:系统要实现的业务功能点(登录 注册 下订单…)
AOP解决的是程序中横切关注点功能(程序中的日志管理,安全性检查,事务管理)的实现
AOP针对横切关注点的功能,把代码提到某一个类中去实现,在程序的编译或者运行期间,把横切关注点的代码加入到业务代码中。
好处:提升代码的开发效率,提高代码重用性
AOP是一种设计思想,Spring框架对AOP思想有比较好的实现,所以我们都选用Spring框架的AOP来解决程序中对象管理的问题。
Spring的体系结构:
Spring Core: Spring IOC功能
Spring AOP:面向切面编程
Spring ORM:提供和持久层框架进行集成的功能
Spring DAO
Spring Web: 提供了和Servlet技术进行集成的功能
Spring Webmvc:是Spring框架的子框架,用来代替Servlet技术完成控制层功能的框架
Spring Context:提供和企业级开发技术(邮件技术 付款技术…)进行集成的功能
搭建Spring框架:
- 创建普通的java工程
- 添加jar包
- 引入Spring配置文件
Spring IOC:
单独进行一个对象的管理:
依赖注入(DI)
基于xml文件进行依赖注入
构造注入
<bean id="example" class="spring.bean.ExampleBean" >
<constructor-arg>
<ref bean="first" />
</constructor-arg>
</bean>
public class ExampleBean {
private FirstBean f;
public ExampleBean(FirstBean f){
this.f=f;
}
属性注入(setter注入) 推荐使用
<bean id="newExample" class="spring.bean.NewExampleBean" >
<!--
name属性:要和类中对应的成员变量的名字相同
ref属性:要注入的bean的id
-->
<property name="firstBean" ref="first" />
</bean>
public class NewExampleBean {
private FirstBean firstBean;
public void setFirstBean(FirstBean firstBean) {
this.firstBean = firstBean;
}
接口注入
不同数据类型的依赖注入:
- 基本数据类型和String类型
<bean id="student" class="spring.bean.Student" >
<property name="sid" value="101" />
<property name="sname" value="lilei" />
<property name="sex" value="女" />
<property name="email" value="lilei@163.com" />
</bean>
- 空值的依赖注入
直接将属性值清空,或者设置为</null>
- 引用数据类型的依赖注入
<bean id="userService" class="spring.service.UserService" >
<property name="dao" ref="userDAO" />
</bean>
- 集合类型的依赖注入
<property name="list">
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
注解形式开发:
<!-- 组件扫描管理某个包中所有类的对象 -->
<context:component-scan base-package="com.service"></context:component-scan>
<context:component-scan base-package="com.dao"></context:component-scan>
@Service("userService")
public class UserService {
//@Autowired 默认就是by-type的自动依赖注入
@Autowired
private UserDAO dao;
public void login(){
System.out.println("login------------------");
dao.login();
}
}
@Component
public class UserDAO {
public void login(){
System.out.println("222222222222222222");
}
}
Spring AOP:
记录程序运行的时间
实现思路:把横切关注点的功能代码单独用一个类去实现,在程序的编译或者运行期间动态的加入该部分代码
- 通知
用于编写横切关注点代码的类
前置通知:通知的代码在业务代码执行之前就执行完毕
返回后通知:通知的代码在业务代码执行之后才执行
环绕通知:通知类的代码在业务代码执行之前会被执行,在业务代码执行之后也会执行
public class SelectAdvice implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation method) throws Throwable {
System.out.println("-------程序运行开始-------");
Date begin = new Date();
//继续执行业务代码
Object obj = method.proceed();
System.out.println("-------程序运行结束-------");
Date end = new Date();
System.out.println("程序运行的时间为:"+(end.getTime() -begin.getTime())+"ms");
return obj;
}
}
抛出通知:业务代码抛出异常时执行通知类的代码
2. 切入点
定义横切关注点的代码何时会被执行
<bean id="selectPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" >
<property name="pattern" value=".*select" />
</bean>
- 顾问
是通知和切入点的配置器
<!-- 配置顾问 -->
<bean id="selectAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor" >
<property name="advice" ref="selectAdvice" />
<property name="pointcut" ref="selectPointcut" />
</bean>
- 代理
<!-- 代理 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
AOP解决事务处理