Spring Boot conditional annotation expression

Source: Internet
Author: User

@Configuration
@ConditionalOnClass (Jaxrsserverfactorybean.class)
@ConditionalOnExpression ("' ${cxf.jaxrs.component-scan} ' = = ' true ' && ' ${cxf.jaxrs.classes-scan} '! = ' True ')
@Import (Springcomponentscanserver.class)
Protected static class Jaxrscomponentconfiguration {

}

/**
* Configuration annotation for a conditional element, depends on the value of a spel
* expression.
*
* @author Dave Syer
*/
@Retention (Retentionpolicy.runtime)
@Target ({elementtype.type, elementtype.method})
@Documented
@Conditional (Onexpressioncondition.class)
Public @interface conditionalonexpression {

/**
* The SPEL expression to evaluate. Expression should return {@code true} if the
* Condition passes or {@code false} if it fails.
* @return the Spel expression
*/
String value () default "true";

}

/*
* Copyright 2012-2016 The original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You are not a use this file except in compliance with the License.
* Obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable or agreed to writing, software
* Distributed under the License is distributed on a "as is" BASIS,
* Without warranties or CONDITIONS of any KIND, either express or implied.
* See the License for the specific language governing permissions and
* Limitations under the License.
*/

Package org.springframework.boot.autoconfigure.condition;

Import Org.springframework.beans.factory.config.BeanExpressionContext;
Import Org.springframework.beans.factory.config.BeanExpressionResolver;
Import Org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Import Org.springframework.context.annotation.ConditionContext;
Import Org.springframework.context.expression.StandardBeanExpressionResolver;
Import org.springframework.core.Ordered;
Import Org.springframework.core.annotation.Order;
Import Org.springframework.core.type.AnnotatedTypeMetadata;

/**
* A Condition that evaluates a spel expression.
*
* @author Dave Syer
* @see Conditionalonexpression
*/
@Order (ordered.lowest_precedence-20)
Class Onexpressioncondition extends Springbootcondition {

@Override
Public Conditionoutcome Getmatchoutcome (Conditioncontext context,
Annotatedtypemetadata metadata) {
string expression = (string) metadata
. Getannotationattributes (ConditionalOnExpression.class.getName ())
. Get ("value");
expression = wrapifnecessary (expression);
String rawexpression = expression;
expression = Context.getenvironment (). resolveplaceholders (expression);
configurablelistablebeanfactory beanfactory = Context.getbeanfactory ();
beanexpressionresolver resolver = (beanfactory! = null)
? Beanfactory.getbeanexpressionresolver (): null;
beanexpressioncontext expressioncontext = (beanfactory! = null)
? New Beanexpressioncontext (Beanfactory, NULL): null;
if (resolver = = null) {
resolver = new Standardbeanexpressionresolver ();
}
Boolean result = (Boolean) resolver.evaluate (expression, expressioncontext);
return new Conditionoutcome (result, conditionmessage
. Forcondition (Conditionalonexpression.class, "(" + rawexpression + ")")
. Resultedin (result));
}

/**
* Allow user to provide bare expression with no ' #{} ' wrapper.
* @param expression Source expression
* @return Wrapped expression
*/
private string Wrapifnecessary (string expression) {
if (!expression.startswith ("#{")) {
Return "#{" + Expression + "}";
}
return expression;
}

}

Learn how annotation expressions are calculated.

Spring Boot conditional annotation expression

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.