Example of application of spring @Profile

Source: Internet
Author: User

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

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.