Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

spring - @Value not set in one specific class

I'm fairly sure I'm being some kind of idiot, but for the life of me I can't see it.

I have a large Spring Boot 2.1 application that extensively uses injection of properties through the @Value annotation. This works great, has done for years. But there's one specific, brand-new object where I can't get the values set. They are always null.

I know the problem isn't with the values themselves, because some of the same values inject just fine into other objects. But I just can't see what's wrong with THIS object, and would be grateful for your eyeballs.

The values in this object (which is in the same directory and builds just fine) are always null:

@Service
public class SSOUtil {
    private String domain = "https://login.microsoftonline.com/";
    private String tenantId = "[deleted guid]";
    public static String localEnvironment = "local";
    public static String devEnvironment = "dev";
    public static String testEnvironment = "test";
    public static String prodEnvironment = "prod";

    @Value("${actions.PROD.touchnet_azure_ad_client_secret}")
    private String clientSecretTouchnetProd;
    @Value("${actions.TEST.touchnet_azure_ad_client_secret}")
    private String clientSecretTouchnetTest;
    @Value("${actions.DEV.touchnet_azure_ad_client_secret}")
    private String clientSecretTouchnetDev;

    @Value("${actions.touchnet_azure_ad_client_id_dev}")
    private String clientIdDev;
    @Value("${actions.touchnet_azure_ad_client_id_test}")
    private String clientIdTest;
    @Value("${actions.touchnet_azure_ad_client_id_prod}")
    private String clientIdProd;

    @Value("${touchnet.redirectURLDev}")
    private String redirectURLDev;
    @Value("${touchnet.redirectURLTest}")
    private String redirectURLTest;
    @Value("${touchnet.redirectURLProd}")
    private String redirectURLProd;

    private String clientId;
    private String clientSecret;
    private String redirectURI;

    public SSOUtil() {
        this.redirectURI = redirectURLTest;
        this.clientSecret = clientSecretTouchnetTest;
    }

    public String getADLoginURL() {
        String returnURL = "";
        System.out.println(clientIdTest); // always prints null
    }
}

The values in this object work just fine, though, and note that one of them is the same @Value as in the other class:

@Service
public class LibraryHelpServiceBean implements LibraryHelpService {
    private CourseServiceBean courseServiceBean;
    private final RestTemplate restTemplate;

    @Value("${actions.libraryhelp_lti_api_key}")
    private String apikey;

    @Value("${actions.touchnet_azure_ad_client_id_test}")
    String clientIdTest;

    public LibraryHelpServiceBean(CourseServiceBean courseServiceBean, RestTemplateBuilder restTemplateBuilder) {
        this.courseServiceBean = courseServiceBean;
        this.restTemplate = restTemplateBuilder.build();
    }

    public void doesValueWork() {
        this.apikey = this.apikey;
        System.out.println(this.clientIdTest); // always prints correct value, a guid
    }
 }

Both objects are initialized in a similar way: either directly or indirectly through the @Autowired annotation in other objects that I use (and which work fine, and have worked fine for ages). Here's the creation of SSOUtil (my problem class):

@RestController
@RequestMapping(value = "/web")
public class SSOLandingController {

    @Autowired
    private SSOUtil ssoUtil;

 [rest of class omitted]
}

And here's the creation of LibraryHelpServiceBean, which is working fine and has all @Values populate correctly:

@Service
public class LibraryHelpStreamServiceBean implements LibraryHelpStreamService {

    private LibraryHelpServiceBean libraryHelpServiceBean;
    
    public LibraryHelpStreamServiceBean(LibraryHelpServiceBean libraryHelpServiceBean){
         this.libraryHelpServiceBean = libraryHelpServiceBean;
    }
}

I have already tried changing the class annotation for SSOUtil from @Service to @Component (and @Configuration, just for the heck of it).

What could be causing the @Values in SSOUtil to come back null even though some of those same @Values populate just fine in other classes?

I'm convinced that I'm missing something obvious. I'm hoping it's something small like a typo. I'm nervous that it's something big, like I've completely misunderstood how Spring IOC works for the past several years.

Thanks for your help.

question from:https://stackoverflow.com/questions/65837267/value-not-set-in-one-specific-class

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

1 Answer

0 votes
by (71.8m points)

I tested your case on my computer, but I'm not able to reproduce your problem. When things like this is happening, try something very simple like this

package no.mycompany.springbootapp;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

 @Component
 public class SSOUtil2 {
    @Value("${actions.touchnet_azure_ad_client_id_test}")
    private String clientIdTest;
 }

Inject this component into your controller, set a breakpoint inside your controller method and inspect the injected instance.

My experience is that some unexplainable cases I've been involved in here on SO, were solved by cleaning the build or wiping the .m2-folder.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...