在Spring的@Cacheable注释中,我想指定一个unless条件.
但是我的返回值既不是原始类型也不是Java bean,而是Enum.
如何在SpEL(Spring Expression Language)中与另一个Enum进行相等性比较?
我正在寻找两件事:
如何使用Spring启动"dev"配置文件禁用开发期间的所有缓存.在application.properties中,没有seam作为一般设置来关闭它.什么是最简单的方法?
如何禁用特定方法的缓存?我试着像这样使用SpEl:
@Cacheable(value = "complex-calc", condition = "#{${spring.profiles.active} != 'dev'}")
public String someBigCalculation(String input){
...
}
但我可以让它发挥作用.关于SO的问题有几个问题,但它们指的是XML配置或其他东西,但我使用的是Spring Boot 1.3.3,它使用了自动配置.
我不想让事情过于复杂.
我想通过XML配置Spring,这样如果存在特定的bean,它将被注入目标bean.如果它不存在,将注入一个不同的默认bean.
例如,如果我有这样的文件
<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>
Run Code Online (Sandbox Code Playgroud)
加载它,我想defaultCar注入驱动程序.但是,如果我还加载以下文件:
<bean id="customCar" class="FlyingCar">
<property name="name" value="Rocket Car"/>
<property name="maxAltitude" value="80000"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
我希望使用customCarbean而不是defaultCarbean.我最初的尝试不起作用,但我认为说明了我想要实现的目标:
<bean id="carDriver" class="Driver">
<property name="car" value="#{ @customCar eq null ? 'defaultCar' : 'customCar' }"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
我知道如何使用a PropertyPlaceholderConfigurer,但除了包含自定义bean的文件之外,我不想提供属性文件/ VM属性/环境变量/等.谢谢!
更新:
基于"使用工厂bean"评论,我调查了这个并提出了以下解决方案.首先,我创建了一个通用工厂bean,允许您指定默认的bean名称和覆盖bean名称:
public class DefaultOverrideFactoryBean implements FactoryBean, BeanFactoryAware {
public Object getObject() throws Exception {
return beanFactory.containsBean(overrideBeanName) ?
beanFactory.getBean(overrideBeanName) …Run Code Online (Sandbox Code Playgroud) 我按照http://spring.io/guides/gs/batch-processing/上的指南进行了操作,但它描述了一个没有可配置参数的作业.我正在使用Maven来构建我的项目.
我正在移植我在XML中定义的现有作业,并希望通过命令传入jobParameters.
我尝试了以下方法:
@Configuration
@EnableBatchProcessing
public class MyBatchConfiguration {
// other beans ommited
@Bean
public Resource destFile(@Value("#{jobParameters[dest]}") String dest) {
return new FileSystemResource(dest);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法编译项目:
mvn clean package
Run Code Online (Sandbox Code Playgroud)
然后我尝试启动这样的程序:
java my-jarfile.jar dest=/tmp/foo
Run Code Online (Sandbox Code Playgroud)
我得到一个例外说:
[...]
Caused by: org.springframework.expression.spel.SpelEvaluationException:
EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object of
type 'org.springframework.beans.factory.config.BeanExpressionContext'
Run Code Online (Sandbox Code Playgroud)
谢谢 !
我正在尝试使用Java Config实现方法安全性,但我收到一个错误: -
org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): No bean resolver registered in the context to resolve access to bean 'appPermissionEvaluator'
Run Code Online (Sandbox Code Playgroud)
方法是: -
@PreAuthorize("@appPermissionEvaluator.hasSystemPermission()")
public String something() {
...
}
Run Code Online (Sandbox Code Playgroud)
Config类定义是(MethodSecurityConfig.java): -
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Bean
public AppPermissionEvaluator appPermissionEvaluator() {
return new AppPermissionEvaluator();
}
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler =
new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(appPermissionEvaluator());
return expressionHandler;
}
...
}
Run Code Online (Sandbox Code Playgroud)
我检查了我能够在同一个类中自动装配bean,我发现默认的hasPermission()方法正在我已经实现它们,唯一的问题是从SpEL读取bean.我不确定是什么问题.任何指针?
我正在使用Spring 4.1.5和Spring security 3.2.7
我需要访问Thymeleaf模板中的系统属性.如果这是可能的,那将是很好的,这样我就不必使用属性显式填充spring mvc模型.我正在尝试将SPEL用于此目的,但它无法正常工作.
<h2 th:text="${ systemProperties['serverName'] }">Service name</h2>
<h2 th:text="*{ systemProperties['serverName'] }">Service name</h2>
Run Code Online (Sandbox Code Playgroud)
这两个都给了我:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1012E:(pos 17): Cannot index into a null value
Run Code Online (Sandbox Code Playgroud)
即使我尝试访问jdk属性它也会出现相同的错误,所以我知道这不是属性丢失的事实.我做错了什么或者有其他方法可以做到这一点?
有没有办法instanceof在Thymeleaf中使用Java 运算符?
就像是:
<span th:if="${animal} instanceof my.project.Cat" th:text="A cat"></span>
<span th:if="${animal} instanceof my.project.Dog" th:text="A dog"></span>
Run Code Online (Sandbox Code Playgroud) 我想在spring xml配置中使用我的应用程序常量.
我知道用Spring SpEl这样做是这样的:
<bean class="example.SomeBean">
<property name="anyProperty" value="#{ T(example.AppConfiguration).EXAMPLE_CONSTANT}" />
<!-- Other config -->
</bean>
Run Code Online (Sandbox Code Playgroud)
那么,有更好的方法吗?
我使用spring表达式语言有以下代码:
StandardEvaluationContext stdContext = new StandardEvaluationContext();
stdContext.setVariable("emp", filterInputData);
ExpressionParser parser = new SpelExpressionParser();
parser.parseExpression("#emp.?[name.toLowerCase().contains('Hari')]").getValue(stdContext);
Run Code Online (Sandbox Code Playgroud)
其中emp是bean的名称.这里的名称可以为null,并且在调用时name.toLowerCase()我得到一个nullpointer异常.如何处理此方案中的空值?我只需要调用toLowercase()非空值.
我想从单个类创建许多bean,所有bean都要在当前应用程序上下文中实例化,每个bean都基于属性文件中的前缀属性.我给出了一个我想要实现的例子.任何有关如何在没有过多代码的情况下执行此操作的提示(例如,没有多个类,复杂的工厂等)将不胜感激.
XML配置:
<bean id="bean1" class="Mybean">
<property name="prefix" value="bean1"/>
</bean>
<bean id="bean2" class="Mybean">
<property name="prefix" value="bean2"/>
</bean>
<bean id="bean3" class="Mybean">
<property name="prefix" value="bean3"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
属性文件:
bean1.name=alfred
bean2.name=bobby
bean3.name=charlie
Run Code Online (Sandbox Code Playgroud)
类:
class Mybean {
@Value("${#{prefix}.name}")
String name;
}
Run Code Online (Sandbox Code Playgroud)
主类:
public class Main {
@Autowired
List<MyBean> mybeans;
}
Run Code Online (Sandbox Code Playgroud) spring-el ×10
java ×9
spring ×9
spring-boot ×3
thymeleaf ×2
comparison ×1
constants ×1
enums ×1
spring-batch ×1
spring-cache ×1
spring-mvc ×1
xml ×1