找不到依赖项类型为[org.springframework.social.twitter.api.Twitter]的合格Bean
问题内容:
在我的Spring Boot应用程序中,我正在尝试实现Twitter
oAuth支持。按照以下示例http://spring.io/guides/gs/accessing-
twitter/
我已经成功将Twitter访问权限添加到了我的应用程序中,所以现在我有了:
家用控制器:
@Controller
@RequestMapping("/")
public class HelloController {
private Twitter twitter;
private ConnectionRepository connectionRepository;
@Inject
public HelloController(Twitter twitter, ConnectionRepository connectionRepository) {
this.twitter = twitter;
this.connectionRepository = connectionRepository;
}
@RequestMapping(method=RequestMethod.GET)
public String helloTwitter(Model model) {
if (connectionRepository.findPrimaryConnection(Twitter.class) == null) {
return "redirect:/connect/twitter";
}
TwitterProfile userProfile = twitter.userOperations().getUserProfile();
model.addAttribute(userProfile);
CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
model.addAttribute("friends", friends);
return "hello";
}
}
应用类别:
@PropertySources({ @PropertySource(value = "classpath:application.properties") })
@ComponentScan("com.example")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
到目前为止,一切正常。我可以启动应用程序而没有任何问题。
另外,我有很多集成测试,例如:
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest("server.port: 0")
public class SearchControllerTest {
@Value("${local.server.port}")
protected int port;
@Test
public void testSearchResultsWithDefaultPageAndSize() {
// @formatter:off
given()
.when()
.get(format("http://localhost:%d/api/v1.0/search/%s", port, "RDBMa mosyl"))
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("totalCount", equalTo(4))
.body("entries.size", equalTo(4));
// @formatter:on
}
....
}
现在,在添加了Twitter社交功能之后,我的所有测试均失败,但出现以下异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.social.twitter.api.Twitter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 43 common frames omitted
为什么这对Application起作用,而对测试却失败?我的测试配置有什么问题?
问题答案:
原因是您没有定义spring.social.twitter.appId
和的src / main / resources /
application.properties或config / application.properties
spring.social.twitter.appSecret
。