Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringApplicationConfiguration中声明配置类的顺序。弹簧靴_Java_Spring_Spring Boot_Spring Test - Fatal编程技术网

Java 在SpringApplicationConfiguration中声明配置类的顺序。弹簧靴

Java 在SpringApplicationConfiguration中声明配置类的顺序。弹簧靴,java,spring,spring-boot,spring-test,Java,Spring,Spring Boot,Spring Test,我有这样的春季开机测试: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = { PropertyConfig.class, ServiceConfigA.class, ServiceConfigB.class} ) public class SpringTest { @Test public void test() { ...

我有这样的春季开机测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {
    PropertyConfig.class,
    ServiceConfigA.class,
    ServiceConfigB.class}
)
public class SpringTest {

    @Test
    public void test() {
    ...
    }
}
当类列表中第一个声明
PropertyConfig
类时,我得到了上下文初始化错误,因为
PropertyConfig
中的bean被忽略,来自服务配置的bean无法自动关联某些字段。当我在一些serivce配置之后移动
PropertyConfig
时,就会初始化
PropertyConfig
中的bean

更详细地说,
PropertyConfig
包含两个bean:
PropertiesFactoryBean
propertysourcesplaceconfigurer
propertysourcesplaceplaceconfigurer
的原因不存在服务配置中的bean无法使用
@Value
注释自动连接字段(无法从字符串自动转换为整数)

我的问题是为什么
PropertyConfig
中的bean在第一种情况下没有初始化?
在Spring引导测试中是否有一些配置加载的特性

Spring扫描注册到CI容器中的依赖项,并确定它们的初始化顺序。您所指的订单根本不相关

如果你出故障了,你的线路肯定还有其他问题。但是您没有向我们展示您的代码,根本原因很难说。

请参阅Javadoc的“BeanFactoryPostProcessor returning@Bean methods”部分,了解
@Bean

必须特别考虑返回的@Bean方法 SpringBeanFactoryPostProcessor(BFPP)类型。因为BFPP对象 必须在容器生命周期的早期实例化,它们可以 干扰诸如@Autowired、@Value、, 和@Configuration类中的@PostConstruct。为了避免这些 生命周期问题,将返回@Bean方法的BFPP标记为静态。对于 例如:

 @Bean
 public static PropertyPlaceholderConfigurer ppc() {
     // instantiate, configure and return ppc ...
 }
通过将此方法标记为static,可以调用它,而不会导致其声明的@Configuration类实例化,因此 避免上述生命周期冲突。然而,请注意 静态@Bean方法将不会针对作用域和AOP进行增强 语义如上所述。这适用于BFPP案例,因为 通常不被其他@Bean方法引用。作为提醒,一个 将为任何非静态@Bean方法发出警告级别日志消息 具有可分配给BeanFactoryPostProcessor的返回类型


换句话说,确保您的
PropertiesFactoryBean
PropertySourcesPlaceholderConfigurer
Bean声明为
static
,并且它应该可以工作。

我的提示是否让您的
PropertySourcesPlaceholderConfigurer
beans
static
为您工作?