2013-03-05 57 views
5

我有兩個豆子。兩者都實現郵件功能。一個僅在部署到應用程序服務器時才起作用。另一個用於測試。彈簧與配置文件的默認佈線

我們有每個開發人員和環境的配置文件。我只想在實際測試時連線測試bean。其他bean應該在未測試時使用。我怎樣才能存檔這個?

@Component 
@Profile("localtest") 
public class OfflineMail implements Mailing {} 

解決辦法:

使用「默認」我看這個地方,但似乎沒有落回至「默認」對於喜歡「開發」的輪廓:

@Component 
@Profile("default") 
public class OnlineMail implements Mailing {} 

- >發現接線沒有bean的例外。

離開輪廓了:

@Component 
public class OnlineMail implements Mailing {} 

- >運行 「localtest」 配置文件時拋出一個獨特的異常。

添加所有配置:

@Component 
@Profile("prod") 
@Profile("integration") 
@Profile("test") 
@Profile("dev1") 
@Profile("dev2") 
@Profile("dev3") 
... 
public class OnlineMail implements Mailing {} 

這實際上是工作的,但是我們的開發人員都沒有編號,他們使用「開發<WindowsLogin>」和添加配置文件,可以爲一個豆工作,但一會就搞定當它用於幾個豆時會陷入麻煩,因爲這肯定會變得很難看。

使用像@Profile(「!localtest」)這樣的東西似乎也不起作用。

有誰知道一個更好的方式來獲得「默認情況下,如果沒有找到特定的bean的電線」?

+0

'@ Component'沒有'@ Profile'必須是默認的bean,如果沒有什麼是有線。你有沒有試圖避免與組件名稱設置名稱的唯一例外?我的意思是,'@Component(「mail」)'帶'@Profile(「localtest」)',沒有它? – n1ckolas 2013-03-05 14:04:35

回答

5

我終於找到了一個簡單的解決方案。

在線郵件默認爲有線。

@Component 
public class OnlineMail implements Mailing {} 

使用@Primary註釋離線郵件優先於OnlineMail,避免唯一的例外。

@Component 
@Profile("localtest") 
@Primary 
public class OfflineMail implements Mailing {} 
2

試試這個:

@Component 
@Profile("production") 
public class OnlineMail implements Mailing {} 

@Component 
@Profile("localtest") 
public class OfflineMail implements Mailing {} 

然後使用@ActiveProfiles運行測試( 「localtest」),並使用 「生產」 爲DEFAULT譜運行生產環境。

另外我希望在下一個版本的春天ActiveProfilesResolver會推出SPR-10338 - 它可能對你有幫助(避免「dev1」,「dev2」等)。

+0

我不明白這個解決方案的重點。我們在我們部署應用程序的任何地方都設置了正確的環境。所以Spring不應該加載默認值,應該嗎?而且我不確定如何將測試的默認值分開,因爲生產資源也將位於我們的測試文件中。 – 2013-03-05 14:37:50

+0

有兩種類型的配置文件 - 默認<配置文件和活動配置文件,檢查http://stackoverflow.com/questions/10041410/default-profile-in-spring-3-1 – 2013-03-05 14:50:38

+0

因此,最好避免使用默認(空的)任何已經具有模擬功能的bean的彈簧配置文件 - 使用「生產」和「本地測試」配置文件。並在開始時選擇所需 - 使用@ActiveProfiles進行測試,'pring.profiles.default'或'pring.profiles.active'服務器 – 2013-03-05 14:53:54

0

Spring支持通過@profile注入豆得非常好:

interface Talkative { 
    String talk(); 
} 

@Component 
@Profile("dev") 
class Cat implements Talkative { 
     public String talk() { 
     return "Meow."; 
    } 
} 

@Component 
@Profile("prod") 
class Dog implements Talkative { 
    public String talk() { 
     return "Woof!"; 
    } 
} 

作品在單元測試

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:applicationContex-test.xml"}) 
@ActiveProfiles(value = "dev") 
public class InjectByDevProfileTest 
{ 
    @Autowired 
    Talkative talkative; 

    @Test 
    public void TestTalkative() { 
     String result = talkative.talk(); 
     Assert.assertEquals("Meow.", result); 

    } 
} 

作品在main():

@Component 公衆class Main {

 public static void main(String[] args) { 
      // Enable a "dev" profile 
      System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev"); 
      ApplicationContext context = 
        new ClassPathXmlApplicationContext("applicationContext.xml"); 
      Main p = context.getBean(Main.class); 
      p.start(args); 
     } 

     @Autowired 
     private Talkative talkative; 

     private void start(String[] args) { 
      System.out.println(talkative.talk()); 
     } 
    } 

檢查本作的演示代碼:https://github.com/m2land/InjectByProfile