Profile Bean Usage Scenario Description:
In the development of software, in the database, often not a library can be solved, generally divided into the development of libraries, test libraries, production libraries, when these libraries set up links, will also configure their corresponding data.
One way to do this is to have a separate configuration class, or to configure the Bean in XML, and then, at build time, determine which configuration is compiled into the deployed application. This approach is feasible, but there is a problem, that is, when migrating from the development phase to the QA phase, there is no problem with redeployment, but when migrating from the QA phase to the production phase, it is possible that the rebuild will introduce bugs and cause disturbing emotions among QA team members.
At this point, we can introduce profile.
The benefits of profile bean
Spring provides the profile bean configuration, the principle is that the root of the solution is no different, the difference between the two methods is that spirng at runtime, depending on the environment to determine which beans should be created, which are not created. Because it is not in the deployment to judge, but wait for the time to judge, so that a deployment unit (possibly a war package) can be suitable for all environments, so there is no need to refactor.
Next, let's take a sample to understand what it means.
Let's say we have three kinds of mounts, and when we're on land we can choose to jump frogs, choose a little black dragon in the sky, and choose a skin shrimp in the ocean.
So we first create a pet Mount interface class petmounts and then create their own mounts respectively. flyingmounts ,landmounts ,seamounts
Petmounts
Package com.bean.profile;/** * Created by IntelliJ idea. * Author Xueyuan * Data 2017/02/22 * Time 16:46 */public interface petmounts { void Letsgo ();}
Flyingmounts
Package Com.bean.profile; /** * Created by IntelliJ idea. * Author Xueyuan * Data 2017/02/22 * time */publicclas Simplements petmounts { publicvoid Letsgo () { System.out.println ("I am flying mount Little Black dragon, we go ...! "); }}
Landmounts
Package Com.bean.profile; /** * Created by IntelliJ idea. * Author Xueyuan * Data 2017/02/22 * time */publicclas Simplements petmounts { publicvoid Letsgo () { System.out.println ("I am a land horse jumping frog, we go ...! "); }}
Seamounts
Package Com.bean.profile; Import org.springframework.stereotype.Component; /** * Created by IntelliJ idea. * Author Xueyuan * Data 2017/02/22 * time */@Component
public
class
implements
petmounts { public
void
Letsgo () { System.out.println ("I am the Sea mount shrimp, we go ...! "
); }}
All right, we've created the mounts, and we'll start by choosing what mounts to choose from.
First look at the Java file configuration method
PackageCom.bean.profile;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.Profile;/*** Created by IntelliJ idea. * Author Xueyuan * Data 2017/02/22 * Time 16:44*/@Configuration Public classpetmountsconfig {@Bean @Profile ("Sky") Publicpetmounts Sky () {return Newflyingmounts (); } @Bean @Profile ("Land") Publicpetmounts Land () {return Newlandmounts (); } @Bean @Profile ("Sea") Publicpetmounts Sea () {return Newseamounts (); }}
We're going to create the beans separately and then add the profile to the corresponding method
Tip: You can add @profile annotations only at the class level before Spring3.1, and support commenting on methods from Spring3.2 onwards
Configuration profile in XML
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <Beans Profile= "Sky"> <BeanID= "Flyingmounts"class= "Com.bean.profile.FlyingMounts"/> </Beans> <Beans Profile= "Land"> <BeanID= "Landmounts"class= "Com.bean.profile.LandMounts"/> </Beans> <Beans Profile= "Sea"> <BeanID= "Seamounts"class= "Com.bean.profile.SeaMounts"/> </Beans></Beans>
Configuration in XML, mainly the profile property in the Lee <beans>
Good configuration is also configured, then how to use the following, how to activate it?
Spring relies on two independent properties to determine which profile is active: Spring.profiles.active and Spring.profiles.default. If the Spring.profiles.active property is set, then its value is used to determine which profile is active. But 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, then there is no active profile, so only those beans that are not defined in the profile are created.
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 following is the configuration profile in the Web (lazy down the picture on the book)
The method used when using profile in the test is as follows:
@RunWith (Springjunit4classrunner. Class)/*@ContextConfiguration (classes = petmountsconfig.class)* / = "config.") @ActiveProfiles ("Sea")publicclass letsgo { ... }
Activation method We also know, now let us choose a mount, go to the sister bar!
PackageCom.bean.profile;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.test.context.ActiveProfiles;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** Created by IntelliJ idea. * Author Xueyuan * Data 2017/02/22 * Time 20:42*/@RunWith (Springjunit4classrunner.class)/*@ContextConfiguration (classes = petmountsconfig.class)*/@ContextConfiguration (Value= "Config + +") @ActiveProfiles ("Sea") Public classLetsgo {@AutowiredPetMounts petmounts; @Test Public voidLetsgolog () {petmounts.letsgo (); }}
The above is a simple small example of peofile bean, if there are errors, please point out, thank you ~
Code: Https://github.com/eoooxy/springinaction Test under Com.bean.profile
springinaction--Configuring the Profile Bean