小白的Spring框架学习之路(1)

Spring简介

1.Sring为一个开源框架,解决企业
应用程序复杂性而创建。
2.优势:分层架构,允许您使用哪一个组件,
       同时为J2EE应用程序开发提供集成的框架。
3.Spring的主要思想:IoC、AOP
   (1)IoC(Inversion of Control):控制反转/反向控制
   (2)AOP(Aspect Oriented Programming):面向切面编程
4.Spring也是一个"一站式"框架,
  即Spring的三层架构:
    表现层(Web层)
    业务逻辑层(Service层)
    数据访问层(DAO层)

Spring框架模块

这里写图片描述

(1)Core Container(核心容器):核心容器提供Spring框架的基本功能.
   核心容器的主要组件:
      BeanFactory:工厂模式的实现,使用控制反转(IoC)模式
      将应用程序的配置的依赖性规范与实际应用程序代码分开。
(2)Context(Spring应用上下文):Spring上下文的一个配置文件,
                               向Spring框架提供上下文信息。
(3)Spring AOP:通过配置管理特性,直接将面向方面的编程集成到Spring框架中。
(4)Spring DAO:JDBC DAO抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。
(5)Spring ORM:Spring框架插入了若干个ORM(对象关系映射)的对象关系工具,如:JDO,Hibernate,Mybatis SQL Map。
(6)Spring Web:Web上下文模块建立在应用程序上下文模块之上,为基于Web的应用程序提供了上下文。
(7)Spring MVC框架:MVC是一个全功能的构建Web应用程序的MVC实现。通过策略接口,MVC变成为高度可配置的,MVC容纳大量的视图技术。

Spring核心思想

基本概念
1.IoC:控制反转,控制权从应用程序转移到框架(IoC容器),是框架的共性。
2.IoC容器:实现了IoC思想的容器称为IoC容器,如:SpringFramework
3.DI:依赖注入:用一个单独的对象(装配器)来装配对象之间的依赖关系。
IoC容器的特点
 1.无需主动new对象:我们只需描述对象如何被创建,接下来就是IoC容器帮我们创建,即被动实例化。
 2.不需要主动装配对象之间的依赖关系:只需描述需要哪个服务(组件),IoC容器帮助我们配置,即被动接受装配。
 3.主动变被动(好莱坞法则)
 4.迪米特法则(最少知识原则):我们不知道依赖的具体实现,只知道需要提供某类服务的具体对象,松散耦合。
 5.IOC是一种让服务消费者不直接依赖于服务提供者的组件设计方式,减少类与类之间依赖的设计原则。
IoC容器理解的关键问题
1.谁控制谁?为什么叫反转?
  IoC容器控制,因为以前是应用程序控制,所以称之为反转。
2.控制什么?
  控制应用程序所需要的资源,如:对象,文件等。
3.为什么控制?
  解耦组件之间的关系。
4.控制的哪些方面被反转了?
  程序的控制权发生了反转,从应用程序转移到IoC容器。
Ioc思想的容器
 容器:提供组件运行环境,管理组件生命周期,不关心组件如何创建及组件之间的关系如何装配。
 IoC容器不仅具有容器的功能,还具有一些其他特性,如依赖装配。
 由于IoC太过于宽泛,提出了DI(依赖注入)
DI问题理解的关键
1.谁依赖谁
  应用程序依赖于IoC容器。
2.为什么需要依赖
  应用程序与IoC容器装配类之间存在关系。
3.依赖什么东西
  依赖了IoC容器的转配功能。
4.谁注入了谁
  IoC容器注入了应用程序。
5.注入了什么
  注入了应用程序所需要的资源。
因为IoC容器也具有依赖注入功能,所以也可以叫做DI容器。

Spring应用案例

step1:创建Maven项目(对于为什么不之间选择Spring项目?因为它不是Maven项目。。。哈哈哈)
step2:pom.xml文件中添加Spring依赖
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
</dependencies>
step3创建接口及实现类(此处提供了Shape接口包含面积、周长的计算,及三个实现类而圆Circle、矩形Rectangle、三角形Triangle)

1.Shape接口

package com.dyy.springcore.common;

public interface Shape {

    /**
    *@Description: 求取shape形状的面积
    */
    double computeArea();
    /**
     *@Description: 求取shape形状的周长
     */
    double computeSide();
}

2.Circle实现类

package com.dyy.springcore.common.impl;

import com.dyy.springcore.common.Shape;

public class Circle implements Shape {

    private final double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double computeArea() {
        return Math.PI * Math.pow(this.radius,2);
    }

    public double computeSide() {
        return 2 * Math.PI * this.radius;
    }

    public double getRadius() {
        return radius;
    }

    @Override
    public String toString() {
        return "Circle{" +
                "radius=" + radius +
                "area="+this.computeArea()+
                "side="+this.computeSide()+
                '}';
    }
}

3.Rectangle

package com.dyy.springcore.common.impl;

import com.dyy.springcore.common.Shape;

public class Rectangle implements Shape {

    private final double width;
    private final double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double computeArea() {
       return this.height * this.width;
    }

    public double computeSide() {
       return 2 * (this.width + this.height);
    }

    public double getWidth() {
        return width;
    }

    public double getHeight() {
        return height;
    }

    @Override
    public String toString() {
        return "Rectangle{" +
                "width=" + width +
                ", height=" + height +
                ",area="+this.computeArea()+
                ",side="+this.computeSide()+
                '}';
    }
}

4.Triangle

package com.dyy.springcore.common.impl;

import com.dyy.springcore.common.Shape;

public class Triangle implements Shape {

    private final double A;
    private final double B;
    private final double C;

    public Triangle(double a, double b, double c) {
        A = a;
        B = b;
        C = c;
    }


    public double computeArea() {
        double q = (this.computeSide())/2;
        return Math.sqrt((q-A)*(q-B)*(q-C)*q);
    }

    public double computeSide() {
        return (this.A + this.B + this.C);
    }

    public double getA() {
        return A;
    }

    public double getB() {
        return B;
    }

    public double getC() {
        return C;
    }

    @Override
    public String toString() {
        return "Triangle{" +
                "A=" + A +
                ", B=" + B +
                ", C=" + C +
                ",area="+this.computeArea()+
                ",side="+this.computeSide()+
                '}';
    }
}
step4创建一个测试类,通过传入不同的参数实现,实现不同图像面积、周长的计算。
package com.dyy.springcore.common.xml;

import com.dyy.springcore.common.Shape;

public class XmlShapeCompute {

    private Shape circle;
    private Shape triangle;
    private Shape rectangle;

    public Shape getCircle() {
        return circle;
    }

    public void setCircle(Shape circle) {
        this.circle = circle;
    }

    public Shape getTriangle() {
        return triangle;
    }

    public void setTriangle(Shape triangle) {
        this.triangle = triangle;
    }

    public Shape getRectangle() {
        return rectangle;
    }

    public void setRectangle(Shape rectangle) {
        this.rectangle = rectangle;
    }

    public Shape computeShape(String shapeName){
        if(shapeName.length()==0||shapeName==null){
           throw new IllegalArgumentException("Not Found");
        }
        if(shapeName.equals("circle")){
            return circle;
        }
        if(shapeName.equals("triangle")){
            return triangle;
        }
        if(shapeName.equals("rectangle")){
            return triangle;
        }
        throw new IllegalArgumentException("Not Found:"+shapeName);
    }
}
step5创建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:contex="http://www.springframework.org/schema/context"
       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">

    <bean  id="circle" class="com.dyy.springcore.common.impl.Circle">
        <constructor-arg name="radius" value="10"/>
    </bean>
    <bean id="rectangle" class="com.dyy.springcore.common.impl.Rectangle">
        <constructor-arg name="height" value="10"/>
        <constructor-arg name="width" value="10"/>
    </bean>
    <bean id="triangle" class="com.dyy.springcore.common.impl.Triangle">
        <constructor-arg name="a" value="10"/>
        <constructor-arg name="b" value="10"/>
        <constructor-arg name="c" value="10"/>
    </bean>

    <bean id="compute" class="com.dyy.springcore.common.xml.XmlShapeCompute">
        <property name="triangle" ref="triangle"/>
        <property name="rectangle" ref="rectangle"/>
        <property name="circle" ref="circle"/>
    </bean>
</beans>
6创建IoC容器使用Bean的类
package com.dyy.springcore.common.xml;

import com.dyy.springcore.common.Shape;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class XmlApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("application-content.xml");
        XmlShapeCompute compute = (XmlShapeCompute) context.getBean("compute");
        Shape shape = compute.computeShape("circle");
        System.out.println(shape);
    }
}

测试结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_40409115/article/details/80983377