Spring-profile settings

Source: Internet
Author: User

The development environment and production environment are usually different database connection methods, the development environment can be intrusive, and the production environment using JNDI connection pool, so according to different environment configuration different bean,spring provide the profile to achieve dynamic generation of the corresponding bean:

Javaconfig Way:

1  PackageCom.myapp;2 3 ImportJavax.activation.DataSource;4 5 ImportOrg.springframework.context.annotation.Bean;6 ImportOrg.springframework.context.annotation.ComponentScan;7 Importorg.springframework.context.annotation.Configuration;8 ImportOrg.springframework.context.annotation.Profile;9 ImportOrg.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;Ten ImportOrg.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; One ImportOrg.springframework.jndi.JndiObjectFactoryBean; A @Configuration - @ComponentScan -  Public classDatasourceconfig { the@Bean (destroymethod= "Shutdown") -@Profile ("Dev") -      PublicDataSource Embeddeddatasource () { -         return(DataSource)NewEmbeddeddatabasebuilder () + . SetType (EMBEDDEDDATABASETYPE.H2) -. Addscript ("Classpath:xxx.sql") +. Addscript ("Classpath:yyy.sql") A . Build (); at     } -  - @Bean -@Profile ("prod")     -      PublicDataSource Jndidatasource () { -Jndiobjectfactorybean jofb=NewJndiobjectfactorybean (); inJofb.setjndiname ("Jndi/ids"); -Jofb.setresourceref (true); toJofb.setproxyinterface (XXX.class); +         return(DataSource) jofb.getobject (); -     } the}

XML mode:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beans3     xmlns= "Http://www.springframework.org/schema/beans"4 Xmlns:c= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/C"5 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"6 xmlns:p= "http://www.springframework.org/schema/p"7 Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"8 Xmlns:jee= "Http://www.springframework.org/schema/jee"9 Xmlns:context= "Http://www.springframework.org/schema/context"Ten xsi:schemalocation= "Http://www.springframework.org/schema/beans One http://www.springframework.org/schema/beans/spring-beans.xsd A Http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context.xsd - Http://www.springframework.org/schema/jdbc the http://www.springframework.org/schema/jdbc/spring-jdbc.xsd - Http://www.springframework.org/schema/jee - http://www.springframework.org/schema/jee/spring-jee.xsd "> -      +     <Beans Profile= "Dev"> -         <Jdbc:embedded-databaseID= "DataSource"> +             <Jdbc:script Location= "Classpath:xxx.sql"/> A             <Jdbc:script Location= "Classpath:yyy.sql"/> at         </Jdbc:embedded-database> -     </Beans> -     <Beans Profile= "prod"> -         <Jee:jndi-lookupJndi-name= "Jdbc/mydatabase"Resource-ref= "true"Proxy-interface= "Javax.sql.DataSource"/> -     </Beans> - </Beans>

Different environments are tagged with profile, but how can you activate it by setting spring.profiles.active and Spring.profiles.default. If Active,default is set, it loses its function. If none of the two are set, then beans with profiles are not generated.

There are several ways to set these two properties:

    • As the initialization parameter of Dispatcherservlet;
    • As the context parameter of the Web application;
    • As a jndi entry;
    • as environment variables;
    • As a system attribute of the JVM;
    • On the integration test class, use the @activeprofiles annotation configuration.

In the previous two ways, they can all be set in Web. xml:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appversion= "3.0" 3 xmlns= "Http://java.sun.com/xml/ns/javaee" 4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">7   <Display-name></Display-name>    8   <welcome-file-list>9     <Welcome-file>index.jsp</Welcome-file>Ten   </welcome-file-list> One   <Context-param> A       <Param-name>ApplicationContext</Param-name> -       <Param-value>/applicationcontext.xml</Param-value> -   </Context-param> the   <!--set the default value of the profile in context - -   <Context-param> -       <Param-name>Spring.profiles.default</Param-name> -       <Param-value>Dev</Param-value> +   </Context-param> -    +   <Listener> A       <Listener-class> at Org.springframework.web.context.ContextLoaderListener -       </Listener-class> -   </Listener> -    -   <servlet> -       <Servlet-name>Appservlet</Servlet-name> in       <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> -       <!--set the default value of the profile in the servlet - to       <Init-param> +       <Param-name>Spring.profiles.default</Param-name> -       <Param-value>Dev</Param-value> the       </Init-param> *       <Load-on-startup>1</Load-on-startup> $   </servlet>Panax Notoginseng   <servlet-mapping> -       <Servlet-name>Appservlet</Servlet-name> the       <Url-pattern>/</Url-pattern> +   </servlet-mapping> A </Web-app>

This defaults to the dev environment, which means that the bean that @Profile ("prod") annotations will not appear.

Spring-profile settings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.