2016-07-12 14 views
2

Satz aktives Profil wie context.getEnvironment().setActiveProfiles("DEV"); setzen, dieWie aktive Profile in Spring Annotation basiert Java Config

unter Verwendung erreicht werden kann
public class SpringWebInitializer implements WebApplicationInitializer 
{ 

    public void onStartup(final ServletContext servletContext) throws ServletException 
    { 
     AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
     context.getEnvironment().setActiveProfiles("DEV") 

    } 
} 

Aber beim Ausfahren AbstractAnnotationConfigDispatcherServletInitializer. Wie können wir das aktive Profil einstellen?

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer 
{ 
    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] { WebConfig.class }; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[] { "/" }; 
    } 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return null; 
    } 

} 

Antwort

0

Sie können @ActiveProfiles("DEV") auf einige Ihrer @Configuration Klassen verwenden, aber wahrscheinlich nützliches Profil von außen würde vorbei - nur Ihre .jar mit zusätzlichen Parametern laufen wie -Dspring.active.profiles=DEV

+0

danke für die Antwort @ActiveProfiles ("DEV") ist in Testfällen.Es gibt noch einen anderen besseren Weg die Java-Konfiguration. –

0

Sie haben ein paar Optionen ..

  1. Sie können versuchen, einen Kontext initializer mit dem Federprofil zu laden, von einer Eigenschaften auf der classpath-Datei, wie folgt aus:

    public class ContextProfileInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { 
    
        private static final Logger LOGGER = LoggerFactory.getLogger(ContextProfileInitializer.class); 
    
        private static final String DEFAULT_SPRING_PROFILE = "local"; 
    
        @Override 
        public void initialize(final ConfigurableApplicationContext applicationContext) { 
    
         ConfigurableEnvironment environment = applicationContext.getEnvironment(); 
         try { 
           environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:conf/application.properties")); 
           if (environment.getProperty("spring.profiles.active") == null) { 
            environment.setActiveProfiles(DEFAULT_SPRING_PROFILE); 
           } 
           LOGGER.info("Activated Spring Profile: " + environment.getProperty("spring.profiles.active")); 
          } catch (IOException e) { 
           LOGGER.error("Could not find properties file in classpath."); 
          } 
        } 
    
    } 
    

Hier sind einige Führer mit mehr Info: (! Und eine viel einfachere Art und Weise)

https://gist.github.com/rponte/3989915

http://www.java-allandsundry.com/2014/09/spring-webapplicationinitializer-and.html

  1. Alternativ Verwenden Sie Spring Boot.

    Sie können einfach spring.profiles.active in einer application.properties Datei im Klassenpfad definieren. Dies wird automatisch aufgenommen und in Ihre Umgebung geladen.

Mehr Infos hier:

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

3

Aktivieren Sie Ihr Profil von spring.profiles.active Eigenschaft.

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] { WebConfig.class }; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[] { "/" }; 
    } 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return null; 
    } 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     super.onStartup(servletContext); 
     servletContext.setInitParameter("spring.profiles.active", "DEV"); 
    } 

} 
Verwandte Themen