4

我想知道如何初始化Spring Bean中的字段?下面是幾個可能的解決方案:在Spring Beans中初始化字段的正確方法是什麼?

1.初始化場直接申報

import org.springframework.stereotype.Component; 

@Component 
public class DeclarationInit { 

    private final int field = Integer.MAX_VALUE; 

    public int getField() { 
     return field; 
    } 
} 

2.初始化領域使用@Value註釋

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

@Component 
public class ValueInit { 

    @Value("#{T(Integer).MAX_VALUE}") 
    private int field; 

    public int getField() { 
     return field; 
    } 
} 

3.使用@Autowired註釋初始化場

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

@Component 
public class AutowiredInit { 

    private int field; 

    @Autowired 
    private void initField() { 
     field = Integer.MAX_VALUE; 
    } 

    public int getField() { 
     return field; 
    } 
} 

使用@PostConstruct註釋

import javax.annotation.PostConstruct; 
import org.springframework.stereotype.Component; 

@Component 
public class PostConstructInit { 

    private int field; 

    @PostConstruct 
    private void initField() { 
     field = Integer.MAX_VALUE; 
    } 

    public int getField() { 
     return field; 
    } 
} 

所有測試成功,並且沒有顯示出任何區別4.初始化字段:

import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.Matchers.equalTo; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.SpringApplicationConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = SomeTestContextConfiguration.class) 
public class FieldInitTest { 

    @Autowired 
    private DeclarationInit declarationInit; 

    @Autowired 
    private ValueInit valueInit; 

    @Autowired 
    private AutowiredInit autowiredInit; 

    @Autowired 
    private PostConstructInit postConstructInit; 

    @Test 
    public void shouldInitializeFieldOnDeclaration() { 
     assertThat(declarationInit.getField(), equalTo(Integer.MAX_VALUE)); 
    } 

    @Test 
    public void shouldInitializeFieldWithValueAnnotation() { 
     assertThat(valueInit.getField(), equalTo(Integer.MAX_VALUE)); 
    } 

    @Test 
    public void shouldInitializeFieldWithAutowiredSetter() { 
     assertThat(autowiredInit.getField(), equalTo(Integer.MAX_VALUE)); 
    } 

    @Test 
    public void shouldInitializeFieldWithPostConstruct() { 
     assertThat(postConstructInit.getField(), equalTo(Integer.MAX_VALUE)); 
    } 
} 

此聲明彼此相等,或者我應該使用只有他們中的一個或兩個都沒有?

回答

6

假設該值是一個常數,第一個選項是最簡單的理解和沒有春天工作,簡化單元測試。

第二個和第四個選項比較複雜,並且在Spring容器上引入了一個不必要的依賴,沒有任何好處。第三個選項是徹頭徹尾的怪異,因爲你使用@Autowired而不執行依賴注入。

2

我相信spring會提供所有這些選項,因爲你可能遇到不同的需求...... 1.如果你想要MAX_INT並且地球上沒有任何需要以不同的方式初始化它,那麼聲明「int field = Integer.MAX_INT」不管彈簧的

  • 如果要允許其它的初始配置,則可以使用它@Autowired,或通過構造ARG初始化,或setter /吸氣劑...這是一個品味的問題

  • @PostConstruct更適合複雜的情況,例如如果你的領域需要根據其他注入領域進行計算。

  • 相關問題