Freakyuser
Freakyuser

Reputation: 2814

Unable to initiate a variable using @Value annotation, spring

My process class:

@Configurable("checkLicense")
public class CheckLicense {
String licensePath ;

@Value("${licenseKeyNotFound}")
String licenseKeyNotFound;

    public boolean checkIn(String licensepath) {
        System.out.println("hello "+licenseKeyNotFound);
        licensePath = licensepath;
        return checkIn();
    }

}

My ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean class="com.smart.applicationlicense.CheckLicense"
        scope="prototype">
    </bean>
</beans>

Here is my properties file.

licenseKeyNotFound = License File Corrupted

Here is my servlet xml.

<context:property-placeholder location="conf/LicenseSettings.properties"
    order="2" ignore-unresolvable="true" />

Eventhough I have used the @Configurable annotation along with the attributes Autowire.BY_NAME, Autowire.BY_TYPE, I am unable to initiate the variable of licenseKeyNotFound from the property file.
I was able to initiate the variable from a controller but not from this class which is declared @Configurable.

Can anyone please let me know what I am missing or what's wrong with my code?
Please let me know if there is something required from my code.

Upvotes: 1

Views: 901

Answers (2)

Festus Tamakloe
Festus Tamakloe

Reputation: 11310

Try this

@Configurable
public class CheckLicense {
   String licensePath;
   String licenseKeyNotFound;

   @Value("${licenseKeyNotFound}")   
   public void setLicenseKeyNotFound(String licenseKeyNotFound) {
      this.licenseKeyNotFound = licenseKeyNotFound;
   }

   public boolean checkIn(String licensepath) {
      System.out.println("hello " + licenseKeyNotFound);
      licensePath = licensepath;
      return checkIn();
   }
}

in your properties file

licenseKeyNotFound=${value}

Upvotes: 0

Kent
Kent

Reputation: 195219

try this:

in your spring xml:

<context:property-placeholder location="classpath:your.properties" />
<context:load-time-weaver />

Upvotes: 1

Related Questions