@profile of Spring annotations

Source: Internet
Author: User

@profile in spring is similar to the profile in Maven and is configured to change parameters.

For example, in the development environment and production environment using different parameters, you can configure two sets of configuration files, through the @profile to activate the required environment, but maintenance of two sets of profiles than in Maven to maintain a set of configuration files, in the POM through profile to modify the configuration file parameters of the benefits.

There are exceptions, such as I call the Mall interface in development often can not return the data I need, every time I need to mock data, so I wrote a mock argument to invoke the class, in the development environment to use this class, test environment and production environment using the normal pretext to invoke the class, This way, you don't have to manually change the code every time you develop it.

Note: @profile versions that support the method level and class level after 3.2, the 3.1 version only supports the class level.

To the next, say @profile use method.

First, the annotation configuration

 
  1. /** Configuring the production environment Call class **/
  2. @service ("Productrpc")
  3. @profile ("prop")
  4. Public class Productrpcimpl implements PRODUCTRPC
  5. Public String Productbaseinfo (Long sku) {
  6. return Productresource.querybaseinfo (Long SKU);
  7. }
  8. }
  9. /** Configuring the production environment Call class **/
  10. @service ("Productrpc")
  11. @profile ("Dev")
  12. Public class Mockproductrpcimpl implements PRODUCTRPC
  13. Public String Productbaseinfo (Long sku) {
  14. return "Iphone7";
  15. }
  16. }
  17. /** Call class **/
  18. Public class Demo () {
  19. @Resource (name="Productrpc")
  20. private Productrpc Productrpc;
  21. Public Void Demo () {
  22. String skuinfo = Productrpc.productbaseinfo (123123L);
  23. Logger.info (Skuinfo);
  24. }
  25. }

This completes the annotation-based profile configuration. The interface is called normally when configured as a production environment, and the mock interface is used when the callback is made for the development environment.

Second, XML configuration

 
  1. <!--development Environment--
  2. <beans profile="Dev">
  3. <Bean id= "beanname" class="Com.pz.demo.ProductRPC"/>
  4. </Beans>
  5. <!--production Environment--
  6. <beans profile="Dev">
  7. <Bean id= "beanname" class="Com.pz.demo.MockProductRPC"/>
  8. </Beans>


Third, activate profile

Note: When spring determines that the profile is active, it relies on two separate properties: Spring.profiles.active and Spring.profile.default. If the Spring.profiles.actives property is set, its value is used to determine if the profile is active. If the spring.profiles.active attribute is not set, spring will look for the value of Spring.profiles.default. If both spring.profiles.active and Spring.profiles.default are not set. (The red part is not validated successfully in the project, pending testing)

1. Configuring in the context of a servlet (web. xml)

 
    1. <context-param>
    2. <param-name>spring.profiles.default</param-name>
    3. <param-value>dev</param-value>
    4. </context-param>

2. Initialization parameters as Dispatcherservlet

 
  1. Ervlet>
  2. <servlet-name>springmvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class >
  4. <init-param>
  5. <param-name>contextconfiglocation</param-name>
  6. <param-value>classpath:/spring-servlet.xml</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>spring.profiles.default</param-name>
  10. <param-value>dev</param-value>
  11. </init-param>
  12. <load-on-startup>1</load-on-startup>
  13. </servlet>

3.spring-junit using @activeprofiles for activation

 
    1. @RunWith (Springjunit4classrunner. Class)
    2. @ContextConfiguration (locations = "/spring-config.xml")
    3. @ActiveProfiles ("Dev")
    4. Public class Maintest {
    5. ...
    6. }

4. As a Jndi entry


5. As an environment variable


6. System Properties as a JVM

@profile of Spring annotations

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.