Spring MVC - Einstellen des aktiven Profils

Spring MVC - So legen Sie ein aktives Profil fest

In diesem Beispiel zeigen wir Ihnen, wie Sie aktive@Profile in einer Spring MVC-Webanwendung festlegen.

@Profile Beispiele

@Configuration
public class AppConfig {

    @Profile("dev")
    @Bean
    public CacheManager cacheManager() {
        //...
    }

    @Profile("live")
    @Bean
    public EhCacheManagerFactoryBean ehCacheCacheManager() {
        //...
    }

    @Profile("testdb")
    @Bean
    public DataSource dataSource() {
        //...
    }

}

Um aktive@Profile im Frühjahr festzulegen, definieren Sie einen Wert über die Systemeigenschaftspring.profiles.active.

1. web.xml

Für normale Webanwendungen, dieweb.xml Dateien enthalten.

1.1 Set an active profile.

web.xml

    
        spring.profiles.active
        live
    

1.2 Set multiple active profile.

web.xml

    
        spring.profiles.active
        dev, testdb
    

2. Servlet 3.0+ Container

Verwenden Sie eine der folgenden Methoden, damit Webanwendungen keineweb.xml-Datei haben:

2.1 Override onStartup

MyWebInitializer.java

package com.example.servlet3;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class MyWebInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

    //...

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

        //Set multiple active profile
        //servletContext.setInitParameter("spring.profiles.active", "dev, testdb");
    }

}

2.2 Depends which context (root or servlet) to load the @Profile beans.

MyWebInitializer.java

package com.example.servlet3;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyWebInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

    //If the @Profile beans are loaded via root context
    @Override
    protected WebApplicationContext createRootApplicationContext() {

        WebApplicationContext context =
                     (WebApplicationContext)super.createRootApplicationContext();
            ((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("live");

        //Set multiple active profiles
        //((ConfigurableEnvironment)context.getEnvironment())
                //          .setActiveProfiles(new String[]{"live", "testdb"});

            return context;

    }

    //If the @Profile beans are loaded via servlet context
    /*
    @Override
    protected WebApplicationContext createServletApplicationContext() {

        WebApplicationContext context =
                     (WebApplicationContext)super.createServletApplicationContext();
            ((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("dev");
            return context;

    }*/

}