springboot中各模块间实现bean之间互相调用(service以及自定义的bean)

1.建一个项目,两个module(注意项目和module都是创建类型如下图)

        

2. 新建module1: demo1 ; module2: demo3,实现demo3 调用 demo1 的service

建好如图:

 3.在demo3中引入demo1的依赖,demo3的pom文件中加入:

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>demo1</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

4.demo1中新建service,实现调用。

package com.example.demo1.controller;

import com.example.demo1.configure.TesttConfigure;
import com.example.demo1.service.TestService;
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("/demo1")
public class TestController {

    @Autowired
    private TestService testService;


    @Autowired
    private TesttConfigure testtConfigure;

    @GetMapping("/test1")
    public String test1(){
        System.out.println(testtConfigure.ttt());
        return testtConfigure.ttt()+testService.testService();
    }
}

5.demo1中成功调用后,demo3中直接引入依赖就可以

package com.example.demo3.controller;

import com.example.demo1.configure.TesttConfigure;
import com.example.demo1.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test3Controller {

    @Autowired
    private TestService testService;

    @Autowired
    private TesttConfigure testtConfigure;

    @RequestMapping("/test3")
    public String test3(){
        String str = testService.testService()+"------test3-------"+testtConfigure.ttt();
        return str;
    }
}

6.demo3实现调用demo1中自定义的bean。

demo1中自定义bean,启动类中配置,实现能正常能访问
自定义配置类:

package com.example.demo1.configure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TesttConfigure {


    @Bean(name = "Test1")
    public String ttt(){
        return "----tttttt-----";
    }
}

demo1中启动类设置:

package com.example.demo1;

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

@SpringBootApplication
@ComponentScan(basePackages = "com.example.demo1")
public class Demo1Application {

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

}

controller层注入,实现调用:

package com.example.demo1.controller;

import com.example.demo1.configure.TesttConfigure;
import com.example.demo1.service.TestService;
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("/demo1")
public class TestController {

    @Autowired
    private TestService testService;


    @Autowired
    private TesttConfigure testtConfigure;

    @GetMapping("/test1")
    public String test1(){
        System.out.println(testtConfigure.ttt());
        return testtConfigure.ttt()+testService.testService();
    }
}

这里的  testtConfigure 是自定义配置类。

demo1能访问成功后,demo3直接注入就可以,和上面注入demo1的service一样。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值