ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot项目多个启动类模块直接相互调用

2022-06-22 10:33:31  阅读:518  来源: 互联网

标签:调用 SpringBoot spring server client 模块 org com String


1. 项目整体结构

1.2 先创建 spring-server 模块,在 spring-client 模块引用 spring-server 模块的依赖

2. 搭建 spring-server 模块

2.1 创建一个 springboot 工程模块

2.2 修改 pom 文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.3 修改 application.properties 文件

server.port=9092

2.4 创建 ServerController

package com.server.service.impl;

import com.server.service.ServerService;
import org.springframework.stereotype.Service;

@Service
public class ServerServiceImpl implements ServerService {

    @Override
    public String selectName(String name) {
        return name;
    }

}

2.5 创建 ServerService 接口和实现类 ServerServiceImpl

package com.server.service;

public interface ServerService {

    String selectName(String name);

}

 

package com.server.service.impl;

import com.server.service.ServerService;
import org.springframework.stereotype.Service;

@Service
public class ServerServiceImpl implements ServerService {

    @Override
    public String selectName(String name) {
        return name;
    }

}

2.6 spring-server 模块结构

3. 搭建 spring-client 模块

3.1 创建一个 springboot 工程模块

3.2 修改 pom 文件,添加 spring-server 模块的依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.server</groupId>
<artifactId>spring-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

3.3 修改 application.properties 文件

server.port=9091

3.4 创建 ClientController

package com.client.controller;

import com.client.service.ClientService;
import com.server.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @Autowired
    private ServerService serverService;

    @GetMapping("/selectClientName")
    public String selectClientName(String name) {
        String result = clientService.selectName(name);
        return result;
    }

    @GetMapping("/selectServerName")
    public String selectServerName(String name) {
        String result = serverService.selectName(name);
        return result;
    }

}

3.5 创建 ClientService 接口和实现类 ClientServiceImpl

package com.client.service;

public interface ClientService {

    String selectName(String name);

}

 

package com.client.service.impl;

import com.client.service.ClientService;
import org.springframework.stereotype.Service;

@Service
public class ClientServiceImpl implements ClientService {

    @Override
    public String selectName(String name) {
        return name;
    }

}

3.6 注入 ServerService 之后发现注入失败了,原因是 spring-client 和 spring-server 下面的包结构不同

 

3.7 修改 spring-client 启动类,加入扫描包的注解

@ComponentScan(basePackages = {"com.client", "com.server"})

 

package com.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.client", "com.server"})
public class SpringClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringClientApplication.class, args);
    }

}

3.8 spring-client 模块结构

4. 测试接口功能

4.1 测试 spring-client 模块接口 /client/selectClientName

http://localhost:9091/client/selectClientName?name=呵呵

4.2 测试 spring-client 模块接口 /client/selectServerName

http://localhost:9091/client/selectServerName?name=嘻嘻

4.3 测试 spring-server 模块接口 /server/selectServerName

http://localhost:9092/server/selectServerName?name=哈哈

4.4 完整项目地址:springboot-client-server

 

标签:调用,SpringBoot,spring,server,client,模块,org,com,String
来源: https://www.cnblogs.com/liyhbk/p/16399548.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有