请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

单独运行Weather这个项目报找不到bean的错误

第一步:在application.properties里面加上这三个属性,
weather.type=rain
weather.rate=serious
weather.enable=enable

第二步,测试类里面如下测试

@SpringBootTest(classes = WeatherApplication.class)
class WeatherApplicationTests implements ApplicationContextAware {
	private ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	@Test
	void contextLoads() {
		System.out.println(applicationContext.getBean("weatherSource"));

	}

预期的话,我认为是能注入这个bean实例的,这里为什么会报错呢。麻烦老师帮忙解释下

正在回答

1回答

小伙伴你好,需要提供具体报错信息哦。

1 回复 有任何疑惑可以回复我~
  • 提问者 慕哥6062902 #1
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'weatherSource' available
    	org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
    	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
    	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
    	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    
    	
    这个weather项目是打成Jar包给其它服务去使用的,这点没有疑问,我主要想问,为什么单独跑这个weather项目,会起不来呢,application.properties里面已经配置了那三个必要属性
    回复 有任何疑惑可以回复我~ 2020-07-15 19:49:00
  • 源码师 回复 提问者 慕哥6062902 #2
    测试类上没有启动配置信息,找不到bean
    回复 有任何疑惑可以回复我~ 2020-07-16 18:56:37
  • 提问者 慕哥6062902 回复 源码师 #3
    @Test
    	void contextLoads() {
    		WeatherService weatherService = applicationContext.getBean("weatherService", WeatherService.class);
    		System.out.println(weatherService);
    
    		WeatherSource weather = applicationContext.getBean("weather-com.rongsm.weather.WeatherSource", WeatherSource.class);
    		System.out.println(weather);
    	}
    感谢老师的回答,让我有些思路,真正的原因不在没有启动配置,我去打断点看了下,WeatherSource这个类在容器中的名字是weather-com.rongsm.weather.WeatherSource,不是我一直以为的weatherSource
    回复 有任何疑惑可以回复我~ 2020-07-16 20:12:34
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号