简体   繁体   English

值无法注入服务 class spring 启动

[英]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.

相关问题 没有在Spring Boot中扩展抽象类的类中注入bean - Not injected bean in class that extends abstract class in Spring Boot 无法让UserDetailsManager注入Spring Boot和基于Java的配置 - Cannot get UserDetailsManager injected with Spring Boot and Java-based Configuration 如何在 Spring 引导中将属性注入到测试 class 中? - How to get properties injected into test class in Spring Boot? 使用带有注入Java和Spring Boot的RestTemplate的类进行单元测试 - Unit test using class with RestTemplate injected with java and spring boot Spring Boot 2.x Jackson ObjectMapper 实例没有被注入到服务类中 - 尝试了引导默认值和来自配置类的 bean 方法 - Spring Boot 2.x Jackson ObjectMapper instance does not get injected in service class - tried both boot default & with bean method from config class Spring boot - 服务类为空 - Spring boot - Service class is null 在春季启动中,为什么我没有将服务类的返回值返回给另一个服务类 - in spring boot, why i did`t get returned value of a service class to another service class 春天注入类 - Spring Injected Class Spring 引导中的服务接口 Class 的用途 - Purpose of Service Interface Class in Spring Boot Spring Boot抽象类bean服务问题 - Spring Boot Abstract Class beans service problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM