我有两个弹簧引导项目A和B.项目A是内置到jar中的公共库;项目B是打包成战争的Web服务。项目B在pom.xml中依赖于A.项目A的application.yml没有加载到项目B中,它依赖于A
在项目答:
@SpringBootApplication
@PropertySource("classpath:application.yml")
@ComponentScan({ "com.a" })
public class AppA {
}
@Configuration
@ConfigurationProperties(prefix="a")
@Data
public class AConfig {
private String attr1; //in application.yml under project A's classpath
}
在项目中的下列测试通过:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppA.class)
public class SDTTokenServiceTest {
@Autowired
private AConfig aConfig;
@Test
public void testAConfig() {
Assert.assertNotNull(aConfig.getAtrr1());
}
}
在项目B:
@SpringBootApplication
@Import(AppA.class)
@PropertySource("classpath:application.yml")
@ComponentScan({ "com.b" })
public class AppB {
}
在项目B,下面的测试失败原因到null属性。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppB.class)
public class SDTTokenServiceTest {
@Autowired
private AConfig aConfig;
@Test
public void testAConfig() {
Assert.assertNotNull(aConfig.getAtrr1());
}
}
为什么项目B无法获得A中的属性?我怎样才能解决这个问题?谢谢!
更新: 两个YAML文件是他们的类路径下,也许这造成冲突时,项目B.后,我在项目中的YAML文件移动到不同的路径classpath:config/application.yml
进口项目A,那么它工作正常。
有没有人知道在这种情况下yaml文件是如何加载的?当他们都在自己的类路径下时,为什么它不工作?谢谢!
它在一个JAR/WAR中吗? – luboskrnac
@luboskrnac项目A是一个内置于一个罐子里的通用库;项目B是打包成战争的Web服务。 – snowery