2016-11-04 87 views
0

在我的Spring Boot應用程序中,我使用調用存儲過程的方法實現了以下類。如何將屬性值注入到Spring Boot組件

@Component 
@ConfigurationProperties(prefix = "spring") 
public class FmTrfUtil { 
    static int returnVal; 
    @Value("${spring.datasource.url}") 
    static String url; 

    public static int insertFmTrfs(List<String> trfs, String source) { 
     System.out.println(url); 
     EntityManager em = Persistence.createEntityManagerFactory("RIStore_FM").createEntityManager(); 
     Session session = em.unwrap(Session.class); 
     final String[] trfArray = trfs.toArray(new String[trfs.size()]); 
     final String src = source; 

     session.doWork(new Work(){ 
      public void execute(Connection conn) throws SQLException { 
       CallableStatement stmt = null;    
       OracleConnection oraCon = conn.unwrap(OracleConnection.class); 
       Array array = oraCon.createARRAY("VARCHAR2_TAB_T", trfArray); 
       stmt = conn.prepareCall("{? = call FM_TRF_UTIL.process_fm_trf(?,?)}"); 
       stmt.registerOutParameter(1, Types.INTEGER); 
       stmt.setArray(2, array); 
       stmt.setString(3, src); 
       stmt.execute(); 
       returnVal = stmt.getInt(1); 
      } 
     }); 
     return returnVal; 
    } 
} 

由於調用存儲過程需要數據庫連接,我需要從application.properties加載這些相應的屬性值:

spring.profiles.active=dev 
spring.datasource.url=jdbc:oracle:thin:@ldap://xxx:389/risdev3, cn=OracleContext,dc=x,dc=net 
spring.datasource.username=owner 
spring.datasource.password=owner987 

基於對類似的問題,Spring boot - custom variables in Application.propertiesUsing Spring-Boot configuration properties in your own classesSpring Boot @ConfigurationProperties example以下文章,我爲我的類@ConfigurationProperties(prefix = "spring")(數據庫連接屬性都以「spring」作爲前綴)添加了此註釋。但是,當我用如下的測試類運行它時,出現錯誤「應用程序必須提供JDBC連接」,這意味着application.properties中的屬性未被拾取。

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = RistoreWebApplication.class, initializers = ConfigFileApplicationContextInitializer.class) 
public class FmTrfUtilTest { 
    @Test 
    public void test() { 
     List<String> trfs = new ArrayList<String>(); 
     trfs.add("TRF000001"); 
     trfs.add("TRF000002"); 
     int ret = FmTrfUtil.insertFmTrfs(trfs, "SARC"); 
     assertTrue(ret > 0); 
    } 
} 

爲了@ConfigurationProperties工作,我加了Maven的依賴spring-boot-configuration-processor了。爲什麼它還沒有工作?我錯過了什麼?

回答

1

有幾件事錯在這裏:

  • @Value不會對靜態字段
  • @ConfigurationProperties工作,用於從application.propertiesapplication.yml到Java對象綁定字段。查看Spring Boot本身的任何@ConfigurationProperties註釋類,以便輕鬆理解它應該如何使用。
  • 你不應該使用自己的@ConfigurationProperties前綴spring因爲它已經由春節引導本身
  • spring-boot-configuration-processor僅用於在IDE中更好的代碼完成。你不需要這個。

如果你想利用春節引導配置屬性的數據庫連接,而不是創建EntityManager像你這樣的:

EntityManager em = Persistence.createEntityManagerFactory("RIStore_FM").createEntityManager(); 

你應該只把它注射假設你有春天數據JPA啓動您的依賴列表。

我看你用了很多static的方法和字段。這不適用於Spring。改爲使用依賴注入並自動裝載你需要的東西。

+0

我應該如何注入EM?我嘗試在方法的上方注入'EntityManager'' @PersistenceContext(unitName =「RIStore_FM」)''static EntityManager em;',但'Persistence annotations不支持靜態字段'錯誤。如果我取出'static',它將不會編譯,說明方法中的em必須聲明爲靜態。 – ddd

+0

方法和字段都不應該是靜態的。查看Spring Data示例項目和參考。如果你以Spring方式使用JPA,使用Spring非常簡單。 –

+0

它現在的作品,謝謝。 – ddd

相關問題