Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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 如何在contextLoadListener上切换取决于spring配置文件_Java_Spring_Spring Profiles - Fatal编程技术网

Java 如何在contextLoadListener上切换取决于spring配置文件

Java 如何在contextLoadListener上切换取决于spring配置文件,java,spring,spring-profiles,Java,Spring,Spring Profiles,我需要在应用程序启动之前执行代码 我编写了以下侦听器(在web.xml中注册): 我希望只有当testprofile被激活时才会调用侦听器。 但它总是调用。如何修复此缺陷?根据文档@Profile注释仅可应用于@组件或@配置。无论如何,您无法在ContextLoaderListener中获取有关配置文件的信息,因为尚未加载ApplicationContext。如果您想在应用程序启动时调用代码,我的建议是创建ApplicationListener并收听ContextRefreshEvent: @C

我需要在应用程序启动之前执行代码

我编写了以下侦听器(在web.xml中注册):

我希望只有当
test
profile被激活时才会调用侦听器。

但它总是调用。如何修复此缺陷?

根据文档
@Profile
注释仅可应用于
@组件
@配置
。无论如何,您无法在ContextLoaderListener中获取有关配置文件的信息,因为尚未加载ApplicationContext。如果您想在应用程序启动时调用代码,我的建议是创建
ApplicationListener
并收听
ContextRefreshEvent

@Component
public class CustomContextListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        Environment environment = contextRefreshedEvent.getApplicationContext().getEnvironment();
        if(environment.acceptsProfiles("test")){
            System.out.print("Test profile is active");
        }
    }
}
要激活所需的配置文件,您必须将其添加到web.xml特殊spring环境属性-
spring.profiles.active

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>default,test</param-value>
</context-param>

注意,您可以添加多个以逗号分隔的配置文件名。

据我所知,spring配置文件仅适用于配置


似乎配置文件注释不适用于您提到的场景。

初始化后,对于me上下文是延迟的。getInitParameter(“spring.profiles.active”)始终返回null。我这样启动应用程序:mvn clean install jetty:run-Dspring.profiles.active=test@gstackoverflow,因为要从jvm选项获取参数,我们必须使用System.getProperty()。代码已更新。
public class ProfileContextListener extends ContextLoaderListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        if(isProfileActive("test", event.getServletContext())){
            System.out.println("Profile is active");
        }
    }

    private boolean isProfileActive(String profileName, ServletContext context) {
        String paramName = "spring.profiles.active";            
        //looking in web.xml
        String paramValue = context.getInitParameter(paramName);
        if(paramValue==null) {
            //if not found looking in -D param
            paramValue = System.getProperty(paramName);
        };
        if(paramValue==null) return false;
        String[] activeProfileArray = paramValue.split(",");
        for(String activeProfileName : activeProfileArray){
            if(activeProfileName.trim().equals(profileName)) return true;
        }
        return false;
    }
}
<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>default,test</param-value>
</context-param>
-Dspring.profiles.active=default,test