2015-09-05 76 views
0

我想讀取Spring Roo項目中的簡單屬性文件。看起來很簡單,但經過半天以上的掙扎,我需要尋求幫助。Spring Roo&讀取屬性文件

我在互聯網上看過很多Q & A,但我無法讓它工作。 我看着Properties with SpringSpring @PropertySource example。 我是Spring的新手,請耐心等待。

我的Spring版本是3.2.8

問題: 我怎樣才能檢索到的BaseController類test.name鍵值。

我創建了一個非常簡單的Spring Roo應用程序來說明我做了什麼。

我的袋鼠腳本:

// Create a new project 
project --topLevelPackage com.springsource.pizzashop --projectName pizzashop 

// Setup JPA persistence using EclipseLink and H2 
jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY 

// Create domain entities 
entity jpa --class ~.domain.Base 
field string --fieldName name --sizeMin 2 --notNull 

// Adding web layers 
web mvc setup 
web mvc all --package ~.web 

默認情況下,在我的 「的applicationContext.xml」:

<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/> 

我沒有改變這個文件。所以我沒有添加一個bean。

我在..resources \ META-INF \ spring \文件夾中添加了test.properties文件,位於那裏的database.properties旁邊。

test.name=Google 
test.url=www.google.com 

我加了一個Java bean在... \ java中的\ com \ SpringSource的\ pizzashop \域\ PizzaProperties.java配置文件,旁邊base.java類文件。

package com.springsource.pizzashop.domain; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.roo.addon.javabean.RooJavaBean; 

import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 

@RooJavaBean 
@PropertySource("classpath:META-INF/spring/test.properties") 
public class PizzaProperties { 

    @Value("${test.name}") 
    private String name; 

    @Value("${test.url}") 
    private String url; 

} 

Ofcourse「PizzaProperties_Roo_JavaBean.aj」也創建有getter和setter。

找到屬性文件,因爲當我將名稱更改爲test1.properties時,出現錯誤。

我推入Base.list方法來測試我是否能夠讀取屬性文件。在這個方法中,我做了一個System.out.println。 結果總是:

在Base.list
名稱= NULL

package com.springsource.pizzashop.web; 
import com.springsource.pizzashop.domain.Base; 
import com.springsource.pizzashop.domain.PizzaProperties; 

import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 

@RequestMapping("/bases") 
@Controller 
@RooWebScaffold(path = "bases", formBackingObject = Base.class) 
public class BaseController { 

    @RequestMapping(produces = "text/html") 
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) { 

     System.out.println("In Base.list"); 
     PizzaProperties config = new PizzaProperties(); 
     System.out.println("Name = " + config.getName()); 

     if (page != null || size != null) { 
      int sizeNo = size == null ? 10 : size.intValue(); 
      final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo; 
      uiModel.addAttribute("bases", Base.findBaseEntries(firstResult, sizeNo, sortFieldName, sortOrder)); 
      float nrOfPages = (float) Base.countBases()/sizeNo; 
      uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages)); 
     } else { 
      uiModel.addAttribute("bases", Base.findAllBases(sortFieldName, sortOrder)); 
     } 
     return "bases/list"; 
    } 
} 

回答

3

爲了讓Spring來注入性質必須是Spring如何創建bean。

試試這個:

添加到PizzaProperties@Bean註釋(@RooJavaBean剛剛產生干將制定者

... 
... 

@RooJavaBean 
@Component 
@PropertySource("classpath:META-INF/spring/test.properties") 
public class PizzaProperties { 

... 
... 

添加到BaseController一個@Autowired屬性PizzaProperties

... 
... 

@RequestMapping("/bases") 
@Controller 
@RooWebScaffold(path = "bases", formBackingObject = Base.class) 
public class BaseController { 

    @Autowired 
    private PizzaProperties config; 

    @RequestMapping(produces = "text/html") 
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) { 


     System.out.println("In Base.list"); 
     System.out.println("Name = " + config.getName()); 

... 
... 

請注意,所有這些只是Spring配置,而不是特別的Spring Roo的東西。

祝你好運!

+1

謝謝!!!!,要試試這個。 順便說一句,我真的很喜歡Spring Roo/gvNIX! – dhmc

+0

謝謝!很高興看到這個:-) – jmvivo

0

jmvivo肯定讓我走上正軌。

稍微更新了一下,我最終設法以一種非常簡單,乾淨的方式讀取了Spring Roo項目中的屬性文件。

1)在屬性類中只有@RooJavaBean和@Component就夠了!不需要@PropertySource,也不需要添加@Bean。 可能是因爲<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>沒有必要使用@PropertySource註釋。

2)jmvivo指示我不得不

@Autowired private PizzaProperties config;

添加到我想要的使用屬性的類。

所以最終它非常乾淨和簡單。我喜歡保持它KIS。

package com.springsource.pizzashop.domain; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.roo.addon.javabean.RooJavaBean; 
import org.springframework.stereotype.Component; 

@RooJavaBean 
@Component 
//@PropertySource("classpath:META-INF/spring/test.properties") 
public class PizzaProperties { 

    @Value("${test.name}") 
    private String name; 

    @Value("${test.url}") 
    private String url; 

} 

@RequestMapping("/bases") 
@Controller 
@RooWebScaffold(path = "bases", formBackingObject = Base.class) 
public class BaseController { 

    @Autowired 
    private PizzaProperties config; 

    @RequestMapping(produces = "text/html") 
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) { 

     System.out.println("In Base.list"); 
     System.out.println("Name = " + config.getName()); 

     ... 
     ... 

再次感謝您的幫助!