gpt4 book ai didi

java - Spring Boot 2 - 无法在 null 上找到属性或字段 'entitiesProperties'

转载 作者:行者123 更新时间:2023-12-02 01:40:48 29 4
gpt4 key购买 nike

我正在尝试将一些项目从 Spring Boot 1.5 移植到 Spring Boot 2.1.2

我有一个名为 common.jar 的 bean 库,其中包含一些常见的 bean 类,每个类代表 Mongo db 中的一个文档。

使用 SPEL 设置每个文档的集合名称。

在另一个使用公共(public)库的 Spring Boot 应用程序中,我有一个名为“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 的基本接口(interface)

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");

我知道基本上,在正确的上下文中搜索实体属性的机制存在一些问题,但是当我将 mongodb 映射上下文设置为应用程序上下文时,它没有任何效果。

如有任何帮助,我们将不胜感激。

加尔。

最佳答案

从 Spring Boot 2.1 + 开始,在 Spel 表达式中引用 bean 的正确方法是在其名称前添加 @ 前缀。它以前可以工作,因为 Spring Data 用于在评估上下文中提供 ListableBeanFactory 的内容。现在编写 Spel 表达式的正确方法是:

@Document(collection = "#{@entitiesProperties.getAdmissions()}")

您可以查看此问题以获取更多信息:https://github.com/spring-projects/spring-boot/issues/15707

关于java - Spring Boot 2 - 无法在 null 上找到属性或字段 'entitiesProperties',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54460867/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com