Springbootは、構成ファイルの値を取得する2つの方法値アノテーションとConfigurationPropertiesアノテーションの使用

まず、値の注釈にコントローラーコードを使用します

package com.quan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.service.HuaweiService;
import com.quan.service.HuaweiService2;

/**
 * @author yangquan
 */

@RestController
@RequestMapping("/controller")
public class HuaweiController {

    @Autowired
    private HuaweiService huaweiService;
    @Autowired
    private HuaweiService2 huaweiService2;

    @RequestMapping("/test")
    public void test(){
        huaweiService.test();
    }

    @RequestMapping("/test2")
    public void test2(){
        huaweiService2.test();
    }
}

サービスコード

package com.quan.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import com.quan.service.HuaweiService;

// @ConfigurationProperties(
//     prefix = "huawei"
// )
@Service
public class HuaweiServiceImpl implements HuaweiService {

    /**用户在华为开发者联盟申请Push服务获取的服务参数*/
    // private static  String appId = "12345678";
    @Value("${huawei.app-id}")
    private String appId;
    /**用户在华为开发者联盟申请Push服务获取的服务参数*/
    // private static String appSecret = "appSecret";
    @Value("${huawei.app-secret}")
    private String appSecret;

    @Override
    public void test() {
        System.out.println(appId+"  "+appSecret);
    }
}

次に、ConfigurationPropertiesアノテーションのサービスコードの使用

package com.quan.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import com.quan.service.HuaweiService;
import com.quan.service.HuaweiService2;

import lombok.Data;

/**
 * @author yangquan
 */
@Data
// @Configuration
@ConfigurationProperties(prefix="hw")
@Service
public class HuaweiServiceImpl2 implements HuaweiService2 {

    /**用户在华为开发者联盟申请Push服务获取的服务参数*/
    // private static  String appId = "12345678";
    private String appId;
    /**用户在华为开发者联盟申请Push服务获取的服务参数*/
    // private static String appSecret = "appSecret";
    private String appSecret;


    @Override
    public void test() {
        System.out.println(appId+"  "+appSecret);
    }
}

yml構成ファイル

server:
  port: 8802

huawei:
  app-id: 123
  app-secret: 123
hw:
  app-id: 234
  app-secret: 234

リクエストパス

http://localhost:8802/controller/test2
http://localhost:8802/controller/test2

実行結果インターネット上で、ConfigurationPropertiesのアノテーションを使用するには、次の依存関係を追加する必要あるということわざがあり
ここに画像の説明を挿入します
ます。

 <!-- 支持 @ConfigurationProperties 注解 -->
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>-->

ここには追加していませんが、構成ファイルで値を取得できます。誰もが自分で決めることができます

テクニカルグループチャットへようこそ
ここに画像の説明を挿入します

この人生では、永続性または非永続性はひどいものではありません。恐れているのは、永続性の道を一人で歩くことです!

おすすめ

転載: blog.csdn.net/taiguolaotu/article/details/113824608