Spring Combat (iv) Bean profile in spring advanced Assembly

Source: Internet
Author: User

Profile, profiles, etc., can be translated into "configuration" in software development.

In version 3.1, spring introduces the functionality of Bean profile. To use profile, you first organize all the different bean definitions into one or more profiles, and ensure that the corresponding profile is in active state when you deploy the app to each environment.

1. @Profile annotations applied on the class

Use annotation @profile ("xxx") in Javaconfig on a class to specify that the bean in this class belongs to a certain profile.

It tells Spring that the bean in this configuration class will only be created when XXX profile is activated;

If the XXX profile is not activated, the method of @bean annotations in that class will be ignored.

2. @Profile annotation applied on the method

Spring3.2 start, you can use @profile annotations at the method level with @bean annotations.

One advantage of this is that you can put the declarations of different beans (which belong to the profile) in the same configuration Class (@Configuration).

The corresponding Bean is created only if the specified profile is activated.

Beans that do not have a profile specified will always be created.

@Configuration  Public class aconfigclass{@Bean @Profile ("A") MethodA () {...};
@Bean @Profile ("B") MethodB () {...}; }

3. Configure multiple profiles in XML

The following <beans> elements are nested in root <beans>, which allows you to configure multiple profiles in an XML file.

<beans profile= "Dev" > ...    ...

4. How to activate a profile?

Spring relies on two independent properties when determining whether a profile is activated:

A---spring.profiles.active

B---Spring.profiles.default

If the A property is set, its value is preferred by spring to determine which profile is active;

If not, spring looks for the value of B;

If both A and B are not set, then the profile,spring that are not activated will only create beans that are not defined in profile.

5, how to set the value of a and B properties above?

One of the authors ' favorite ways is to use the Dispatcherservlet parameter to set Spring.profiles.default as the profile of the development environment so that all developers can get the application source code from the version control software.

Web. XML (in-app)

<context-param>    <param-name>spring.profiles.  Default</param-name>    <param-value>dev</param-value></context-param>

6. When testing, activation of the relevant profile,spring provides @activeprofiles ("dev") annotations.

Spring Combat (iv) Bean profile in spring advanced Assembly

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.