欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

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

时间:2023-07-09

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

        

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

建好如图:

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

com.example demo1 0.0.1-SNAPSHOT

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;@RestControllerpublic 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;@Configurationpublic 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一样。

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。