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

问题描述 投票:0回答:1

我试图将一些项目从Spring Boot 1.5移植到Spring Boot 2.1.2

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

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

在另一个使用公共库的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的基本接口

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

这是我试图在mongodb中找到的实体

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映射上下文设置为应用程序上下文时它没有任何效果。

任何帮助,将不胜感激。

也许吧。

java spring spring-boot spring-el
1个回答
1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.