Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

java - 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...

Also I try to use answer https://stackoverflow.com/questions/45970442/spring-boot-value-returning-null and still doesn't work...

Here is my 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

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

The issue that in my service class the webserviceurl return null. How can I fix this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your controller you are creating your own instance of the service and so Spring is unaware of it and cannot inject the value. 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.

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.

@RestController
@RequestMapping("/api/gift-card")
public class GiftCardController{

    private GiftCardServiceImpl giftCardService;

    @Autowired
    public GiftCardController(GiftCardServiceImpl giftCardService) {
        this.giftCardService = giftCardService;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...