PDA

View Full Version : Facing a problem converting @Bean to xml configuration for Facebook integration



vijay.olety
Jul 22nd, 2011, 02:22 AM
Hi,

I am using "spring-social-quickstart" code base (Spring 3.1 and Spring Social 1.0.0.RC1) for connecting to Facebook. It works fine. Now i wanted to put all @Configuration and @Bean to xml files. I referred http://static.springsource.org/spring-social/docs/1.0.x/reference/html/connecting.html and have made the changes for the other @Beans. But I am not sure how to convert the following @Bean to xml form -



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


Tried a couple of things but failed. Some exceptions I get are -

1. Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDepen dencyException

2. org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'scopedTarget.Facebook': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request? If you are actually operating within a web request and still receive this message,your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

Please help.

Thanks.

Keith Donald
Jul 22nd, 2011, 07:04 AM
Did you look at spring-social-quickstart-3.0.x which shows the XML alternative. Also the spring-social-extending-existing-api sample also shows this.

vijay.olety
Jul 25th, 2011, 02:18 AM
Thanks Keith for the quick response.

I had a look at the other samples earlier. But after u pointed it out, I had a second look again and here are my observations -

1. spring-social-quickstart-3.0.x
In controllers.xml, we have the following bean definition -


<!-- Controls rendering of the home page once a user has signed in -->
<bean class="org.springframework.social.quickstart.HomeControll er">
<constructor-arg ref="facebook" />
</bean>


And the bean 'facebook' is defined in SocialConfig.java. So it's a mix of xml-java based configuration.

2. spring-social-extending-existing-api
In social.xml, we have the connection repository related configurations. And in TweetController.java, we have -



List<Connection<Twitter>> connections = connectionRepositoryProvider.get().findConnections (Twitter.class);


Here also, we have no 'twitter' bean definition per se in xml configuration.

My question is: Is it possible to have a complete xml-based configuration including bean definition for 'facebook'? Or am I completely off-track in my observations?

Keith Donald
Jul 25th, 2011, 07:15 AM
Yes, it should be possible. I forgot I had updated spring-social-quickstart-3.0.x to be as consistent with the regular quickstart as possible, except 3.0.5 compatible. You're going to have to define a bean that is a request-scoped proxy whose target is resolved by invoking an expression like #{connectionRepository.getConnection(T(Twitter.cla ss)).getApi()}. I'd have to experiment with the syntax to see exactly how this can be done. You can see why we tend to favor strongly-typed Java config :)

Keith

vijay.olety
Jul 25th, 2011, 07:57 AM
Yes. It makes sense to stick to strongly-typed java config. However, in hot-deploy web environments I guess xml-based config has a slight upper hand. I will try out the complete xml-based config too.

Thanks.

harshalv@telenav.com
Feb 7th, 2012, 12:03 PM
Can you post the xml configuration ??

Regards,

vijay.olety
Feb 10th, 2012, 12:28 AM
After discussion with my team members, we went ahead with the strongly-typed java config as Keith had suggested.

habuma
Feb 10th, 2012, 08:28 AM
There are examples of using the XML configuration, such as the spring-social-twitter4j sample (https://github.com/SpringSource/spring-social-samples/tree/master/spring-social-twitter4j), but since it also demonstrates how to use Spring Social with a 3rd party API binding, it doesn't really show a "typical" configuration.

Overall, I strongly recommend the Java-based configuration. It's more strongly typed, is more flexible, and is much easier to configure Spring Social. Spring's XML configuration seems more natural since that's how we've been doing Spring for years...but once you get the feel for the JavaConfig configuration, I'm sure you'll prefer it over XML. Of course, these aren't mutually exclusive options: You can always use XML for some of your app and JavaConfig for other parts.

That said, I tend to agree that configuring Spring Social, whether in XML or JavaConfig, could stand to be easier. That's why I've created https://jira.springsource.org/browse/SOCIAL-292 and https://jira.springsource.org/browse/SOCIAL-293 to improve configuration ease. I've not yet started work on them, but watch those issues to track progress on how this might be made easier for Spring Social 1.1.0.