簡體   English   中英

春天,無法使用@Value注釋初始化變量

[英]Unable to initiate a variable using @Value annotation, spring

我的過程課:

@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();
    }

}

我的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>

這是我的屬性文件。

licenseKeyNotFound = License File Corrupted

這是我的servlet xml。

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

盡管我已將@Configurable注釋與屬性Autowire.BY_NAME, Autowire.BY_TYPE一起使用,但無法從屬性文件初始化licenseKeyNotFound變量。
我能夠從控制器初始化變量,但是不能從聲明為@Configurable此類初始化變量。

誰能讓我知道我丟失了什么或者我的代碼有什么問題?
請讓我知道我的代碼中是否需要某些內容。

嘗試這個:

在您的spring xml中:

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

嘗試這個

@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();
   }
}

在您的屬性文件中

licenseKeyNotFound=${value}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM