On @ profile of spring annotation

The @ profile in spring is very similar to the profile in Maven. The parameters are changed through configuration.

For example, if different parameters are used in the development environment and production environment, two sets of configuration files can be configured to activate the required environment through @ profile, but maintaining two sets of configuration files is not as beneficial as maintaining one set of configuration files in Maven and modifying the parameters of the configuration file through profile in POM.

There are exceptions, for example, when I call the shopping mall interface in development, I can't return the data I need, and I need mock data every time. So I wrote an excuse for calling mock parameter, and used this class in the development environment. The test environment and production environment use normal excuse to call class, so that we do not need to manually change some code when we develop.

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

To get back to business, let's talk about how to use @ profile.

1、 Annotation configuration

This completes the annotation based profile configuration. When it is configured as a production environment, the interface will be called normally. When it is a development environment, the mock interface will be used for callback.

2、 XML configuration

3、 Activate profile

Note: when determining which profile is active, spring needs to rely on two independent properties: spring profiles. Active and spring profile. default。 If spring is set profiles. The active property, its value will be used to determine which profile is active. If spring is not set profiles. With the active attribute, spring will look for spring profiles. The value of default. If spring profiles. Active and spring profiles. Default is not set. (the red part is not verified successfully in the project, to be tested)

1. Configure in servlet context (web. XML)

2. As the initialization parameter of dispatcherservlet

3. Spring JUnit is activated using @ activeprofiles

4. As JNDI entry

5. As an environmental variable

6. System attribute as JVM

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>