[英]Value cannot injected into service class spring boot
I already try to search through stackoverflow, and I don't think I find the solution I want...我已经尝试通过stackoverflow搜索,但我认为我找不到我想要的解决方案......
Also I try to use answer https://stackoverflow.com/questions/45970442/spring-boot-value-returning-null
and still doesn't work...另外我尝试使用答案
https://stackoverflow.com/questions/45970442/spring-boot-value-returning-null
仍然无法正常工作...
Here is my controller class这是我的 controller class
package com.vincent.springoauth.controller;
import com.vincent.springoauth.model.GiftCardRequest;
import com.vincent.springoauth.model.GiftCardResponse;
import com.vincent.springoauth.service.InCommGiftCardServiceImpl;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/gift-card")
public class GiftCardController{
@PostMapping("/activate")
public @ResponseBody
GiftCardResponse activate(GiftCardRequest request) {
GiftCardServiceImpl giftCardService = new GiftCardServiceImpl("");
return giftCardService.activate(request);
}
}
And here is my service class这是我的服务 class
package com.vincent.springoauth.service;
import com.vincent.springoauth.model.GiftCardRequest;
import com.vincent.springoauth.model.GiftCardResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@Log4j2
public class GiftCardServiceImpl {
private final String baseEndpoint;
public GiftCardServiceImpl( @Value("${webserviceurl}")String baseEndpoint){
this.baseEndpoint = baseEndpoint;
}
public String accessToken() {
log.info("Access oauth token url address: " + baseEndpoint);
// will be use that base endpoint to manipulate stuff later
return "abcdefg";
}
public GiftCardResponse activate(GiftCardRequest request) {
log.info("Calling token ...");
accessToken();
log.info("Incomm Pre Auth Service");
// Generate preAuth request;
//RetailTransactionGenericRequestWrapper retailTransactionGenericRequest = buildRequest(request);
//log.info("RetailTransactionGenericRequest: " + retailTransactionGenericRequest);
GiftCardResponse response = GiftCardResponse.builder().responseCode("0").responseMessage("Success").build();
return response;
}
}
And in my application.properties
I have following line webserviceurl=https://localhost/giftcard
在我的
application.properties
我有以下行webserviceurl=https://localhost/giftcard
The issue that in my service class the webserviceurl
return null
.在我的服务 class 中,
webserviceurl
返回null
的问题。 How can I fix this?我怎样才能解决这个问题?
In your controller you are creating your own instance of the service and so Spring is unaware of it and cannot inject the value.在您的 controller 中,您正在创建自己的服务实例,因此 Spring 不知道它并且无法注入该值。 Your service class is annotated as a Service so Spring will create an instance, which will have the value injected but that is not the instance that your controller is using.
您的服务 class 被注释为服务,因此 Spring 将创建一个实例,该实例将注入值,但这不是您的 controller 正在使用的实例。
Instead you should declare that service as an instance variable in your controller and use either Autowire annotation on that instance variable or use constructor autowiring to ensure that the bean created by Spring is the one that is used by your controller.相反,您应该将该服务声明为 controller 中的实例变量,并在该实例变量上使用 Autowire 注释或使用构造函数自动装配来确保 Spring 创建的 bean 是 Z594C103F2C6E04C3D8AZA 使用的那个。
@RestController
@RequestMapping("/api/gift-card")
public class GiftCardController{
private GiftCardServiceImpl giftCardService;
@Autowired
public GiftCardController(GiftCardServiceImpl giftCardService) {
this.giftCardService = giftCardService;
}
@Value
annotation uses for injecting values into fields in Spring-managed beans. @Value
注释用于将值注入 Spring 管理的 bean 中的字段。 In your example, you create GiftCardServiceImpl
on your own and Spring cannot control the creation and inject the webserviceurl
value from application.properties
.在您的示例中,您自己创建
GiftCardServiceImpl
并且 Spring 无法控制创建并从application.properties
注入webserviceurl
值。 You can change GiftCardController
to allow Spring to do this.您可以更改
GiftCardController
以允许 Spring 执行此操作。
package com.vincent.springoauth.controller;
import com.vincent.springoauth.model.GiftCardRequest;
import com.vincent.springoauth.model.GiftCardResponse;
import com.vincent.springoauth.service.InCommGiftCardServiceImpl;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/gift-card")
public class GiftCardController{
private final GiftCardServiceImpl giftCardService;
public GiftCardController(GiftCardServiceImpl giftCardService) {
this.giftCardService = giftCardService;
}
@PostMapping("/activate")
public @ResponseBody
GiftCardResponse activate(GiftCardRequest request) {
return giftCardService.activate(request);
}
}
Try to inject GiftCardServiceImpl
via Spring DI like in the example below尝试通过 Spring DI 注入
GiftCardServiceImpl
,如下例所示
package com.vincent.springoauth.controller;
import com.vincent.springoauth.model.GiftCardRequest;
import com.vincent.springoauth.model.GiftCardResponse;
import com.vincent.springoauth.service.InCommGiftCardServiceImpl;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/gift-card")
public class GiftCardController{
@Autowired
private GiftCardServiceImpl giftCardService;
@PostMapping("/activate")
public @ResponseBody
GiftCardResponse activate(GiftCardRequest request) {
return giftCardService.activate(request);
}
}
The @Value
annotation only works for Spring Beans, when you create the class via simple new
keyword.当您通过简单的
new
关键字创建@Value
时,@Value 注释仅适用于 Spring Bean。 Spring doesn't catch that should inject property in the constructor. Spring 没有发现应该在构造函数中注入属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.