0

long time user. Hopefully a useful question: I am Autowiring a property in a Component class. I am pulling the value from a property file using PropertySourcesPlaceholderConfigurer in the context xml. and referencing the property as such in class:

 @Value("${my.url}")
 @Autowired
private transient String myURL;

The value is resolved and works; however, I want to display value once at instantiation of Bean. If I log value in method it shows fine. But if in constructor it displays as null. Here's what I coded to that shows as null in log:

 public MyHelper(){
    init();
}

private void init(){
    LOGGER.info("MY_URL...{}", myURL);

}

If I log it method it shows value(partial code):

 public String getReport(final String report) throws ReportOrderingException {
    URL wsdlURL = null;
    try {

        wsdlURL = new URL(myURL);
        LOGGER.info("MY_URL...{}", myURL);
        }

Here's the context.xml

     <bean id="propertyConfigurer"  class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/my.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

Here's the startup log, myURl is vendor.wsdl.url. It creates bean before the property is resolved:

[INFO ] : [localhost-startStop-1] 2017-06-15 10:38:48.562 com.kemper.ros.helper.ExternalServiceHelper : VENDOR_WSDL_URL...null [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.562 org.springframework.beans.factory.annotation.InjectionMetadata : Registered injected element on class [com.kemper.ros.helper.ExternalServiceHelper]: AutowiredFieldElement for private transient java.lang.String com.kemper.ros.helper.ExternalServiceHelper.vendorWsdlURL [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.562 org.springframework.beans.factory.support.DefaultListableBeanFactory : Eagerly caching bean 'externalServiceHelper' to allow for resolving potential circular references [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.562 org.springframework.beans.factory.annotation.InjectionMetadata : Processing injected element of bean 'externalServiceHelper': AutowiredFieldElement for private transient java.lang.String com.kemper.ros.helper.ExternalServiceHelper.vendorWsdlURL [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.jndi.JndiTemplate : Looking up JNDI object with name [java:comp/env/vendor.wsdl.url] [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.jndi.JndiLocatorDelegate : Converted JNDI name [java:comp/env/vendor.wsdl.url] not found - trying original name [vendor.wsdl.url]. javax.naming.NameNotFoundException: Name [vendor.wsdl.url] is not bound in this Context. Unable to find [vendor.wsdl.url]. [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.jndi.JndiTemplate : Looking up JNDI object with name [vendor.wsdl.url] [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.jndi.JndiPropertySource : JNDI lookup for name [vendor.wsdl.url] threw NamingException with message: Name [vendor.wsdl.url] is not bound in this Context. Unable to find [vendor.wsdl.url].. Returning null. [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.core.env.PropertySourcesPropertyResolver : Could not find key 'vendor.wsdl.url' in any property source [DEBUG] : [localhost-startStop-1] 2017-06-15 10:38:48.563 org.springframework.core.env.PropertySourcesPropertyResolver : Found key 'vendor.wsdl.url' in [localProperties] with type [String]

  • 1
    The duplicate addresses `@Autowired` but the behavior is the same for `@Value` (actually get rid of the `@Autowired` entirely). Use constructor injection. – Sotirios Delimanolis Jun 15 '17 at 14:51
  • Sorry for duplicate entry. I must not have searched properly for solution. This article fixed my issue. I created an init() method, removed the Autowired as `@Value` seems to take care of the injection. I added `@PostConstruct` to the init(). Thanks – Stijo03 Jun 15 '17 at 17:21

0 Answers0