Primero, use el código del controlador para la anotación de valor
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();
}
}
código de servicio
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);
}
}
En segundo lugar, el uso del código de servicio para las anotaciones de 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);
}
}
archivo de configuración yml
server:
port: 8802
huawei:
app-id: 123
app-secret: 123
hw:
app-id: 234
app-secret: 234
Ruta de solicitud
http://localhost:8802/controller/test2
http://localhost:8802/controller/test2
Resultados en ejecución Existe
un dicho en Internet que dice que el uso de la anotación de ConfigurationProperties debe agregar las siguientes dependencias
<!-- 支持 @ConfigurationProperties 注解 -->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>-->
No lo agregué aquí, pero puedo obtener el valor en el archivo de configuración. Todos pueden decidir por sí mismos
Bienvenido a unirse al chat del grupo técnico
En esta vida, la perseverancia o la no perseverancia no es terrible, ¡lo que se teme es caminar solo por el camino de la perseverancia! ! !