Spring Bootのすべての便利さは、いわゆるStarterの使用に基づいています。これにより、設定済みのBeanのセットを取得し、プロパティファイルを使用してすぐに使用でき、設定に使用できます。 しかし、スターターが必要なテクノロジー向けにまだ作成されていない場合はどうでしょうか?
この記事では、Spring-social-vkontakteのスターターの例を使用して、スターターがどのように作成されるかについてお話したいと思います。 Spring Socialは、ソーシャルネットワークとの統合に使用されるSpring Frameworkモジュールの1つです。 Spring Bootプロジェクトには、 Facebook (spring-boot-starter-social-facebook)、Twitter(spring-boot-starter-social-twitter)、LinkedIn(spring-boot-starter-social-twitter)などのソーシャルネットワークのスターターが含まれます。適切なソーシャルモジュールの使用に基づきます。 CISのほとんどの開発者は、サードパーティのモジュールspring-social-vkontakteがあるソーシャルネットワークVkontakteに主に興味を持っています。 したがって、このモジュールのスターターはまだありません。 この記事では、このスターターを作成します。
Spring Bootインフラストラクチャの基礎は、アプリケーションの起動時にSpring Bootが検出し、それを使用してBeanを自動的に作成および構成するAutoConfigurationクラスです。
まず、このようなクラスを作成してみましょう。
@Configuration @ConditionalOnClass({SocialConfigurerAdapter.class, VKontakteConnectionFactory.class}) @ConditionalOnProperty(prefix= "ru.shadam.social-vkontakte", name = { "client-id", "client-secret"}) @AutoConfigureBefore(SocialWebAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) public class VKontakteAutoConfiguration { }
クラスが構成(@Configuration)であることをSpringBootに伝えるために注釈を使用し、ビンを作成するためにAutoConfigurationが使用される条件を指定するために注釈を使用します。
このように:
@ConditionalOnClass({SocialConfigurerAdapter.class, VKontakteConnectionFactory.class})
は、SocialConfigurerAdapter(Spring-Socialモジュールに含まれる)およびVKontakteConnectionFactory(Spring-Social-Vkontakteモジュールに含まれる)がクラスパスにある場合、ビンが作成されることを意味します。 したがって、スターターに必要な依存関係がないと、Beanは作成されません。
@ConditionalOnProperty(prefix= "ru.shadam.social-vkontakte", name = { "client-id", "client-secret"})
は、プロパティru.shadam.social-vkontakte.client-idおよびru.shadam.social-vkontakte.client-secretが存在する場合にのみビンが作成されることを意味します。
@AutoConfigureBefore(SocialWebAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class)
BeanはWebMvcの後、SocialWebの前に初期化されることを意味します。 これは、SocialWebが初期化されるまでに、Beanがすでに登録されているために必要です。
次に、AutoConfigurationでどのBeanを構成するかを見てみましょう。
VKontakteAutoConfiguration.java
@Configuration @ConditionalOnClass({SocialConfigurerAdapter.class, VKontakteConnectionFactory.class}) @ConditionalOnProperty(prefix= "ru.shadam.social-vkontakte", name = { "client-id", "client-secret"}) @AutoConfigureBefore(SocialWebAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class) public class VKontakteAutoConfiguration { @Configuration @EnableSocial @EnableConfigurationProperties(VKontakteProperties.class) @ConditionalOnWebApplication protected static class VKontakteConfigurationAdapter extends SocialConfigurerAdapter { @Autowired private VKontakteProperties properties; @Bean @ConditionalOnMissingBean @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) public VKontakte vkontakte(ConnectionRepository repository) { Connection<VKontakte> connection = repository.findPrimaryConnection(VKontakte.class); if (connection != null) { return connection.getApi(); } return new VKontakteTemplate(this.properties.getClientId(), this.properties.getClientSecret()); } private ConnectionFactory<?> createConnectionFactory() { return new VKontakteConnectionFactory(this.properties.getClientId(), this.properties.getClientSecret()); } @Override public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer, Environment environment) { connectionFactoryConfigurer.addConnectionFactory(createConnectionFactory()); } } }
ConnectionFactoryを登録するために必要なSocialConfigurationAdapterを拡張し ます 。 SocialConfigurerAdapterには、このためのコールバックメソッドがあります。
addConnectionFactories(ConnectionFactoryConfigurer, Environment)
ConnectionFactoryを追加して再定義します。
また、Vkontakte APIにアクセスするためのインターフェースである、要求スコープのビンVkontakteを登録します。 この場合、ユーザーがアプリケーションを介して承認されると、auth_tokenを使用してAPIとの対話が実行されます。
アプリケーションプロパティファイルから構成を取得するために使用されるVkontaktePropertiesクラスも検討してください。
VkontakteProperties.java
@ConfigurationProperties(prefix = "ru.shadam.social-vkontakte") public class VKontakteProperties { private String clientId; private String clientSecret; public String getClientId() { return clientId; } public void setClientId(String clientId) { this.clientId = clientId; } public String getClientSecret() { return clientSecret; } public void setClientSecret(String clientSecret) { this.clientSecret = clientSecret; } }
アノテーションは、プロパティファイルから値を取得します。
@ConfigurationProperties(prefix = "ru.shadam.social-vkontakte")
彼女は、クラスの適切なフィールドに入れるために、接頭辞ru.shadam.social-vkontakteで始まるすべてのプロパティを試す必要があることをSpringBootに伝えます。
最後の手順は、SpringBootがAutoConfigurationクラスを見つけることができるファイルを作成することです。 これを行うには、作成されたjarファイルのMETA-INFフォルダーに配置する必要がある特別なspring.factoriesファイルがあります。
このファイルでは、AutoConfigurationクラスを指定する必要があります。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=ru.shadam.spring.boot.vkontakte.VKontakteAutoConfiguration
作成したjarをSpring Bootプロジェクトに接続し、構成でru.shadam.social-vkontakte.client-idおよびru.shadam.social-vkontakte.client-secretを設定することにより、/ connect / vkontakteおよびVkontaktebin。VkontakteAPIにアクセスするために使用できます。
使用材料: