Spring的详解及如何使用Spring?

Spring简介:
一、专业术语
1.侵入式设计
引入框架,对现有的类的结构有影响,即需要实现或继承某些特定类。如:Struts框架
2.非侵入式设计
引入框架,对现有的类结构没有影响。如:Hibernate、Spring
3.控制反转(IoC)
控制反转(Inversion on Control 、IoC):把对象的创建交给外部容器完成。
4.依赖注入(DI)
依赖注入(dependency injection):处理对象间的依赖关系
5.IoC和DI的区别
控制反转:解决对象创建的问题【对象创建交给其他类】
依赖注入:解决类与类紧耦合的问题。
例如,类A依赖于类B,常规的做法是在A中直接创建B的对象,然后再调用B对象的方法;控制反转就是将创建B对象的实例化交给第三方实现;然后再创建类B的操作接口I,并在A中创建接口I的对象,最后再由第三方将B的实例注入给A中的接口I的对象。这样实例化B类对象的控制权就由A交给了第三方,同时也解决了类A和类B紧耦合的问题。
6.AOP
面向切面编程(Aspect Oriented Programming)是软件编程思想发展到一定阶段的产物,是面向对象编程的有益补充。AOP一般适用于具有横切逻辑的场合,如访问控制、事务管理、性能监测等。面向切面编程简单地说就是在不改变源程序的基础上为代码段增加新的功能,对代码段进行增强处理。
7.横切逻辑
在业务系统中,总有一些散落、渗透到系统各处且不得不处理的事情,这些穿插在既定业务中的公共操作就是所谓的横切逻辑,也称为切面。
8.增强处理
在目标方法执行前后进行的操作或执行的功能就是增强处理。
9.切点
可以插入增强处理的目标方法就是所谓的切点。

二、Spring简介
Spring框架可以解决对象创建以及对象之间依赖关系的一个轻量级框架。Spring是一个全面的、企业应用开发一站式的解决方案,Spring贯穿表现层、业务层、持久层。但是Spring仍然可以和其他的框架无缝整合。

三、Spring Jar包介绍
1.org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现
2.org.springframework.asm——spring3.0开始提供自己独立的asm jar包
3.org.springframework.aspects——Spring提供的对AspectJ框架的整合
4.org.springframework.beans——所有应用都用到,包含访问配置文件,创建和管理bean等。
5.org.springframework.context.support——Spring context的扩展支持,用于MVC方面
6.org.springframework.context——提供在基础IOC功能上的扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。
7.org.springframework.core——Spring的核心工具包,其他包依赖此包
8.org.springframework.expression——Spring表达式语言
9.org.springframework.instrument.tomcat——Spring对tomcat连接池的集成
10.org.springframework.instrument——Spring对服务器的代理接口
11.org.springframework.jdbc——对JDBC 的简单封装
12.org.springframework.jms——为简化jms api的使用而做的简单封装
13.org.springframework.orm——整合第三方的orm实现,如hibernate,ibatis,jdo,jpa等
14.org.springframework.oxm——Spring对于object/xml映射的支持,可以让JAVA与XML来回切换
15.org.springframework.test——对JUNIT等测试框架的简单封装
16.org.springframework.transaction——为JDBC,HIBERNATE,JDO,JPA提供一致的声明式和编程式事务管理
17.org.springframework.web.portlet——Spring MVC的增强
18.org.springframework.web.servlet——对J2EE6.0 servlet3.0的支持
19.org.springframework.web.struts——整合对struts框架的支持,更方便更容易的集成Struts框架。
20.org.springframework.web——包含Web应用开发时,用到Spring框架时所需的核心类。
21.Org.springframework.web.mvc——包含SpringMVC应用开发时所需的核心类。
注意:core、beans、expression、context是Spring的核心jar包。使用Spring时必须导入commons-logging-*.jar包。
四、applicatonContext.xml - bean配置详解
1.Spring相关的命名空间介绍
<?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:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="   
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

五、处理增强
1.处理增强的类型
前置增强MethodBeforeAdvice
后置增强AfterReturningAdvice
环绕增强MethodInterceptor
抛出异常增强ThrowsAdvice
2.实现增强的3种方式
通过接口实现增强
前置增强的接口为:MethodBeforeAdvice
后置增强的接口为:AfterReturningAdvice
环绕增强的接口为:MethodInterceptor
抛出异常增强的接口为:ThrowsAdvice
通过注解实现增强
通过Scheme配置实现增强
3.通过接口实现增强
<aop:config>
<aop:pointcut expression="execution(public void *User())" id="addoptpointcut"/>
<aop:advisor advice-ref="logbefore" pointcut-ref="addoptpointcut" />
</aop:config>

        <aop:config>
<aop:pointcut expression="execution(* spring_basic.UserService.*(..))" id="userServiceOptPointcut"/>
<!--
<aop:advisor advice-ref="exceptionAdvice" pointcut-ref="userServiceOptPointcut" />
-->
<aop:advisor advice-ref="logger" pointcut-ref="userServiceOptPointcut" />
</aop:config>
4.通过注解实现增强
前置增强     
@Before("execution(* service.*.*(..))")
后置增强
@AfterReturning(pointcut="execution(* service.*.*(..))", returning="result")
异常抛出增强 
@AfterThrowing(pointcut="execution(* service.*.*(..))", throwing="ex")
环绕增强     
@Around("execution(* service.*.*(..))")
注意:还需要在applicationContext.xml文件中添加如下配置
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
5.通过Scheme配置实现增强
首先:添加普通的类,里面按要求编写方法
public class Logger {
public void before(JoinPoint jp){
}
public void after(JoinPoint jp, Object result){
}
public void aterThrowing(JoinPoint jp, Exception ex){
}
public Object aroundExecute(ProceedingJoinPoint pjp) throws Throwable{
}
  }
其次:在applicationContext.xml中配置aop-aspect相关的配置
<aop:config>
<aop:pointcut expression="execution(* service.*.*(..))" id="optpointcut"/>
<aop:aspect ref="mylogger">
<aop:before method="before" pointcut-ref="optpc" />
<aop:after-returning method="after" returning="result" pointcut-ref="optpc" />
<aop:after-throwing method="aterThrowing" throwing="ex" pointcut-ref="optpc" />
<aop:around method="aroundExecute" pointcut-ref="optpointcut"/>
</aop:aspect>
</aop:config>
六、拆分Spring配置文件
方式一:在web.xml文件中加载多个spring配置文件
<!-- spring监听器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:applicationContext.xml,classpath:applicationContext-dao.xml,
    classpath:applicationContext-service.xml,classpath:applicationContext-action.xml,
    </param-value>
  </context-param>
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
方式二:通配符方式加载多个spring配置文件
<!-- spring监听器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:applicationContext.xml,
    classpath:applicationContext-*.xml
    </param-value>
  </context-param>
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
方式三:使用import加载其他spring配置文件
<import resource="applicationContext-dao.xml" />

猜你喜欢

转载自fei-wang.iteye.com/blog/2358216