gpt4 book ai didi

java - 将 Spring Socials (Facebook) 与基于 XML 的 Spring MVC 集成

转载 作者:行者123 更新时间:2023-11-30 03:03:55 25 4
gpt4 key购买 nike

我有一个正在运行的 spring mvc webapp,它是基于 xml 的,所以我必须使用相同的过程,而不是“纯 java 配置”。

我正在尝试将 Facebook 登录集成到我的应用程序中,并且我尝试遵循许多教程,但无法使它们正常工作。

这是我的尝试之一:( https://spring.io/guides/gs/accessing-facebook/ )

编辑:

我的 XML 现在是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:facebook="http://www.springframework.org/schema/social/facebook"
xmlns:twitter="http://www.springframework.org/schema/social/twitter"
xmlns:social="http://www.springframework.org/schema/social"
xmlns:linkedin="http://www.springframework.org/schema/social/linkedin"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/social/facebook http://www.springframework.org/schema/social/spring-social-facebook.xsd
http://www.springframework.org/schema/social/linkedin http://www.springframework.org/schema/social/spring-social-linkedin.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/social/twitter http://www.springframework.org/schema/social/spring-social-twitter.xsd
http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

<bean id="connectionFactoryLocator"
class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
<property name="connectionFactories">
<list>

<bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
<constructor-arg value="${facebook.clientId}" />
<constructor-arg value="${facebook.clientSecret}" />
</bean>
</list>
</property>
</bean>

<bean id="usersConnectionRepository"
class="org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository">
<constructor-arg ref="dataSource" />
<constructor-arg ref="connectionFactoryLocator" />
<constructor-arg ref="textEncryptor" />
</bean>

<bean id="connectionRepository" factory-method="createConnectionRepository"
factory-bean="usersConnectionRepository" scope="request">
<constructor-arg value="#{request.userPrincipal.name}" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>

<bean class="org.springframework.social.connect.web.ConnectController">
<!-- relies on by-type autowiring for the constructor-args -->
</bean>

<bean class="org.springframework.social.connect.web.ConnectController">
<!-- relies on by-type autowiring for the constructor-args -->
<property name="applicationUrl" value="${application.url}" />
</bean>



<facebook:config app-id="962223610477458" app-secret="b7dfec28b08ac4e8c2a09cbac4662c15" app-namespace="setelog_selectandwin" />

</beans>

家庭 Controller :

@ Controller 公共(public)类 HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

private Facebook facebook;
private ConnectionRepository connectionRepository;

@Inject
public HomeController(Facebook facebook, ConnectionRepository connectionRepository) {
this.facebook = facebook;
this.connectionRepository = connectionRepository;
}

/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
return "redirect:/connect/facebook";
}

model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
PagedList<Post> feed = facebook.feedOperations().getFeed();
model.addAttribute("feed", feed);
return "facebook/hello";
}

}

现在错误是导致:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“scopedTarget.connectionFactoryLocator”的bean

如果我删除 facebook:config 标签,则会出现以下错误,因为没有这样的 bean:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.social.facebook.api.Facebook] found for dependency: expected at least 1 bean which

有什么建议吗?

最佳答案

你不需要在你的xml中添加spring的facebook接口(interface)作为类。相反,使用 xml 或 java 代码使用您的 facebook 客户端 ID 和 key 创建 FacebookConnectionFactory。

@Configuration
public class SocialConfig implements SocialConfigurer {

@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
cfConfig.addConnectionFactory(new FacebookConnectionFactory(
env.getProperty("facebook.clientId"),
env.getProperty("facebook.clientSecret")));
}

...
}

使用 Spring Social Facebook 的 XML 配置命名空间:

<facebook:config app-id="${facebook.clientId}"
app-secret="${facebook.clientSecret}"
app-namespace="socialshowcase" />

引用:Spring Social Facebook Reference

关于java - 将 Spring Socials (Facebook) 与基于 XML 的 Spring MVC 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35274042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com