【问题标题】:mongodb auditing in spring boot for saving createdDate, lastModifiedDate, createdBy, lastModifiedBySpring Boot 中的 mongodb 审计以保存 createdDate、lastModifiedDate、createdBy、lastModifiedBy
【发布时间】:2017-01-19 12:21:52
【问题描述】:

我使用的是 spring boot,因此我没有使用任何 xml 文件进行配置。 我要做的是启用MongoAuditing 以保存createdDate、lastModifiedDate 等,同时使用MongoRepositories 保存数据。

我的模型课

@Component
@Document(collection = "CAPPING")
public class TemporaryCapping extends BaseEntity {

    @Field("contract_id")
    private BigInteger contractId;

    @Field("period_id")
    private BigInteger periodId;

    @Field("user_id")
    private BigInteger userId;

    @Field("amount")
    private Double amount;

    @Field("type_of_capping")
    private TypeOfCapping typeOfCapping;

    public BigInteger getContractId() {
        return contractId;
    }

    public void setContractId(BigInteger contractId) {
        this.contractId = contractId;
    }

    public BigInteger getPeriodId() {
        return periodId;
    }

    public void setPeriodId(BigInteger periodId) {
        this.periodId = periodId;
    }

    public BigInteger getUserId() {
        return userId;
    }

    public void setUserId(BigInteger userId) {
        this.userId = userId;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public TypeOfCapping getTypeOfCapping() {
        return typeOfCapping;
    }

    public void setTypeOfCapping(TypeOfCapping typeOfCapping) {
        this.typeOfCapping = typeOfCapping;
    }


}



public class BaseEntity implements Serializable{

@Id
@Indexed(unique = true)
private BigInteger id;

@CreatedDate
private DateTime createdDate;

@Field("modified_date")
private BigInteger modifiedDate;

public BigInteger getId() {
    return id;
}

public void setId(BigInteger id) {
    this.id = id;
}

public DateTime getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(DateTime createdDate) {
    this.createdDate = createdDate;
}

public BigInteger getModifiedDate() {
    return modifiedDate;
}

public void setModifiedDate(BigInteger modifiedDate) {
    this.modifiedDate = modifiedDate;
}

我使用@CreateDate 注释来保存createDate。 我已经为 DateTime 使用了 jodatime 依赖项

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>

spring-data-mongodb 也加入了依赖。

这是我的主要应用程序类

 @SpringBootApplication
    @EnableMongoAuditing
   public class Application {

   public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
   }

} 

由于日期没有保存在数据库中,我在这个实施中哪里错了? 我也知道要保存 @createdBy 你需要编写 AuditorAware bean,但现在我只是想保存 createdBy。

应该在哪里使用@EnableMongoAuditing?

【问题讨论】:

    标签: java spring mongodb maven


    【解决方案1】:

    在我的应用程序中,我通过 Java 代码进行配置。我以这种方式使用@EnableMongAuditing,并为 ZonedDateTime 创建转换。

    @Configuration
    @EnableMongoAuditing
    @EnableMongoRepositories(basePackages = { BASE_PACKAGE })
    public class MongoConfiguration extends AbstractMongoConfiguration {
    
        public static final String BASE_PACKAGE = "package.with.aggregates";
    
        @Value("${spring.data.mongodb.uri}")
        private String mongoUri;
    
        @Value("${spring.data.mongodb.database}")
        private String databaseName;
    
        @Override
        protected String getDatabaseName() {
            return databaseName;
        }
    
        @Override
        public Mongo mongo() throws Exception {
            return new MongoClient(new MongoClientURI(mongoUri));
        }
    
        // Here you must add converters to Joda datetypes. In my solution is ZonedDateTime
        @Override
        public CustomConversions customConversions() {
            List<Converter<?, ?>> converterList = new ArrayList<>();
            converterList.add(new DateToZonedDateTimeConverter());
            converterList.add(new ZonedDateTimeToDateConverter());
            return new CustomConversions(converterList);
        }
    
        @Override
        protected String getMappingBasePackage() {
            return BASE_PACKAGE;
        }
    }
    

    【讨论】:

    • MongoConfiguration 类在哪里使用?
    • 注解@Configuration 表示该类是类似于XML 的配置,但在Java 代码中。您可以拥有许多配置类,只需将 packageScan 打包到配置类所在的位置即可。
    • 如何将包扫描添加到 Spring Boot 应用程序。 @SpringBootApplication(scanBasePackages = {"com.some.package"})
    • 如果添加这样的 MongoConfiguration 类我的应用程序没有开始。并为 EsourceApplication 的主类提供 org.springframework.beans.factory.UnsatisfiedDependencyException 异常
    • 当我尝试使用此配置类连接到 mongodb 时,连接未建立。
    【解决方案2】:

    @EnableMongoAuditing 实际上可以放在配置中的任何位置(在@Configuration 注解旁边)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 2020-02-20
      • 2021-11-23
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多