Spring框架介绍

本文介绍了 Spring框架

Srping简介

广义上的 Spring 泛指以 Spring Framework 为核心的 Spring 技术栈。

经过十多年的发展,Spring 已经不再是一个单纯的应用框架,而是逐渐发展成为一个由多个不同子项目(模块)组成的成熟技术,例如 Spring Framework、Spring MVC、SpringBoot、Spring Cloud、Spring Data、Spring Security 等,其中 Spring Framework 是其他子项目的基础。

Spring 框架是一个分层的、面向切面的 Java 应用程序的一站式轻量级解决方案,它是 Spring 技术栈的核心和基础,是为了解决企业级应用开发的复杂性而创建的。

Spring 有两个核心部分: IoC 和 AOP。

IOC: Inverse of Control 的简写,译为“控制反转”,指把创建对象过程交给 Spring 进行管理。
AOP: Aspect Oriented Programming 的简写,译为“面向切面编程”。

AOP 用来封装多个类的公共行为,将那些与业务无关,却为业务模块所共同调用的逻辑封装起来,减少系统的重复代码,降低模块间的耦合度。另外,AOP 还解决一些系统层面上的问题,比如日志、事务、权限等。

Spring 是一种基于 Bean 的编程技术,它深刻地改变着 Java 开发世界。Spring 使用简单、基本的 Java Bean 来完成以前只有 EJB 才能完成的工作,使得很多复杂的代码变得优雅和简洁,避免了 EJB 臃肿、低效的开发模式,极大的方便项目的后期维护、升级和扩展。

在实际开发中,服务器端应用程序通常采用三层体系架构,分别为表现层(web)、业务逻辑层(service)、持久层(dao)。

Spring 致力于 Java EE 应用各层的解决方案,对每一层都提供了技术支持。

  • 在表现层提供了对 Spring MVC、Struts2 等框架的整合;
  • 在业务逻辑层提供了管理事务和记录日志的功能;
  • 在持久层还可以整合 MyBatis、Hibernate 和 JdbcTemplate 等技术,对数据库进行访问。

spring 框架图:
spring

反转控制和依赖注入

要了解这个问题,我们使用以下几类:

// Interface HelloWorld
public interface HelloWorld {
    public void sayHello();
}
 
// Class implements HelloWorld
public class SpringHelloWorld implements HelloWorld {
   public void sayHello()  {
           System.out.println("Spring say Hello!");
   }
}
 
// Other class implements HelloWorld
public class StrutsHelloWorld implements HelloWorld {
   public void sayHello()  {
           System.out.println("Struts say Hello!");
   }
}
 
 
// And Service class
public class HelloWorldService {
    
     // Field type HelloWorld
     private HelloWorld helloWorld;
    
     // Constructor HelloWorldService
     // It initializes the values for the field 'helloWorld'
     public HelloWorldService()  {
           this.helloWorld = new StrutsHelloWorld();
     }
 
}

显而易见的是 HelloWorldService 类管理创建 HelloWorld 对象。 - 另外,在上述情况下,当 HelloWorldService 对象从它的构造创建时,HelloWorld对象也被创建了。 它是从StrutsHelloWorld 创建。

现在的问题是,您要创建一个HelloWorldService对象,HelloWorld对象也同时被创建,但它必须是SpringHelloWorld。 所以 HelloWorldService 是控制“对象创建” Hello World 的。我们为什么不创建 Hello World 转让由第三方, 而是使用 HelloWorldService ?因为我们有“反转控制”(IOC)的定义。 并且IoC容器将充当管理者角色,创建了HelloWorldService 和 HelloWorld 。

IoC = Inversion of Control

spring ioc

IoC容器创建 HelloWorldService 对象,是通过 setter 方法传递 HelloWorld 对象到HelloWorldService。IoC容器做的是“依赖注入”到HelloWorldService。这里的相关性是指对象之间的依赖关系: HelloWorldService 和 helloWorld.在这一点上,我们已经明确了什么是 IoC和DI。让我们举个例子来更好的理解。

声明Spring的基础库

这是 Spring的 HelloWorld 例子,所以我们只使用基本的Spring库(核心)。打开pom.xml文件来将使用的库声明:

• pom.xml 使用以下内容重新覆盖原上面的内容。

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                         http://maven.apache.org/xsd/maven-4.0.0.xsd">
      
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.yiibai</groupId>
  <artifactId>HelloSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>

      <dependencies>
 
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
    </dependencies>
  
</project>

工程代码实例

  • HelloWorld.java
package com.yiibai.tutorial.spring.helloworld;
 
public interface HelloWorld {
     
    public void sayHello();
     
}
  • HelloWorldService.java
package com.yiibai.tutorial.spring.helloworld;
  
public class HelloWorldService {
  
    private HelloWorld helloWorld;
  
    public HelloWorldService() {
  
    }
  
    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }
    public HelloWorld getHelloWorld() {
        return this.helloWorld;
    }
}
  • SpringHelloWorld.java
package com.yiibai.tutorial.spring.helloworld.impl;
  
import com.yiibai.tutorial.spring.helloworld.HelloWorld;
  
public class SpringHelloWorld implements HelloWorld {
  
    @Override
    public void sayHello() {
        System.out.println("Spring Say Hello!!");
    }
  
}

• StrutsHelloWorld.java 


package com.yiibai.tutorial.spring.helloworld.impl;
  
import com.yiibai.tutorial.spring.helloworld.HelloWorld;
  
public class StrutsHelloWorld implements HelloWorld {
  
    @Override
    public void sayHello() {
        System.out.println("Struts Say Hello!!");
    }
  
}
  • HelloProgram.java
package com.yiibai.tutorial.spring;
  
import com.yiibai.tutorial.spring.helloworld.HelloWorld;
import com.yiibai.tutorial.spring.helloworld.HelloWorldService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class HelloProgram {
  
     
    public static void main(String[] args) {
         
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");
         
        HelloWorldService service =
             (HelloWorldService) context.getBean("helloWorldService");
          
        HelloWorld hw= service.getHelloWorld();
         
        hw.sayHello();
    }
}
  • beans.xml
<beansxmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
  
    <beanid="springHelloWorld"
        class="com.yiibai.tutorial.spring.helloworld.impl.SpringHelloWorld"></bean>
    <beanid="strutsHelloWorld"
        class="com.yiibai.tutorial.spring.helloworld.impl.StrutsHelloWorld"></bean>
  
  
    <beanid="helloWorldService"
        class="com.yiibai.tutorial.spring.helloworld.HelloWorldService">
        <propertyname="helloWorld"ref="springHelloWorld"/>
    </bean>
  
</beans>

运行示例

运行 HelloProgram 类的结果如下:
运行结果

打开 beans.xml 文件并更改配置:

<!-- Original -->
<beanid="helloWorldService"
    class="com.yiibai.tutorial.spring.helloworld.HelloWorldService">
   <propertyname="helloWorld"ref="springHelloWorld"/>
</bean>
 
<!-- Change to: -->
<beanid="helloWorldService"
    class="com.yiibai.tutorial.spring.helloworld.HelloWorldService">
   <propertyname="helloWorld"ref="strutsHelloWorld"/>
</bean>

Spring的工作原理

Spring在这个例子中,工作原理说明如下图所示:
spring原理图

beans.xml

• 这是一个配置文件,您可以在这里声明Java bean。

可以通过读取beans.xml 文件来创建一个应用程序上下文对象

ApplicationContext context = newClassPathXmlApplicationContext("beans.xml");

IoC容器中,其作用是作为第三种角色,它的任务是创建 beans.xml 文件中声明的 Java Bean 对象。并通过setter方法注入依赖

在这个例子中,HelloWorldService 是一个 java bean 注入依赖。

<!-- beans.xml -->
 
<beanid="helloWorldService"
    class="com.yiibai.tutorial.spring.helloworld.HelloWorldService">
    
    <!-- Call: helloWorldService.setHelloWorld(springHelloWorld) -->
    <propertyname="helloWorld"ref="springHelloWorld"/>
 
</bean>
下一篇


推荐文章

评论
说点什么吧?

发表评论

取消回复
  最新文章