spring-el

Passing varargs to Spring spEL causes “Method cannot be found on com.sun.proxy”

我只是一个虾纸丫 提交于 2019-12-04 05:31:56
问题 I try to write custom class for permissions evaluation, so it can be used with Spring Security @PreAuthorize and Spring Expression Language like for example this ( authority is just a regular Spring component with some role names): @PreAuthorize("@permissionEvaluator.anyOfRoles(@authority.ADMIN)") The PermissionEvaluator#anyOfRoles method declaration looks like follows: boolean anyOfRoles(String... roles) As shown this method takes varargs of String type. It works fine when only one parameter

How to run nested evaluations in Spring Expression Language

北战南征 提交于 2019-12-04 04:49:10
问题 I want to Use SPeL and I need to evaluate a parameter from a configuration source. The problem is that the name/key is dynamic. So I rely on one parameter to resolve the other. I basically need to check a Boolean parameter. Example: partial key/prefix: app.name full key: ${app.name}.feature.isEnabled So, in SPeL I try something like: #{'${app.name}.feature.isEnabled' != null && !'${app.name}.feature.isEnabled'} But this compiles but doesn't work. If app.name=my-app, the above resolves to

Using Spring beans as a key with @Cacheable annotation

笑着哭i 提交于 2019-12-03 20:31:38
问题 How to make following to work: - a spring bean that has a method that should be cached with @Cacheable annotation - another spring bean that creates keys for the cache (KeyCreatorBean). So the code looks something like this. @Inject private KeyCreatorBean keyCreatorBean; @Cacheable(value = "cacheName", key = "{@keyCreatorBean.createKey, #p0}") @Override public List<Examples> getExamples(ExampleId exampleId) { ... However the above code doesn't work: it gives following exception: Caused by:

Spring Expression Language - Java 8 forEach or stream on list

心不动则不痛 提交于 2019-12-03 19:31:17
问题 Is it possible for stream or forEach on list in SpEL? e.g. List<String> x = new LinkedList<>(Arrays.asList("A","AAB")); ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(x); parser.parseExpression("x.stream().map(x -> x.replaceAll(\"A\", \"B\")).collect(Collectors.toList())").getValue(context)) 回答1: SpEL is not Java, it's a different language; the acronym stands for Spring Expression Language . It doesn't understand Java8

Spring 3.1 cache - how to use the returned value in the SpEL

倾然丶 夕夏残阳落幕 提交于 2019-12-03 19:05:00
I am trying to make an eviction of an entry in a Spring-managed cache (Spring 3.1 abstraction). I need to refer to the returned value of the method in the SpEL of the "key" property in the annotation: /* (How to refer to the 'T' returned value in the "KEY_ID"?) */ @Caching(evict = { @CacheEvict(value = CACHE_BY_ID, key = KEY_ID) }) public T delete(AppID appID, UserID userID) throws UserNotFoundException { return inner.delete(appID, userID); } Is there any way to do this? It doesn't seem like there is any way to reference the returned object: http://static.springsource.org/spring/docs/3.1.x

Injecting property values from property file or xml file into PreAuthorize(…) java annotation (Unresolved)

不想你离开。 提交于 2019-12-03 16:24:29
I've asked this question in my previous post here: SpEL for spring security: Passing Values from XML to Java based SpEL configuration . But it wasn't yet resolved. I want to inject values either from an xml configuration or from external file into @PreAuthorize(...) annotation. It is not easy like injecting by using @Value annotation. To recall the question, I provide the following information. I have the following xml configuration file (example.xml) that has properties and initialized its corresponding values. <beans> <bean id="userBean" class="x.y.User"> <property name="name" value="A"/>

Spring @Value TypeMismatchException:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'

ぐ巨炮叔叔 提交于 2019-12-03 08:17:30
问题 I want to use the @Value annotation to inject a Double property such as: @Service public class MyService { @Value("${item.priceFactor}") private Double priceFactor = 0.1; // ... and using Spring property placeholder (Properties files): item.priceFactor=0.1 I get Exception: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'; nested exception is java.lang.NumberFormatException: For input string: "${item

How to define default null value in application.yml in Spring Boot

回眸只為那壹抹淺笑 提交于 2019-12-03 02:55:49
I'm trying to define the default value as null value in application.yml with SpringBoot version 1.3.0.RELEASE. The goal is be able to refer it with a class with ConfigurationProperties annotation -- application.yml -- test.foo: ${test.bar:#{null}} but it doesn't work. If the value of test.bar is not defined, set test.foo to null (default value) I already have spring-el in my dependencies. I don't want to use PropertyPlaceholderConfigurer.setNullValue It's seem to work in @Value but not in application.yml (see http://farenda.com/spring/spring-inject-null-value/ ) It's a bug, or yaml is not

Spring @Value TypeMismatchException:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'

时光总嘲笑我的痴心妄想 提交于 2019-12-02 23:36:42
I want to use the @Value annotation to inject a Double property such as: @Service public class MyService { @Value("${item.priceFactor}") private Double priceFactor = 0.1; // ... and using Spring property placeholder (Properties files): item.priceFactor=0.1 I get Exception: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'; nested exception is java.lang.NumberFormatException: For input string: "${item.priceFactor}" Is there a way to use a Double value coming from a properties file? Try changing the following

Spring 3: Inject Default Bean Unless Another Bean Present

依然范特西╮ 提交于 2019-12-02 22:59:26
I would like to configure Spring via XML such that if a particular bean exists, it will be injected into the target bean. If it does not exist, a different, default bean, will be injected. For example if I have a file like this <bean id="carDriver" class="Driver"> <property name="car" value="SOME EXPRESSION GOES HERE, SEE ATTEMPT BELOW"/> </bean> <bead id="defaultCar" class="Car"> <property name="name" value="Honda Accord"/> </bean> And load it, I would like the defaultCar injected into the driver. However, if I also load the following file: <bean id="customCar" class="FlyingCar"> <property