The environment and profile of spring combat

Source: Internet
Author: User

1. Configuring the Profile Bean

The solution provided by spring for environment-related beans is not much different from the solution at the time of the build. Of course, in this process, you need to decide which bean to create and not which bean to create based on the environment. But spring does not make such decisions at the time of construction, but rather waits for the runtime to determine. The result is that the same deployment unit (which could be a war file) can be applied to all environments and there is no need to rebuild. In version 3.1, spring introduces the functionality of Bean profile. To use profile, your firstThe first is to organize all the different bean definitions into one or more profiles and applywhen you deploy to each environment, make sure that the corresponding profile is in the active state. in a Java configuration, you can use @profile annotations to specify which of a bean belongs toProfile . For example, in a configuration class, the DataSource of an embedded database might bePlace the following:

What I want you to notice is that the @profile annotation is applied at the class level. It will tell spring that the bean in this configuration class will only be created when Dev profile is active. If Dev profile is not activated, then the method with @bean annotations is ignored

Off.

At the same time, you may also need to have a configuration for your production environment, as shown below

In this case, the corresponding Bean is created only when PROD profile is active.

In Spring 3.1, @profile annotations can only be used at the class level. However, starting with Spring 3.2, you can also use @profile annotations at the method level with the @bean annotations. In this case, you can put the declarations of these two beans in the same

A configuration class, as shown below

Here's a question to note, though each datasource Bean is declared in a Profile , and the corresponding Bean is created only if the specified profile is activated. But there may be other beans that are not declared in a given profile range inside. Beans that do not have a profile specified are always created and have no relation to which profile is activated .

1.2 Configuring Profiles in XML

1.3 Activating Profile

Spring is dependent on two independent genera when determining which profile is active.Sex: spring.profiles.active andSpring.profiles.default. If you set thethe Spring.profiles.active property, then its value is used to determinewhich profile is active. But if the spring.profiles.active is not setattribute, then spring will look for the value of the Spring.profiles.default. if spring.profiles.active and Spring.profiles.defaultis not set, then there is no active profile, so it will only createa bean that is defined in the profile.

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 settings.

The environment and profile of spring combat

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.