简体   繁体   中英

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?

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;
    }

@Value annotation uses for injecting values into fields in Spring-managed beans. In your example, you create GiftCardServiceImpl on your own and Spring cannot control the creation and inject the webserviceurl value from application.properties . You can change GiftCardController to allow Spring to do this.

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

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. Spring doesn't catch that should inject property in the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM