SpringBootカスタムスタータースターター

実際のプロジェクトの開発では、私たちはしばしば、スターター、スターターの様々なを使用して、これらのいくつかのような、springbootの公式といくつかの基本的な機能を統合されています:springboot-スターター、フレームとの一部は、第三者があります:springbootのような、カスタマイズされた統合を迅速かつ効率的な開発のための私達に提供MyBatisの -spring-ブートスターター

効率的な開発だけでなく、展開などプロジェクトの要件のために、毎日の開発では、私たちができるspringbootプライベートカスタマイズすることが、自分のスターターを生成する必要が、実際のプロジェクトに再導入し、開発の効率を向上させます。

例えば:私たちは、JavaがspringbootコントロールのjedisにRedisの使用すると、必要Jedisのインスタンスを作成し、そのように適切なパラメータ渡す  IP、ポートなど、使用後springboot、スターター、Jedis使用IP、ポートをカスタマイズすることができ、インスタンス化ランチャー製の容器豆注入SpringBoot、によって管理されます。

スターターは、開発の効率を向上させることができるように、プロジェクトのRedisの機能を必要として導入しました。

定期的なスターターは、独立したプロジェクトで、その後、他の開発者があなたのスターターを使用できることに登録Mavenの中に新倉庫を公開します。

Redisのためのカスタマイザースターターの実現。

まず、新しいMavenプロジェクトを作成し、ばねブートスタータのRedis(スターター)

次の依存関係を導入します:

                <dependency>
			<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>1.5.7.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!--自动配置依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>

第二に、このプロジェクトの書き込みRedisProperties.classは、application.propertiesからのRedisの構成情報を読み取るために

package com.example.demo.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @ClassName JedisProperties * @Description TODO * @Author wushaopei * @Date 2020/2/2 18:30 * @Version 1.0 */ @Component @ConfigurationProperties(prefix="jedis") public class RedisProperties { private String host; private int port; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } } 

第三に、RedisAutoConfiguration.classは、BeanコンテナにロードされJedis春にこのプロジェクトで製造しました

package com.example.demo.autoConfigure;

import com.example.demo.properties.RedisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import javax.annotation.PostConstruct; /** * @ClassName JedisAutoConfigure * @Description TODO * @Author wushaopei * @Date 2020/2/2 18:32 * @Version 1.0 */ @Configuration @ConditionalOnClass({ Jedis.class}) @EnableConfigurationProperties({RedisProperties.class}) @AutoConfigureAfter({RedisProperties.class}) public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean public Jedis Jedis(RedisProperties redisProperties) { //spring会自动将RedisProperties这个bean注入进来, return new Jedis(redisProperties.getHost(),redisProperties.getPort(),10000); } }

我々は、これらのアノテーションの意味を説明したよう

リソースディレクトリの新application.propertiesで、このプロジェクトファイルに第四に、

redis.host=127.0.0.1
redis.port=6379

五、豆注入法:

最初:自動注入、依存関係の設定spring.factoriesを作成します

リソースディレクトリMETA-INFディレクトリに新しい、およびディレクトリ内spring.factoriesファイルを作成し、以下のデータファイルを追加します。

org.springframework.boot.autoconfigure.EnableAutoConfiguration =com.example.demo.autoConfigure.RedisAutoConfiguration

ローディングのための現在のデータは、現在のスターターが管理Beanコンテナを生成し、そして特定のプロジェクトに導入されたときに容器の呼び出しでアイテムを走査されます。

第二:手動注入は、作成EnableRedisクラスを

よると@importは、セクションのメモを手動でプロジェクトのスタートアップクラスに追加の役割を設定します

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(RedisAutoConfiguration.class)
public @interface EnableRedis { }

使用します。

@SpringBootApplication
@EnableRedis//关键的一步
public class BlogApplication { ...... }

第六に、新しいブログプロジェクト、依存春・ブート・スターターのRedisの導入

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

ブログでの7つのプロジェクトは、クラスBlogApplicationを書き始めます

package com.imooc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.ConfigurableApplicationContext;
import redis.clients.jedis.Jedis; /** * @ClassName BlogApplication * @Description TODO * @Author wushaopei * @Date 2020/2/2 23:15 * @Version 1.0 */ @SpringBootApplication public class BlogApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args); Jedis jedis = context.getBean(Jedis.class); System.out.println(jedis); //如果成功连接上了redis,jedis.ping()会返回一个pong System.out.println(jedis.ping()); } } 

八、Redisのを開始

Redisのは、仮想マシンまたはクラウドサーバを起動します

[root@wsp /]# redis-server /etc/redis.conf 
[root@wsp /]# redis-cli 

まず起動春ブーツ・スターターは、Redisのスターターをした後、特定のプロジェクト開始春ブート・スターター・アプリケーションを


おすすめ

転載: www.cnblogs.com/wushaopei/p/12283626.html