这似乎是一个非常直截了当的问题,但我似乎无法在任何地方找到答案.
在Spring中,我可以使用@EventListener注释为事件创建一个侦听器,如下所示:
@Component
public class MyListener {
@EventListener
public void handleEvent(ContextRefreshedEvent event) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我需要相同的方法来监听多个事件并根据发生的事件采取不同的行动呢?
直觉上,我在想类似的东西:
@Component
public class MyListener {
@EventListener
public void handleEvents(ContextRefreshedEvent event, ContextStopped event) {
String event;
if(event instanceof ContextRefreshedEvent)
event = "Refreshed";
if(event instanceof ContextStoppedEvent)
event = "Stopped";
}
}
Run Code Online (Sandbox Code Playgroud)
EventListener注释监听多个事件的正确方法是什么?同一方法如何根据发生的实际事件进行区分?
非常感谢.
我目前正在将我所做的一个项目从 Spring Data with Hibernate 转换为 Spring Data with ElasticSearch。
之前,我能够使用注释自动生成实体的 id @GeneratedValue(strategy = GenerationType.IDENTITY)。这样,当实体被保存时,就会为其生成一个 ID 号。
但是,我似乎找不到 ElasticSearch 用于实现相同结果的注释。
使用Hibernate注解的实体:
@Entity
@Table(name = "person")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return …Run Code Online (Sandbox Code Playgroud) java spring elasticsearch spring-data spring-data-elasticsearch
我当前的项目几乎可以使用以下配置:
root %%COMP_WEB_ROOT;
# COMP Static File serving
location /comp {
alias %%COMP_WEB_ROOT;
try_files $uri$args $uri$args/ /index.html;
add_header 'Cache-Control' 'no-cache, no-store, must-revalidate';
}
# Hide the index file, not exposing that path specifically
location = /index.html {
internal;
}
Run Code Online (Sandbox Code Playgroud)
这样,我就防止了整个应用程序的缓存,这是不希望的,因为我只想防止index.html页存储缓存。
所以我试图将add_header线像这样放置在第二个块内:
root %%COMP_WEB_ROOT;
# COMP Static File serving
location /comp {
alias %%COMP_WEB_ROOT;
try_files $uri$args $uri$args/ /index.html;
error_page 401 = @error401web;
}
# Hide the index file so that we're not exposing that path specifically
location = /index.html …Run Code Online (Sandbox Code Playgroud) 我目前有一个多层结构数据,如下所示:
行业类有一个
Set<Company>可以为null 的私有字段.公司类有一个
Set<Division>可以为null 的私有字段.Division类有一个
Set<Group>可以为null 的私有字段.组类有一个私有字段
groupName,可以为null,可以使用getter(getGroupName())检索.
我试图将Industry的一个实例流式传输到Group层,并将所有groupName连接成一个String,其间带有"/".
如果此Industry实例不包含任何groupName,则返回字符串"null".
根据我对Java 8的有限知识,我想这样编码:
industry.stream()
.flatmap(industry -> industry.getCompanies().stream())
.filter(Objects::nonNull)
.flatmap(company -> company.getDivisions().stream())
.filter(Objects::nonNull)
.flatmap(division -> division.getGroups().stream())
.map(group -> group.getGroupName)
.collect(Collectors.joining("/")));
Run Code Online (Sandbox Code Playgroud)
这段代码似乎有些瑕疵.另外,我不知道在哪里添加声明,如果Industry无法检索任何groupName,而不是将所有groupName连接成一个字符串,只需返回一个字符串"null".
在我的情况下使用Java 8流的正确方法是什么?
谢谢.
我一直在使用 JPA CRUD 存储库默认方法,例如 find、findAll、delete 等,用于我所有的数据库操作。
现在我有两个实体:
@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue
private long id;
@OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE)
private Set<Child> children;
}
@Entity
public class Child implements Serializable {
@Id
@GeneratedValue
private long id;
@ManyToOne
@JoinColumn
private Parent parent;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让我在 ParentRepository 中创建一个新方法,让我根据父级的 ID 检索父级的所有子级的计数?
所以在我的 ParentRepository 中,我可以创建一个看起来像这样的方法:
int findAllChildrenCount(Long parentID);
Run Code Online (Sandbox Code Playgroud) java ×4
spring ×3
spring-data ×2
caching ×1
crud ×1
events ×1
java-8 ×1
java-stream ×1
nginx ×1
spring-mvc ×1