简体   繁体   English

Spring的JavaConfig和CustomScopeConfigurer问题

[英]Spring's JavaConfig and CustomScopeConfigurer issue

I'm seeing some odd behavior, I was hoping someone here can shine some light on the issue. 我看到一些奇怪的行为,我希望这里有人可以对这个问题有所了解。

Let me start by describing my setup. 让我先描述一下我的设置。 First, a simple data object 首先,一个简单的数据对象

public class Apple {
    private String name;
    public Apple withName(String name) {
        this.name = name;
        return this;
    }
    public String getName() {
        return name;
    }
}

And a test class.. 和一个测试类..

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={TestConfig.class})
public class AppleTest {
    @Autowired private Apple apples;

    @Test
    public void simpleTest() {
        System.out.println("OBJ: "+apples);
    }
}

The config is as follows 配置如下

@Configuration
public interface ConfigInterface {
    public Apple getApple();
}

With an implementing class 有一个实施类

@Configuration
@Import(AbstractTestConfig.class)
public class TestConfig implements ConfigInterface {
    public Apple getApple() {
        return new Apple().withName("Granny apples");
    }
}

With the config dependency... 使用配置依赖...

@Configuration
public class AbstractTestConfig {
    @Autowired ConfigInterface conf;

    @Bean Apple myTestApple() {
        return conf.getApple();
    }
}

All of this works great. 所有这一切都很棒。 I run the test, I see the output I expect. 我运行测试,我看到了我期望的输出。 But then I throw a spanner into the wheel and modify AbstractTestConfig to look as follows. 但后来我将一个扳手扔到方向盘上并修改AbstractTestConfig,如下所示。

@Configuration
public class AbstractTestConfig {
    @Autowired ConfigInterface conf;

    @Bean Apple myTestApple() {
        return conf.getApple();
    }

    // NEW CODE
    @Bean CustomScopeConfigurer scopeConfigurer() {
        return new CustomScopeConfigurer();
    }
}

And all of a sudden the @Autowired object conf is null when it is required to construct the Apple bean. 当需要构造Apple bean时,@ @Autowired对象conf突然变为null。

Even more odd, if I move the CustomScopeConfigurer bean to the TestConfig class, then it works. 更奇怪的是,如果我将CustomScopeConfigurer bean移动到TestConfig类,那么它可以工作。

Is there something I don't know about scopes or the CustomScopeConfigurer object in particular? 是否有我不了解的范围或者特别是CustomScopeConfigurer对象?

Copied from Spring @Bean javadoc : 从Spring @Bean javadoc复制:

BeanFactoryPostProcessor-returning @Bean methods BeanFactoryPostProcessor-返回@Bean方法

Special consideration must be taken for @Bean methods that return Spring BeanFactoryPostProcessor (BFPP) types. 必须特别考虑返回Spring BeanFactoryPostProcessor(BFPP)类型的@Bean方法。 Because BFPP objects must be instantiated very early in the container lifecycle, they can interfere with processing of annotations such as @Autowired, @Value, and @PostConstruct within @Configuration classes. 由于BFPP对象必须在容器生命周期的早期实例化,因此它们可能会干扰@Configuration类中@Autowired,@ Value和@PostConstruct等注释的处理。 To avoid these lifecycle issues, mark BFPP-returning @Bean methods as static. 要避免这些生命周期问题,请将BFPP返回@Bean方法标记为静态。 For example: 例如:

@Bean
 public static PropertyPlaceholderConfigurer ppc() {
     // instantiate, configure and return ppc ...
 }

By marking this method as static, it can be invoked without causing instantiation of its declaring @Configuration class, thus avoiding the above-mentioned lifecycle conflicts. 通过将此方法标记为静态,可以调用它而不会导致其声明@Configuration类的实例化,从而避免上述生命周期冲突。 Note however that static @Bean methods will not be enhanced for scoping and AOP semantics as mentioned above. 但请注意,如上所述,静态@Bean方法不会针对作用域和AOP语义进行增强。 This works out in BFPP cases, as they are not typically referenced by other @Bean methods. 这在BFPP案例中有效,因为它们通常不被其他@Bean方法引用。 As a reminder, a WARN-level log message will be issued for any non-static @Bean methods having a return type assignable to BeanFactoryPostProcessor. 作为提醒,将为任何具有可分配给BeanFactoryPostProcessor的返回类型的非静态@Bean方法发出WARN级别的日志消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM