Thursday, April 21, 2016

Integrate Spring Socials (Facebook) with Spring MVC XML Based

Leave a Comment

I have a working spring mvc webapp and it is xml based so i have to use the same procedures and not "pure java configs".

I'm trying to integrate facebook sign in to my app and i have tried to follow many tutorials but couldn't manage to make them work.

Here is one of my tries: (https://spring.io/guides/gs/accessing-facebook/)

EDIT:

My XML is now this:

<?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> 

HomeController:

@Controller public class 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"; } 

}

NOW the error is Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'scopedTarget.connectionFactoryLocator' is defined

If I remove th facebook:config tag it gives me the following error because there is no such 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 

Any suggestions?

2 Answers

Answers 1

You need not add spring's facebook interface as class in your xml. Instead create FacebookConnectionFactory using your facebook client id and secret using xml or java code.

@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")));     }      ... } 

or

Using Spring Social Facebook’s XML configuration namespace:

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

Refer : Spring Social Facebook Reference

Answers 2

taken from spring social quickstart example. Probably you cant inject it by yourself, instead you have to use factory-like method:

@Bean @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)    public Facebook facebook() {     return connectionRepository().getPrimaryConnection(Facebook.class).getApi(); } 

it needs other dependencies, no point to copy-paste them all here. Take a look at: https://github.com/spring-projects/spring-social-samples/blob/master/spring-social-quickstart/src/main/java/org/springframework/social/quickstart/config/SocialConfig.java

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment