First, use the controller code for the value annotation
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();
}
}
service code
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);
}
}
Second, the use of service code for ConfigurationProperties annotations
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 configuration file
server:
port: 8802
huawei:
app-id: 123
app-secret: 123
hw:
app-id: 234
app-secret: 234
Request path
http://localhost:8802/controller/test2
http://localhost:8802/controller/test2
Running results There is
a saying on the Internet that using the annotation of ConfigurationProperties needs to add the following dependencies
<!-- 支持 @ConfigurationProperties 注解 -->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>-->
I haven't added it here, but I can get the value in the configuration file. Everyone can decide for themselves
Welcome to join the technical group chat
In this life, persistence or non-persistence is not terrible, what is afraid is to walk alone on the road of persistence! ! !