找不到类型为 [org.springframework.social.twitter.api.Twitter] 的合格 bean 以依赖
No qualifying bean of type [org.springframework.social.twitter.api.Twitter] found for dependency
在我的 Spring 引导应用程序中,我正在尝试实现 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";
}
}
申请class:
@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
为什么这对应用程序有效而测试失败?我的测试配置有什么问题?
原因是您没有定义 spring.social.twitter.appId
和 spring.social.twitter.appSecret
.
的 src/main/resources/application.properties 或 config/application.properties
在我的 Spring 引导应用程序中,我正在尝试实现 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";
}
}
申请class:
@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
为什么这对应用程序有效而测试失败?我的测试配置有什么问题?
原因是您没有定义 spring.social.twitter.appId
和 spring.social.twitter.appSecret
.