6 date: "2016-01-22T16:19:12+00:00"
7 guid: http://juplo.de/?p=579
19 title: 'Develop a Facebook-App with Spring-Social - Part I: Behind the Scenes'
20 url: /develop-a-facebook-app-with-spring-social-part-01-behind-the-scenes/
23 In this series of Mini-How-Tow's I will describe how to develop a facebook app with the help of [Spring-Social](http://projects.spring.io/spring-social/ "Learn more about Spring-Social").
25 In [the last and first part of this series](/develop-a-facebook-app-with-spring-social-part-00/ "Read part 0 of this series, to get prepared!"), I prepared you for our little course.
27 In this part we will take a look behind the scenes and learn more about the autoconfiguration performed by Spring-Boot, which made our first small example so automagically.
29 ## The Source is With You
31 You can find the source-code on [/git/examples/facebook-app/](/git/examples/facebook-app/ "Link for cloning")
32 and [browse it via gitweb](/gitweb/?p=examples/facebook-app;a=summary "Browse the source-code now").
33 Check out `part-01` to get the source for this part of the series.
35 ## Our Silent Servant Behind the Scenes: Spring-Boot
37 While looking at our simple example from the last part of this series, you may have wondered, how all this is wired up.
38 You can log in a user from facebook, access his public profile and all this without one line of configuration.
40 **This is achieved via [Spring-Boot autoconfiguration](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-auto-configuration "Learn more about Spring-Boot's autoconfiguration-mechanism").**
42 What comes in very handy in the beginning, sometimes get's in your way, when your project grows.
43 This may happen, because these parts of the code are not under your control and you do not know what the autoconfiguration is doing on your behalf.
44 Because of that, in this part of our series, we will rebuild the most relevant parts of the configuration by hand.
45 As you will see later, this is not only an exercise, but will lead us to the first improvement of our little example.
47 ## What Is Going On Here?
49 In our case, two Spring-Boot configuration-classes are defining the configuration.
50 These two classes are [SocialWebAutoConfiguration](https://github.com/spring-projects/spring-boot/blob/v1.3.1.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java "View the class on github") and [FacebookAutoConfiguration](https://github.com/spring-projects/spring-boot/blob/v1.3.1.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java "View the class on github").
51 Both classes are located in the package [spring-boot-autoconfigure](https://github.com/spring-projects/spring-boot/tree/v1.3.1.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social "View the package on github").
53 The first one configures the `ConnectController`, sets up an instance of `InMemoryUsersConnectionRepository` as persitent store for user/connection-mappings and sets up a `UserIdService` on our behalf, that always returns the user-id `anonymous`.
55 The second one adds an instance of `FacebookConnectionFactory` to the list of available connection-factories, if the required properties ( `spring.social.facebook.appId` and `spring.social.facebook.appSecret`) are available.
56 It also configures, that a request-scoped bean of the type `Connection<Facebook>` is created for each request, that has a known user, who is connected to the Graph-API.
58 ## Rebuild This Configuration By Hand
60 The following class rebuilds the same configuration explicitly:
65 public class SocialConfig extends SocialConfigurerAdapter
68 * Add a {@link FacebookConnectionFactory} to the configuration.
69 * The factory is configured through the keys <code>facebook.app.id</code>
70 * and <,code>facebook.app.secret</code>.
76 public void addConnectionFactories(
77 ConnectionFactoryConfigurer config,
81 config.addConnectionFactory(
82 new FacebookConnectionFactory(
83 env.getProperty("facebook.app.id"),
84 env.getProperty("facebook.app.secret")
90 * Configure an instance of {@link InMemoryUsersConnection} as persistent
91 * store of user/connection-mappings.
93 * At the moment, no special configuration is needed.
95 * @param connectionFactoryLocator
96 * The {@link ConnectionFactoryLocator} will be injected by Spring.
98 * The configured {@link UsersConnectionRepository}.
101 public UsersConnectionRepository getUsersConnectionRepository(
102 ConnectionFactoryLocator connectionFactoryLocator
105 InMemoryUsersConnectionRepository repository =
106 new InMemoryUsersConnectionRepository(connectionFactoryLocator);
111 * Configure a {@link UserIdSource}, that is equivalent to the one, that is
112 * created by Spring-Boot.
115 * An instance of {@link AnonymousUserIdSource}.
117 * @see {@link AnonymousUserIdSource}
120 public UserIdSource getUserIdSource()
122 return new AnonymousUserIdSource();
126 * Configuration of the controller, that handles the authorization against
127 * the Facebook-API, to connect a user to Facebook.
129 * At the moment, no special configuration is needed.
131 * @param factoryLocator
132 * The {@link ConnectionFactoryLocator} will be injected by Spring.
134 * The {@link ConnectionRepository} will be injected by Spring.
136 * The configured controller.
139 public ConnectController connectController(
140 ConnectionFactoryLocator factoryLocator,
141 ConnectionRepository repository
144 ConnectController controller =
145 new ConnectController(factoryLocator, repository);
150 * Configure a scoped bean named <code>facebook</code>, that enables
151 * access to the Graph-API in the name of the current user.
154 * The {@link ConnectionRepository} will be injected by Spring.
156 * A {@Connection}, that represents the authorization of the
157 * current user against the Graph-API, or null, if the
158 * current user is not connected to the API.
161 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
162 public Facebook facebook(ConnectionRepository repository)
164 Connection connection =
165 repository.findPrimaryConnection(Facebook.class);
166 return connection != null ? connection.getApi() : null;
172 If you run this refined version of our app, you will see, that it behaves in exact the same way, as the initial version.
176 You may ask, why we should rebuild the configuration by hand, if it does the same thing.
177 This is, because the example, so far, would not work as a real app.
178 The first step, to refine it, is to take control of the configuration.
180 In [the next part](develop-a-facebook-app-with-spring-social-part-02-how-spring-social-works "Jump to the third part of this series and read on...") of this series, I will show you, why this is necessary.
181 But, first, we have to take a short look into Spring Social.
183 ## Funded by the Europian Union
185 This article was published in the course of a
186 [resarch-project](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project"),
187 that is funded by the European Union and the federal state Northrhine-Wetphalia.
189 [](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project")