Spring 引导 2 - 属性 或字段 'entitiesProperties' 无法在 null 上找到
Spring Boot 2 - Property or field 'entitiesProperties' cannot be found on null
我正在尝试将一些项目从 Spring Boot 1.5 移植到 Spring Boot 2.1.2
我有一个名为 common.jar 的 bean 库,其中包含一些公共 bean 类 每个代表 Mongo db 中的一个文档。
我使用 SPEL 设置每个文档的集合名称。
在另一个使用公共库的 Spring 引导应用程序中,我有一个名为 'entitiesProperties' 的组件,它实际上从配置中加载它的字段并保存集合的名称。
该应用程序在 Spring Boot 1 中运行良好。5.x 但是在 Spring Boot 2.1.2 中出现异常:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'entitiesProperties' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:53) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:89) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:114) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:300) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity.getCollection(BasicMongoPersistentEntity.java:97) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexForEntity(MongoPersistentEntityIndexResolver.java:109) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexFor(MongoPersistentEntityIndexResolver.java:90) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:134) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:127) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:111) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:54) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:398) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:355) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:405) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:248) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:191) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:85) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2394) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2387) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:823) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at com.opsc.demo.services.OnScheduleService.onSchedule(OnScheduleService.java:34) ~[classes/:na]
这是所有通用 bean 的基本接口
public interface BaseEntity extends Serializable {
String getPid();
void setPid(String pid);
Long getTimeStamp();
void setTimeStamp(Long timestamp);
Boolean getCancelled();
void setCancelled(Boolean cancelled);
Boolean getProcessed();
void setProcessed(Boolean processed);
Date getCancelDate();
void setCancelDate(Date cancelDate);
Boolean getTransferrable();
void setTransferrable(Boolean transferrable);
}
这是我试图在 mongo 数据库中找到的实体
packace com.some.package
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
// SPEL to get the name of the collection name as defined in config
@Document(collection = "#{entitiesProperties.getAdmissions()}")
public class Admission implements BaseEntity {
private static final long serialVersionUID = -7793451827998043905L;
@Id private String admissionId;
private String pid;
private Long timestamp = System.currentTimeMillis();
//... some more fields and getters and setters
}
这是保存集合名称的 bean
package com.some.package.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component("entitiesProperties")
@ConfigurationProperties("some.mongodb")
public class EntitiesProperties {
private String admissions;
public OpscEntitiesProperties() {
}
public String getAdmissions() {
return admissions;
}
public void setAdmissions(String admissions) {
this.admissions = admissions;
}
// ... some more fields getters and setters
}
这是代码的运行方式
Query q = new Query();
q.addCriteria(Criteria.where("processed").is(true).and("cancelled").is(false).and("dischargeDate").is(null));
List<Admission> admissions = mongoTemplate.find(q, Admission.class, "admissions");
我明白,基本上,在正确上下文中搜索 entitiesProperties 的机制存在一些问题,但是当我将 mongodb 映射上下文设置为应用程序上下文时,它没有任何效果。
如有任何帮助,我们将不胜感激。
加仑
从 Spring Boot 2.1 + 开始,在 Spel 表达式中引用 bean 的正确方法是在其名称前加上前缀 @
。它以前可以工作,因为 Spring 数据用于在评估上下文中提供 ListableBeanFactory 的内容。
现在编写 Spel 表达式的正确方法是:
@Document(collection = "#{@entitiesProperties.getAdmissions()}")
您可以查看此问题以获取更多信息:https://github.com/spring-projects/spring-boot/issues/15707
我正在尝试将一些项目从 Spring Boot 1.5 移植到 Spring Boot 2.1.2
我有一个名为 common.jar 的 bean 库,其中包含一些公共 bean 类 每个代表 Mongo db 中的一个文档。
我使用 SPEL 设置每个文档的集合名称。
在另一个使用公共库的 Spring 引导应用程序中,我有一个名为 'entitiesProperties' 的组件,它实际上从配置中加载它的字段并保存集合的名称。
该应用程序在 Spring Boot 1 中运行良好。5.x 但是在 Spring Boot 2.1.2 中出现异常:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'entitiesProperties' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:53) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:89) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:114) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:300) ~[spring-expression-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity.getCollection(BasicMongoPersistentEntity.java:97) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexForEntity(MongoPersistentEntityIndexResolver.java:109) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexFor(MongoPersistentEntityIndexResolver.java:90) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:134) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:127) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:111) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:54) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:398) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:355) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:405) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:248) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:191) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:85) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2394) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2387) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:823) ~[spring-data-mongodb-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at com.opsc.demo.services.OnScheduleService.onSchedule(OnScheduleService.java:34) ~[classes/:na]
这是所有通用 bean 的基本接口
public interface BaseEntity extends Serializable {
String getPid();
void setPid(String pid);
Long getTimeStamp();
void setTimeStamp(Long timestamp);
Boolean getCancelled();
void setCancelled(Boolean cancelled);
Boolean getProcessed();
void setProcessed(Boolean processed);
Date getCancelDate();
void setCancelDate(Date cancelDate);
Boolean getTransferrable();
void setTransferrable(Boolean transferrable);
}
这是我试图在 mongo 数据库中找到的实体
packace com.some.package
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
// SPEL to get the name of the collection name as defined in config
@Document(collection = "#{entitiesProperties.getAdmissions()}")
public class Admission implements BaseEntity {
private static final long serialVersionUID = -7793451827998043905L;
@Id private String admissionId;
private String pid;
private Long timestamp = System.currentTimeMillis();
//... some more fields and getters and setters
}
这是保存集合名称的 bean
package com.some.package.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component("entitiesProperties")
@ConfigurationProperties("some.mongodb")
public class EntitiesProperties {
private String admissions;
public OpscEntitiesProperties() {
}
public String getAdmissions() {
return admissions;
}
public void setAdmissions(String admissions) {
this.admissions = admissions;
}
// ... some more fields getters and setters
}
这是代码的运行方式
Query q = new Query();
q.addCriteria(Criteria.where("processed").is(true).and("cancelled").is(false).and("dischargeDate").is(null));
List<Admission> admissions = mongoTemplate.find(q, Admission.class, "admissions");
我明白,基本上,在正确上下文中搜索 entitiesProperties 的机制存在一些问题,但是当我将 mongodb 映射上下文设置为应用程序上下文时,它没有任何效果。
如有任何帮助,我们将不胜感激。
加仑
从 Spring Boot 2.1 + 开始,在 Spel 表达式中引用 bean 的正确方法是在其名称前加上前缀 @
。它以前可以工作,因为 Spring 数据用于在评估上下文中提供 ListableBeanFactory 的内容。
现在编写 Spel 表达式的正确方法是:
@Document(collection = "#{@entitiesProperties.getAdmissions()}")
您可以查看此问题以获取更多信息:https://github.com/spring-projects/spring-boot/issues/15707