Allow use of '&' prefix to access factory bean in SpEL
Prior to this change SpEL did not have an syntactic construct enabling easy access to a FactoryBean. With this change it is now possible to use &foo in an expression when the factory bean should be returned. Issue: SPR-9511
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,7 +18,9 @@ package org.springframework.expression;
|
||||
|
||||
/**
|
||||
* A bean resolver can be registered with the evaluation context
|
||||
* and will kick in for {@code @myBeanName} still expressions.
|
||||
* and will kick in for {@code @myBeanName} and {@code &myBeanName} expressions.
|
||||
* The <tt>&</tt> variant syntax allows access to the factory bean where
|
||||
* relevant.
|
||||
*
|
||||
* @author Andy Clement
|
||||
* @since 3.0.3
|
||||
@@ -26,7 +28,8 @@ package org.springframework.expression;
|
||||
public interface BeanResolver {
|
||||
|
||||
/**
|
||||
* Look up the named bean and return it.
|
||||
* Look up the named bean and return it. If attempting to access a factory
|
||||
* bean the name will have a <tt>&</tt> prefix.
|
||||
* @param context the current evaluation context
|
||||
* @param beanName the name of the bean to lookup
|
||||
* @return an object representing the bean
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -214,7 +214,7 @@ public enum SpelMessage {
|
||||
"A problem occurred when trying to resolve bean ''{0}'':''{1}''"),
|
||||
|
||||
INVALID_BEAN_REFERENCE(Kind.ERROR, 1059,
|
||||
"@ can only be followed by an identifier or a quoted name"),
|
||||
"@ or & can only be followed by an identifier or a quoted name"),
|
||||
|
||||
TYPE_NAME_EXPECTED_FOR_ARRAY_CONSTRUCTION(Kind.ERROR, 1060,
|
||||
"Expected the type of the new array to be specified as a String but found ''{0}''"),
|
||||
|
||||
@@ -25,12 +25,15 @@ import org.springframework.expression.spel.SpelEvaluationException;
|
||||
import org.springframework.expression.spel.SpelMessage;
|
||||
|
||||
/**
|
||||
* Represents a bean reference to a type, for example "@foo" or "@'foo.bar'"
|
||||
*
|
||||
* Represents a bean reference to a type, for example <tt>@foo</tt> or <tt>@'foo.bar'</tt>.
|
||||
* For a FactoryBean the syntax <tt>&foo</tt> can be used to access the factory itself.
|
||||
*
|
||||
* @author Andy Clement
|
||||
*/
|
||||
public class BeanReference extends SpelNodeImpl {
|
||||
|
||||
private final static String FACTORY_BEAN_PREFIX = "&";
|
||||
|
||||
private final String beanName;
|
||||
|
||||
|
||||
@@ -59,7 +62,10 @@ public class BeanReference extends SpelNodeImpl {
|
||||
|
||||
@Override
|
||||
public String toStringAST() {
|
||||
StringBuilder sb = new StringBuilder("@");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (!this.beanName.startsWith(FACTORY_BEAN_PREFIX)) {
|
||||
sb.append("@");
|
||||
}
|
||||
if (!this.beanName.contains(".")) {
|
||||
sb.append(this.beanName);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -525,7 +525,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
|
||||
// parse: @beanname @'bean.name'
|
||||
// quoted if dotted
|
||||
private boolean maybeEatBeanReference() {
|
||||
if (peekToken(TokenKind.BEAN_REF)) {
|
||||
if (peekToken(TokenKind.BEAN_REF) || peekToken(TokenKind.FACTORY_BEAN_REF)) {
|
||||
Token beanRefToken = nextToken();
|
||||
Token beanNameToken = null;
|
||||
String beanName = null;
|
||||
@@ -543,7 +543,14 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
|
||||
SpelMessage.INVALID_BEAN_REFERENCE);
|
||||
}
|
||||
|
||||
BeanReference beanReference = new BeanReference(toPos(beanNameToken) ,beanName);
|
||||
BeanReference beanReference = null;
|
||||
if (beanRefToken.getKind() == TokenKind.FACTORY_BEAN_REF) {
|
||||
String beanNameString = new StringBuilder().append(TokenKind.FACTORY_BEAN_REF.tokenChars).append(beanName).toString();
|
||||
beanReference = new BeanReference(toPos(beanRefToken.startPos,beanNameToken.endPos),beanNameString);
|
||||
}
|
||||
else {
|
||||
beanReference = new BeanReference(toPos(beanNameToken) ,beanName);
|
||||
}
|
||||
this.constructedNodes.push(beanReference);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -112,6 +112,8 @@ enum TokenKind {
|
||||
|
||||
BEAN_REF("@"),
|
||||
|
||||
FACTORY_BEAN_REF("&"),
|
||||
|
||||
SYMBOLIC_OR("||"),
|
||||
|
||||
SYMBOLIC_AND("&&"),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -182,12 +182,12 @@ class Tokenizer {
|
||||
}
|
||||
break;
|
||||
case '&':
|
||||
if (!isTwoCharToken(TokenKind.SYMBOLIC_AND)) {
|
||||
throw new InternalParseException(new SpelParseException(
|
||||
this.expressionString, this.pos, SpelMessage.MISSING_CHARACTER,
|
||||
"&"));
|
||||
if (isTwoCharToken(TokenKind.SYMBOLIC_AND)) {
|
||||
pushPairToken(TokenKind.SYMBOLIC_AND);
|
||||
}
|
||||
else {
|
||||
pushCharToken(TokenKind.FACTORY_BEAN_REF);
|
||||
}
|
||||
pushPairToken(TokenKind.SYMBOLIC_AND);
|
||||
break;
|
||||
case '|':
|
||||
if (!isTwoCharToken(TokenKind.SYMBOLIC_OR)) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -718,6 +718,9 @@ public class SpelReproTests extends AbstractExpressionTests {
|
||||
else if (beanName.equals("foo.bar")) {
|
||||
return "trouble";
|
||||
}
|
||||
else if (beanName.equals("&foo")) {
|
||||
return "foo factory";
|
||||
}
|
||||
else if (beanName.equals("goo")) {
|
||||
throw new AccessException("DONT ASK ME ABOUT GOO");
|
||||
}
|
||||
@@ -1950,6 +1953,32 @@ public class SpelReproTests extends AbstractExpressionTests {
|
||||
res = parser.parseExpression("#root.![values()]").getValue(context, List.class);
|
||||
assertEquals("[[test12, test11], [test22, test21]]", res.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void AccessingFactoryBean_spr9511() {
|
||||
StandardEvaluationContext context = new StandardEvaluationContext();
|
||||
context.setBeanResolver(new MyBeanResolver());
|
||||
Expression expr = new SpelExpressionParser().parseRaw("@foo");
|
||||
assertEquals("custard", expr.getValue(context));
|
||||
expr = new SpelExpressionParser().parseRaw("&foo");
|
||||
assertEquals("foo factory",expr.getValue(context));
|
||||
|
||||
try {
|
||||
expr = new SpelExpressionParser().parseRaw("&@foo");
|
||||
fail("Illegal syntax, error expected");
|
||||
} catch (SpelParseException spe) {
|
||||
assertEquals(SpelMessage.INVALID_BEAN_REFERENCE,spe.getMessageCode());
|
||||
assertEquals(0,spe.getPosition());
|
||||
}
|
||||
|
||||
try {
|
||||
expr = new SpelExpressionParser().parseRaw("@&foo");
|
||||
fail("Illegal syntax, error expected");
|
||||
} catch (SpelParseException spe) {
|
||||
assertEquals(SpelMessage.INVALID_BEAN_REFERENCE,spe.getMessageCode());
|
||||
assertEquals(0,spe.getPosition());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SPR12035() {
|
||||
|
||||
Reference in New Issue
Block a user