spring知识总结

概述

spring是一个开源的java平台,可开发任何java应用程序,是一个轻量级框架。spring框架的目标是使j2ee开发变的更容易使用,通过启用pojo编程模型来促进良好的编程实践。

依赖注入

spring最优秀的技术是控制反转的依赖注入(DI)模式,当编写一个复杂的java应用程序时,应用程序将尽可能的独立于其他的java类来增加这些类可重用可能性,当进行复杂单元测试时,可使他们独立于其他类进行测试。依赖注入有助于将这些类粘合在一起,并在同一时间内让他们保持独立。

面向切面的程序设计(AOP)

一个程序跨越多个点的功能被称为横切关注点,这些关注点在概念上独立于应用程序的业务逻辑。比如日志记录,声明性事务,安全性,和缓存等。
AOP帮助横切面关注点从它们所影响的对象中分离出来,依赖注入将应用对象从彼此中分离出来。

spring体系

spring体系.png

核心容器由spring-core,spring-beans,spring-context,spring-context-support和spring-expression(SpEL,Spring表达式语言,Spring Expression Language)等模块组成。
数据访问/集成层包括 JDBC,ORM,OXM,JMS 和事务处理模块。
Web 层由 Web,Web-MVC,Web-Socket 和 Web-Portlet 组成。
其他一些重要的模块,像 AOP,Aspects,Instrumentation,Web 和测试模块。

安装配置

  1. 安装jdk,eclipse。
  2. 安装Apache Commons Logging API http://commons.apache.org/logging/
  3. 安装Spring框架库 http://repo.spring.io/release/org/springframework/spring

Hello world程序

HelloWorld.java

public class HelloWorld {
   private String message;
   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

MainApp.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
   }
}

src目录下配置Beans.xml




   
       
   

spring IOC容器

Spring 容器是 Spring 框架的核心。容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁。Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件。这些对象被称为 Spring Beans。配置元数据可以通过 XML,Java 注释或 Java 代码来表示,IOC容器主要有两种

  1. Spring BeanFactory 容器
    它是最简单的容器,给 DI 提供了基本的支持,它用org.springframework.beans.factory.BeanFactory 接口来定义。
  2. Spring ApplicationContext 容器
    该容器添加了更多的企业特定的功能,例如从一个属性文件中解析文本信息的能力,发布应用程序事件给感兴趣的事件监听器的能力。该容器是由 org.springframework.context.ApplicationContext 接口定义。

ApplicationContext 容器包括 BeanFactory 容器的所有功能,所以通常建议超过 BeanFactory。BeanFactory 仍然可以用于轻量级的应用程序,如移动设备或基于 applet 的应用程序,其中它的数据量和速度是显著。

Spring ApplicationContext 容器

最常被使用的 ApplicationContext 接口实现:

FileSystemXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径。

ClassPathXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。

WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。

Bean的作用域

  1. singleton
    在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,默认值
  2. prototype 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()
  3. request 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
  4. session 同一个HTTP Session共享一个Bean,不同Session使用不同的Bean,仅适用于WebApplicationContext环境
  5. global-session 一般用于Portlet应用环境,改作用于仅适用于WebApplicationContext环境。

Bean的继承

Bean 定义模板
你可以创建一个 Bean 定义模板,不需要花太多功夫它就可以被其他子 bean 定义使用。在定义一个 Bean 定义模板时,你不应该指定类的属性,而应该指定带 true 值的抽象属性,如下所示:



   
      
      
      
   

   
      
      
   

依赖注入

  1. 基于构造函数
   
      
   
   
   
   

   
      
      
   
  1. 基于设值函数


   
      
      
   
   
      
   

使用 p-namespace 实现

   
   
   
   

注入集合



   
   
      
      
         
            
            
            Pakistan
         
      
      
      
         
            
            
            Pakistan
         
      
      
      
         
            
            
            
         
      
   

自动装配

Spring 自动装配 ‘byName’
这种模式由属性名称指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName。然后,它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。


      
   

Spring 自动装配 ‘byType’
这种模式由属性类型指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType。然后,如果它的 type 恰好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和连接它的属性。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常

   
      
   

Spring 由构造函数自动装配
这种模式与 byType 非常相似,但它应用于构造器参数。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后,它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线。如果找到匹配项,它会注入这些 bean,否则,它会抛出异常。


      
   

注释方式

@Required
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。

//表示age属性必须被注入
@Required
   public void setAge(Integer age) {
      this.age = age;
   }

@Autowired
你可以在 XML 文件中的 setter 方法中使用 @Autowired 注释来除去 元素。当 Spring遇到一个在 setter 方法中使用的 @Autowired 注释,它会在方法中视图执行 byType 自动连接。

 @Autowired
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }

@Qualifier
当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱。

 @Autowired
   @Qualifier("student1")
   private Student student;

 
   
      
      
   

   
   
      
      
   

@PostConstruct 和 @PreDestroy 注释:
为了定义一个 bean 的安装和卸载,我们使用 init-method 和/或 destroy-method 参数简单的声明一下 。init-method 属性指定了一个方法,该方法在 bean 的实例化阶段会立即被调用。同样地,destroy-method 指定了一个方法,该方法只在一个 bean 从容器中删除之前被调用。

   @PostConstruct
   public void init(){
      System.out.println("Bean is going through init.");
   }
   @PreDestroy
   public void destroy(){
      System.out.println("Bean will destroy now.");
   }

@Resource
你可以在字段中或者 setter 方法中使用 @Resource 注释。@Resource 注释使用一个 ‘name’ 属性,该属性以一个 bean 名称的形式被注入。你可以说,它遵循 by-name 自动连接语义.

@Resource(name= "spellChecker")
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }

@Configuration 和 @Bean
带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。

@Configuration
public class SpringConfig {
 
    @Bean
    public Piano piano(){
        return new Piano();
    }
    @Bean(name = "counter") 
    public Counter counter(){
        return  new Counter(12,"Shake it Off",piano());
    }
}
//调用测试类
ApplicationContext annotationContext = new AnnotationConfigApplicationContext("SpringStudy");
        Counter c = annotationContext.getBean("counter", Counter.class);// 创建bean的引用对象

Spring 框架的 AOP

Spring AOP 模块提供拦截器来拦截一个应用程序,例如,当执行一个方法时,你可以在方法执行之前或之后添加额外的功能。

spring通知的类型

  1. 前置通知 在一个方法执行之前,执行通知。
  2. 后置通知 在一个方法执行之后,不考虑其结果,执行通知。
  3. 返回后通知 在一个方法执行之后,只有在方法成功完成时,才能执行通知。
  4. 抛出异常后通知 在一个方法执行之后,只有在方法退出抛出异常时,才能执行通知。
  5. 环绕通知 在建议方法调用之前和之后,执行通知。

基于AOP的@AspectJ

xml配置文件中要加入:
classpath中要包含:
aspectjrt.jar
aspectjweaver.jar
aspectj.jar
aopalliance.jar

//声明一个Aspect
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AspectModule {
}

//xml文件

   

//下面的示例中定义了一个名为 ‘getname’ 的切入点,该切入点将与 com.tutorialspoint 包下的 Student 类中的 getName() 方法相匹配:
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.tutorialspoint.Student.getName(..))") 
private void getname() {}
//你可以使用 @{ADVICE-NAME} 注释声明五个建议中的任意一个,如下所示。这假设你已经定义了一个切入点标签方法 businessService():
@Before("businessService()")
public void doBeforeTask(){
 ...
}
@After("businessService()")
public void doAfterTask(){
 ...
}
@AfterReturning(pointcut = "businessService()", returning="retVal")
public void doAfterReturnningTask(Object retVal){
  // you can intercept retVal here.
  ...
}
@AfterThrowing(pointcut = "businessService()", throwing="ex")
public void doAfterThrowingTask(Exception ex){
  // you can intercept thrown exception here.
  ...
}
@Around("businessService()")
public void doAroundTask(){
 ...
}

logging.java

package com.tutorialspoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
@Aspect
public class Logging {
   /** Following is the definition for a pointcut to select
    *  all the methods available. So advice will be called
    *  for all the methods.
    */
   @Pointcut("execution(* com.tutorialspoint.*.*(..))")
   private void selectAll(){}
   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   @Before("selectAll()")
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }
   /** 
    * This is the method which I would like to execute
    * after a selected method execution.
    */
   @After("selectAll()")
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }
   /** 
    * This is the method which I would like to execute
    * when any method returns.
    */
   @AfterReturning(pointcut = "selectAll()", returning="retVal")
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }
   /**
    * This is the method which I would like to execute
    * if there is an exception raised by any method.
    */
   @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }  
}

//xml文件中有

 

你可能感兴趣的:(spring知识总结)