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