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
    评论
以下是一个简单的 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、付费专栏及课程。

余额充值