SpringCore的案例1

一、如何创建一个SpringCore的工程
1.创建项目并且创建如下的关系类
在这里插入图片描述
2.pom.xml文件的配置
添加依赖:Spring的物料清单

<?xml version="1.0" encoding="UTF-8"?>
<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.wschase.springcore</groupId>
    <artifactId>javalianxi-springcore-case1</artifactId>
    <version>1.0.0</version>

    <!--这是一个Springcore的案例:我们需要添加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>
<!--Spring框架依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
    </dependencies>
</project>

在这里插入图片描述
配置完以后我们发现在依赖里面总共有5个jar包(将Spring框架里面的jar包都加进来),每个jar包都称之为模块。表示spring-context依赖了下面的4个包。
3.定义Shape接口,定义计算图形面积和周长的方法

package com.wschase.springcore.common;

/**
 * Author:WSChase
 * Created:2019/5/4
 */
public interface Shape {
    /**
     * 计算图形面积
     */
    double computeArea();

    /**
     * 计算图形边长
     */
    double computeSide();
}

4.分别创建不同的图形实现Shape接口
(1)圆形
注意:final修饰的属性没有set方法

package com.wschase.springcore.common.impl;

import com.wschase.springcore.common.Shape;

/**圆形
 * Author:WSChase
 * Created:2019/5/4
 */
public class Circular implements Shape {
    //计算圆形的面积我们自需要半径
    private final double radius;


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

    public double getRadius() {
        return radius;
    }


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

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

(2)矩形

package com.wschase.springcore.common.impl;

import com.wschase.springcore.common.Shape;

/**长方形
 * Author:WSChase
 * Created:2019/5/4
 */
public class Rectangle implements Shape {
    private final double height;
    private final double width;

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

    public double getHeight() {
        return height;
    }

    public double getWidth() {
        return width;
    }

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

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

(3)三角形

package com.wschase.springcore.common.impl;

import com.wschase.springcore.common.Shape;

/**三角形
 * Author:WSChase
 * Created:2019/5/4
 */
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) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getA() {
        return a;
    }

    public double getB() {
        return b;
    }

    public double getC() {
        return c;
    }

    public double computeArea() {
        double q=(this.computeSide())/2;
        double s=Math.sqrt(((a-q)*(b-q)*(c-q))*q);
        return s;
    }

    public double computeSide() {
        return this.getC()+this.getA()+this.getB();
    }
}

5.实现
(1)

package com.wschase.springcore.xml;

import com.wschase.springcore.common.Shape;

/**
 * Author:WSChase
 * Created:2019/5/4
 */
public class XmlShapeCompute {
    private Shape circular;
    private Shape triangle;
    private Shape rectangle;

    public Shape getCircular() {
        return circular;
    }

    public void setCircular(Shape circular) {
        this.circular = circular;
    }

    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==null||shapeName.length()==0){
            throw new IllegalArgumentException("Not Found");
        }
        if(shapeName.equals("circular")){
            return circular;
        }
        if(shapeName.equals("triangle")){
            return triangle;
        }
        if(shapeName.equals("rectangle")){
            return rectangle;
        }
        throw new IllegalArgumentException("Not Found");
    }


}

(2)我们可以看到在这个实现类里面我们并没有通过new创建对象,只是通过new创建了容器,然后容器在我们的配置文件中实现自动给装配,实例化对象

package com.wschase.springcore.xml;

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

/**
 * Author:WSChase
 * Created:2019/5/4
 */
public class XmlApplication {
    public static void main(String[] args) {
        //此时我们创建对象的时候不需要new对象了,直接创建一个容器容器帮我们创建对象:容器会到我们的配置文件中找对应的对象
        ApplicationContext context=new ClassPathXmlApplicationContext("application-context.xml");
        //下面就是创建对象
        XmlShapeCompute xmlShapeCompute= (XmlShapeCompute) context.getBean("xmlShapeCompute");
        Shape shape=xmlShapeCompute.computeShape(args[0]);
        System.out.println(shape);
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得
以下是一个简单的 Spring WebFlux 案例: 1. 首先,需要创建一个 Maven 项目,并在 pom.xml 文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` 2. 接下来,创建一个简单的 REST API,用于返回一些用户数据。在 src/main/java 目录下创建一个名为 UserController 的类: ``` package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux; @RestController public class UserController { @GetMapping("/users") public Flux<User> getUsers() { return Flux.just( new User("John", "Doe"), new User("Jane", "Doe"), new User("Bob", "Smith") ); } public static class User { private String firstName; private String lastName; public User(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } } ``` 3. 启动 Spring Boot 应用程序并访问 http://localhost:8080/users,即可获取用户数据。 这是一个简单的 Spring WebFlux 案例,它使用了 Reactor 的组件来实现响应式编程。使用 Spring WebFlux,可以轻松地构建响应式 Web 应用程序,以满足大量并发访问的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值