小编Anu*_*kar的帖子

Spring-boot EntityListener,应用程序上下文中的一些bean的依赖关系形成一个循环

我在以下设计中面临依赖循环(摘自此处)。

\n

我有 2 个实体 Post 和 PostLog。创建 Post 后,我​​也想将其保留在 PostLog 中。因此创建了侦听器并将其应用于“Post”实体。实体 Post 和 PostLog 也都使用 spring-boot“AuditingEntityListener”,但为了简单起见,我没有在此处添加该代码。

\n

我的实体和监听器结构 -

\n
@Data\n@EqualsAndHashCode(callSuper = false)\n@Entity\n@Table(name = "post")\n@EntityListeners({AuditingEntityListener.class, PostLogListener.class})\npublic class Post extends Auditable<String> {\n...\n}\n\n@Data\n@EqualsAndHashCode(callSuper = false)\n@Entity\n@Table(name = "post_log")\n@EntityListeners(AuditingEntityListener.class)\npublic class PostLog extends Auditable<String> {\n...\n}\n\n@Component\n@RequiredArgsConstructor\npublic class PostLogListener {\n  \n  private final PostLogRepository repo;\n\n  @PostPersist\n  @Transactional(propagation = Propagation.REQUIRES_NEW)\n  public void logEvent(final Post post) {\n    PostLog log = createLog(post); // implementation is omitted here for keeping short\n    repo.save(log);\n  }\n}\n\n@Repository\npublic interface PostLogRepository extends CrudRepository<PostLog, Long> …
Run Code Online (Sandbox Code Playgroud)

java dependency-injection jpa entitylisteners spring-boot

5
推荐指数
1
解决办法
3002
查看次数

如何在application.yml文件中添加类路径资源

使用 spring-boot,在 application.properties 文件的情况下,可以在值中指定类路径。

app.templates = classpath:app\mock-templates

如何在 application.yml 中指定相同的内容?

当这样指定时,读取的值不正确 -

app:
  templates : 'classpath:app\mock-templates'
Run Code Online (Sandbox Code Playgroud)

价值注入——

@Value("${app.templates}")
private String templateLocation; 
// value injected is "classpath:app\mock-templates", it should be <absolute-path>\app\mock-templates
Run Code Online (Sandbox Code Playgroud)

yaml spring-boot

3
推荐指数
1
解决办法
3万
查看次数