当前位置: 首页>后端>正文

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第1张

鍦ㄥ疄闄呴」鐩腑鎴戜滑闇€瑕佹墜鍔ㄦ潵鎵嬪啓Starter缁勪欢鐨勫満鏅浉瀵规潵璇磋繕鏄瘮杈冨皯鐨勶紝浣嗘槸瀵逛簬鑷畾涔塖tarter缁勪欢鐨勫師鐞嗗ぇ瀹惰繕鏄渶瑕佹帉鎻℃竻妤氾紝绗竴涓槸澧炲姞鑷繁鐨勮嚜淇″績锛岀浜屼釜鏄帉鎻$殑鑷姩瑁呴厤鐨勫師鐞嗗悗Starter缁勪欢鐨勫疄鐜板叾瀹炲氨鏄潪甯哥畝鍗曠殑浜嬫儏浜嗐€傛垜浠€氳繃Redis鐨勫鎴风绋嬪簭鏉ョ粰澶у浠嬬粛銆傚浜嶳edis鏈嶅姟鎴戜滑鍙互浣跨敤鐨凧ava瀹㈡埛绔繕鏄瘮杈冨鐨勶紝浣嗘槸鍦⊿pringBoot椤圭洰涓垜浠€庝箞鏉ヤ娇鐢ㄥ憿锛?/p>

鎵嬪啓SpringBoot Starter缁勪欢

1.鑷畾涔塖tarter

鍒涘缓涓€涓櫘閫氱殑Maven椤圭洰锛屽悕绉颁负redisson-spring-boot-starter,鍛藉悕瑕佹敞鎰忥紝绗笁鏂箂tarter绾﹀畾鏄?xxxx+spring-boot-starter.

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第2张

娣诲姞鐩稿叧鐨勪緷璧?/p>

    <dependencies>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.15.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.4.8</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

娣诲姞灞炴€х被

/**
 * 瀹氫箟灞炴€х被
 *    鎸囧畾鍓嶇紑涓?bobo.redisson
 */
@ConfigurationProperties(prefix = "bobo.redisson")
public class RedissonProperties {

    private String host = "localhost";
    private int port = 6379;
    private int timeout = 0;
    private boolean ssl = false;

    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;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public boolean isSsl() {
        return ssl;
    }

    public void setSsl(boolean ssl) {
        this.ssl = ssl;
    }
}

鐒跺悗鍒涘缓瀵瑰簲鐨勯厤缃被銆?/p>

@ConditionalOnClass(Redisson.class) // 鏉′欢瑁呴厤
// 鍏宠仈 灞炴€ч厤缃被
@EnableConfigurationProperties(RedissonProperties.class)
@Configuration
public class RedissonAutoConfiguration {

    @Bean
    public RedissonClient redissonClient(RedissonProperties redissonProperties){
        Config config = new Config();
        String prefix = "redis://";
        if(redissonProperties.isSsl()){
            prefix = "rediss://";
        }
        // 鍗曡妭鐐硅繛鎺ラ厤缃?
        config.useSingleServer()
                .setAddress(prefix+redissonProperties.getHost()+":"+redissonProperties.getPort())
                .setConnectTimeout(redissonProperties.getTimeout());
        return Redisson.create(config);
    }

}

杩欐牱鎴戜滑鐨勮嚜瀹氫箟Starter鐨勫姛鑳藉氨宸笉澶氫簡锛岀劧鍚庢槸瑕佸垱寤簊pring.factories鏂囦欢銆傛潵瀹炵幇鍜孲pringBoot椤圭洰鐨勮嚜鍔ㄨ閰嶃€?/p>

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第3张

鏈€鍚庝负浜嗗鍔犲湪娣诲姞灞炴€ф枃浠剁殑鏃跺€欐湁鎻愮ず淇℃伅銆傛垜浠彲浠ュ厛瀵煎叆渚濊禆

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.4.8</version>
        </dependency>

鐒跺悗娣诲姞json鏂囦欢锛屽悕绉板繀椤讳负additional-spring-configuration-metadata.json

{
  "properties": [
    {
    "name": "bobo.redisson.host",
      "type": "java.lang.String",
      "description": "Redis鐨勬湇鍔″櫒鍦板潃",
      "defaultValue": "localhost"
  },{
      "name": "bobo.redisson.port",
      "type": "java.lang.Integer",
      "description": "Redis鐨勬湇鍔″櫒绔彛",
      "defaultValue": 6379
    }
  ]
} 

鍒拌繖鍎垮氨鍙互浜唅nstall浜嗭紝鐒跺悗灏卞彲浠ヤ娇鐢ㄤ簡銆?/p>

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第4张

2.SpringBoot椤圭洰浣跨敤

鑷畾涔塖tarter椤圭洰鍒涘缓瀹屾垚鍚庯紝鎴戜滑灏卞彲浠ュ湪SpringBoot椤圭洰涓潵浣跨敤浜嗐€傚厛瀵煎叆瀵瑰簲鐨勪緷璧栥€?/p>

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第5张

鐒跺悗鎴戜滑鍦ㄥ睘鎬ф枃浠朵腑閰嶇疆Redis鐨勮繛鎺ヤ俊鎭紝棣栧厛鍙互鐪嬪埌鎻愮ず淇℃伅浜?/p>

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第6张

瀹屾暣鐨勯厤缃俊鎭?/p>

# 娣诲姞MyBatis 鐨勭浉鍏抽厤缃俊鎭?
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisdb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

# 杩炴帴姹犵殑閰嶇疆
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

## 鎸囧畾鏄犲皠鏂囦欢鐨勪綅缃?
mybatis.mapper-locations=classpath:mapper/*.xml

## 鑷畾涔塖tart 鐨勫睘鎬ч厤缃?Redis 閰嶇疆
bobo.redisson.host=192.168.100.120
bobo.redisson.port=6379
bobo.redisson.timeout=5000

鐒跺悗鍒涘缓鎺у埗鍣ㄦ祴璇曘€?/p>

@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/hello")
    public List<User> hello(){
        return userService.query();
    }

    @GetMapping("/query")
    public String query(){
        RBucket<Object> name = redissonClient.getBucket("name");
        if(name.get() == null){
            name.set("BOBO");
        }
        return name.get().toString();
    }

}

鍚姩鏈嶅姟娴嬭瘯锛?/p>

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第7张
掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第8张

鐪嬪畬涓変欢浜嬧潳锔?/h2>

濡傛灉浣犺寰楄繖绡囧唴瀹瑰浣犺繕铔湁甯姪锛屾垜鎯抽個璇蜂綘甯垜涓変釜灏忓繖锛?/p>

  1. 鍏虫敞鍏紬鍙?銆?java鐑傜尓鐨?/strong> 銆忥紝涓嶅畾鏈熷垎浜師鍒涚煡璇嗐€?/p>

  2. 鍚屾椂鍙互鏈熷緟鍚庣画鏂囩珷ing馃殌

  3. .鍏虫敞鍚庡洖澶嶃€?66銆戞壂鐮佸嵆鍙幏鍙栧涔犺祫鏂欏寘

掌握了SpringBoot的自动装配原理后你会发现自定义Starter也是非常容易的哦!,第9张


https://www.xamrdz.com/backend/3b21934972.html

相关文章: