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更适合复杂的情况,例如如果你的领域需要根据其他注入领域进行计算。

  • 相关问题