【问题标题】:Spring Profiles not recognized in a Web ApplicationWeb 应用程序中无法识别 Spring 配置文件
【发布时间】:2014-09-03 18:52:33
【问题描述】:

我正在尝试实现 Spring Profiles 以根据指定的配置文件加载特定的类实现。要使用的配置文件被指定为包含在类路径中的属性文件中的属性 (spring.profiles.active)。

来源:https://github.com/overattribution/spring-profile-test

public interface MyService {
    public void doSomething();
}

@Service
@Profile("preprod")
public class MyServicePreProdImpl implements MyService {
    private final Logger LOG = LoggerFactory.getLogger(MyServicePreProdImpl.class);
    @Override
    public void doSomething() {
        LOG.debug("Doing something in " + MyServicePreProdImpl.class.getName());
    }
}

@Service
@Profile("prod")
public class MyServiceProdImpl implements MyService {
    private final Logger LOG = LoggerFactory.getLogger(MyServiceProdImpl.class);
    @Override
    public void doSomething() {
        LOG.debug("Doing something in " + MyServiceProdImpl.class.getName());
    }
}

但是,我收到以下错误: 没有为依赖找到类型为 [com.example.profileservice.MyService] 的合格 bean。

我错过了什么?

更新 1:

我在 Web 应用程序初始化期间手动设置活动配置文件,但我仍然收到“未找到符合条件的 bean”错误。变化可以看这里:https://github.com/overattribution/spring-profile-test/commit/09175a10b28ea8e5a08b43ad1416431bcf094c9d

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    好的,我让它工作了。在 Web 应用程序初始化期间,需要在根上下文(而不是 servlet 上下文)中设置配置文件。我已经在我的 WebAppInitializer 类中这样做了:

    https://github.com/overattribution/spring-profile-test/blob/f895a8bc67dc1f6ba2fcedb58b73a19cc5cf8cf7/src/main/java/com/example/config/WebAppInitializer.java

    public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        /**
         * Overriding to include setting active profile.
         */
        @Override
        protected WebApplicationContext createRootApplicationContext() {
            Class<?>[] configClasses = getRootConfigClasses();
            if (!ObjectUtils.isEmpty(configClasses)) {
                AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
    
                String[] activeProfiles = getActiveProfiles();
                rootAppContext.getEnvironment().setActiveProfiles(activeProfiles);
    
                rootAppContext.register(configClasses);
                return rootAppContext;
            }
            else {
                return null;
            }
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[]{"/"};
        }
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[] {
                    ApplicationConfig.class
            };
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[] {
                    WebMvcConfig.class
            };
        }
    
        @Override
        protected Filter[] getServletFilters() {
            CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
            characterEncodingFilter.setEncoding("UTF-8");
            characterEncodingFilter.setForceEncoding(true);
            return new Filter[] {characterEncodingFilter};
        }
    
    
        protected String[] getActiveProfiles() {
            PropertySource propertySource = null;
            try {
                propertySource = new ResourcePropertySource("classpath:application.properties");
                String profilesString = (String) propertySource.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME);
                return profilesString.split(",");
            } catch (IOException e) {
                throw new ResourceAccessException("application.properties is not available on the classpath");
            }
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      配置文件是一个命名的逻辑分组,可以通过 ConfigurableEnvironment.setActiveProfiles(java.lang.String...) 以编程方式激活,或者通过设置 spring.profiles.active 属性(通常通过 JVM 系统属性)作为环境以声明方式激活变量,或者对于 web 应用程序作为 web.xml 中的 Servlet 上下文参数。

      我想说的是,除非您使用 Spring Boot 还允许您在 application.properties 文件中设置活动配置文件,否则无法将活动配置文件指定为属性文件中的属性。

      尝试使用上述选项之一。

      【讨论】:

      • 我正在从之前为我提供此功能的 Spring Boot 项目迁移。我想知道他们是怎么做到的?
      • 在 Spring Boot 中,您还可以在 application.properties 中设置活动配置文件。我相应地编辑了答案。
      • 所以在我看来,我需要添加一些从 application.properties 中获取属性的 Java 类,然后使用 ConfigurableEnvironment.setActiveProfiles(java.lang.String...) 设置该值跨度>
      • 如果您不需要将活动配置文件存储在单独的配置文件中,我会说在您的 customizeRegistration 类中取消注释 customizeRegistration 方法并设置相应的配置文件就足够了.
      • 在查看了 WebAppInitializer 父类之后,我决定重写 createServletApplicationContext() 以便我可以在那里注册活动配置文件。但是,它仍然给我“找不到 bean”的错误。我用来源更新了我的原始帖子。
      猜你喜欢
      • 2015-04-03
      • 2021-09-11
      • 1970-01-01
      • 2018-11-07
      • 2020-07-14
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      相关资源
      最近更新 更多