The role of @Profile is to classify some meta-data into two states of active and inactive, and then you can choose to configure the Bean in both active and inactive states.
In the inactive state the usual annotations have one! operator, usually written as: @Profile ("!p"), where p is the name of the profile.
The Appprofileconfig Bean in the following demo is created by the IOC container in the active state, and AppProfileConfig2 is created by the IOC container in inactive state:
The idea of the demo is: first define two domain classes, then write two configuration classes namely the above mentioned Appprofileconfig and AppProfileConfig2 two classes, and finally write a test class:
The sample code is as follows:
The first domain class: the code for the alarm class is as follows:
Package Com.timo.profile.domain; Public classAlarm {PrivateString name; PrivateInteger alarmseverity; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getalarmseverity () {returnalarmseverity; } Public voidsetalarmseverity (Integer alarmseverity) { This. alarmseverity =alarmseverity; }}
The second domain class: Ouyangfeng's code is as follows:
Package Com.timo.profile.domain; Public classOuyangfeng {PrivateString name; PrivateInteger age; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; }}
The first configuration class: Appprofileconfig's code is as follows:
Package Com.timo.profile;import Com.timo.profile.domain.alarm;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.Profile, @Configuration @profile ("Sixi") Public classAppprofileconfig {@Bean PublicAlarm Alarm () {Alarm Alarm=NewAlarm (); Alarm.setalarmseverity (1); Alarm.setname ("Historical Alarms"); returnAlarm; }}
The code for the second configuration class AppProfileConfig2 is as follows:
Package Com.timo.profile;import Com.timo.profile.domain.ouyangfeng;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.Profile, @Configuration @profile ("!flower") Public classAppProfileConfig2 {@Bean PublicOuyangfeng Ouyangfeng () {Ouyangfeng Ouyangfeng=NewOuyangfeng (); Ouyangfeng.setage ( -); Ouyangfeng.setname ("Ouyang"); returnOuyangfeng; }}
The code for the test class is as follows:
Package Com.timo.profile;import Com.timo.profile.domain.alarm;import Com.timo.profile.domain.Ouyangfeng; Import Org.springframework.context.annotation.AnnotationConfigApplicationContext; Public classTest { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext ctx=NewAnnotationconfigapplicationcontext ();
Activates the class name Sixi in @profile: ctx.getenvironment (). Setactiveprofiles ( "sixi" ); Ctx.register (appprofileconfig.class, AppProfileConfig2.class); Ctx.refresh (); Alarm Alarm= Ctx.getbean (Alarm.class); Ouyangfeng Ouyangfeng= Ctx.getbean (Ouyangfeng.class); System. out. println ("alarm="+alarm); System. out. println ("ouyangfeng="+Ouyangfeng); }}
Example of application of spring @Profile