Spring boot 一个模块是否可以读取另一个模块';豆子?
我有两个SpringBoot模块<代码>公共空间和Spring boot 一个模块是否可以读取另一个模块';豆子?,spring-boot,maven-module,Spring Boot,Maven Module,我有两个SpringBoot模块公共空间和网络 在commons模块中,我定义了一个bean: 我可以在commons测试中得到这个bean 但不幸的是,我无法从另一个模块获取bean。 我弄错什么了吗?我想从我的web模块中获取commons模块中定义的bean 这是我的moduleApplication.java package com.github.fish56.modules; 导入org.springframework.boot.SpringApplication; 导入org.
网络
在commons
模块中,我定义了一个bean:
我可以在commons
测试中得到这个bean
但不幸的是,我无法从另一个模块获取bean。
我弄错什么了吗?我想从我的web
模块中获取commons
模块中定义的bean
这是我的moduleApplication.java
package com.github.fish56.modules;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
@SpringBoot应用程序
公共类模块应用程序{
公共静态void main(字符串[]args){
run(moduleapplication.class,args);
}
}
modulesApplicationContest.java
package com.github.fish56.modules;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.test.context.junit4.SpringRunner;
导入静态org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBoot应用程序
公共类模块应用程序测试{
@试验
public void isEnvOk(){}
}
更新:它有效您将无法从一个Spring应用程序访问另一个Spring应用程序中定义的bean。这是因为每个Spring应用程序分别管理其bean,并具有独立的ApplicationContext(用于在应用程序中获取bean的接口)。您将无法从一个Spring应用程序访问另一个Spring应用程序中定义的bean。这是因为每个Spring应用程序分别管理其bean,并具有独立的ApplicationContext(用于在应用程序中获取bean的接口)。使用注释:
@SpringBootTest(
classes = {CommonsApplication.class, ModulesApplication.class})
@RunWith(SpringRunner.class)
public class ModulesApplicationTest {
@Autowired
private YmlConfig ymlConfig;
@Test
public void isEnvOk(){}
}
另外,您的YmlConfigTest
应该扩展ModulesApplicationTest
类。使用注释:
@SpringBootTest(
classes = {CommonsApplication.class, ModulesApplication.class})
@RunWith(SpringRunner.class)
public class ModulesApplicationTest {
@Autowired
private YmlConfig ymlConfig;
@Test
public void isEnvOk(){}
}
另外,您的
YmlConfigTest
应该扩展ModulesApplicationTest
类。为了扫描@Configuration
bean,您需要为@SpringBootApplication
指定基本包,然后添加以下行,它就可以工作了
@SpringBootApplication(scanBasePackages = {"com.github.fish56.modules.commons.config",
"com.github.fish56.modules"})
为了扫描
@Configuration
bean,您需要将基本包指定给@SpringBootApplication
,然后添加以下行,它就可以工作了
@SpringBootApplication(scanBasePackages = {"com.github.fish56.modules.commons.config",
"com.github.fish56.modules"})
您可以添加模块应用程序类代码吗?@JonathanJohx是的,我添加了一些附加代码。您可以添加模块应用程序类代码吗?@JonathanJohx是的,我添加了一些附加代码。您好,您是否也为模块应用程序和模块应用程序测试添加了此代码?您好,您是否也为模块应用程序和模块应用程序测试添加了此项?@bitfishxyz您可以在Github上发布您的项目吗?@bitfishxyz如果对您有帮助,您可以接受我的答案。@bitfishxyz您可以在Github上发布您的项目吗?@bitfishxyz如果对您有帮助,您可以接受我的答案。