2016-01-20 98 views
0

我對SO非常陌生,我正在開發根據環境(dev,prod,test)加載環境配置和屬性的任務,我已使用<beans profile="profile.name">成功實現了DAO級別的數據庫配置。在前端我必須根據環境獲取屬性文件,所以我有不同的文件。叫我曾嘗試下面的代碼:@ wicket項目中的@component spring註釋?

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


@Component 
public class PropertiesUtility { 
    @Value("${mount.images.webpath}") 
    private String imagePath; 

    public String getImagePath() { 
     return imagePath; 
    } 

    public void setImagePath(String imagePath) { 
     this.imagePath = imagePath; 
    } 

    @Override 
    public String toString() { 
     return "PropertiesUtility{" + 
       "imagePath='" + imagePath + '\'' + 
       '}'; 
    } 
} 

我context.xml的配置:

<context:annotation-config/> 
    <beans profile="dev"> 
      <context:property-placeholder location="classpath:properties/pharmisa_web_conf.properties" 
              ignore-unresolvable="true" /> 

     </beans> 
     <beans profile="test"> 
      <context:property-placeholder location="classpath:properties/pharmisa_web_test_conf.properties" 
      ignore-unresolvable="true" /> 

     </beans> 

調用PropertiesUtility:

public class URLUtility { 
    @SpringBean //even @Autowired also not working 
    static PropertiesUtility propertiesUtility; 
    public static String getCompanyLogoUrl(int id) { 
     StringBuffer sb = new StringBuffer(getImagePath()); 
     boolean isEndWithSlash=PharmisaStringUtils.endsWith(getImagePath(),"/"); 
     if (!isEndWithSlash){ 
      sb.append("/"); 
     } 
     sb.append(id); 
     sb.append("/"); 
     return sb.toString(); 
    } 

    private static final String getImagePath() { 

     return propertiesUtility.getImagePath().trim(); 
    } 

} 

SpringJunitTest

測試工作完美

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:spring/pharmisa_web-context.xml"}) 
@ActiveProfiles(profiles = "test") 
public class CompanyServiceImplTest { 

    @Autowired 
    PropertiesUtility propertiUtility; 

    @Test 
    public void testAppProperties() { 
     System.out.println(propertiUtility.getImagePath()); 
    } 

} 

,當我試圖注入PropertiesUtility類在檢票頁。我是沒有得到物業的價值。因爲它沒有被注入。我知道在門票@SpringBean,但即使它不起作用。

是否有無價值的任何替代歡迎。

爲您進一步的我也跟着鏈接

http://examples.javacodegeeks.com/enterprise-java/spring/load-environment-configurations-and-properties-with-spring-example/

+0

如何在Wicket中注入PropertiesUtility?您錯過了向我們展示最重要的部分。 –

+0

除了Martin-g提出的問題之外,我還建議運行一些Spring Test(例如使用TESTNG)和AutoWire組件,以查看您是否實際從Spring組件本身獲取沒有UI的文件路徑。 – Mihir

+0

@ martin-g我已經嘗試過使用Autowired和SpringBean,但也不能正常工作 –

回答

0

URLUtility應該是一個Spring bean,如果你想@Component@Autowire工作。

@SrpingBean僅在Wicket組件中自動工作。在任何其他你需要「請求注射」明確Injector.get().inject(this)