我有一个JpaRepository在Spring MVC应用程序中持久保存新创建的实体.这个实体看起来像这样(非常简化):
@Entity
public class Translation {
.....
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
private Version version;
....
}
Run Code Online (Sandbox Code Playgroud)
和版本实体:
@Entity
public class Version {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "version_code")
private long code;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "version", cascade = {CascadeType.ALL}, orphanRemoval = true)
private Set<Translation> translations;
}
Run Code Online (Sandbox Code Playgroud)
我创建了这样的翻译对象
TranslationDTO t = new TranslationDTO();
t.setText(translationText);
ClientVersionDTO version = new ClientVersionDTO();
version.setId(11); …Run Code Online (Sandbox Code Playgroud) 我通常将所有活动事件委托给一个单独的控制器类,该控制器类有一个处理事件的特殊方法 Activity
@Override
public boolean handleMessage(int what, Object data) {
switch (what) {
case ExerciseViewEvent.STARTUP:
workerHandler.post(new Runnable() {
public void run() {
onStartup();
}
});
return true;
}
Run Code Online (Sandbox Code Playgroud)
这样做是为了保持UI线程响应并在后台任务中进行所有计算.
但是,当Activity.onDestroy()系统controller.dispose()调用方法时,会调用方法,以这种方式清除控制器中的所有内容
@Override
protected synchronized void dispose() {
.................
if (model != null) {
synchronized (model) {
model.dispose();
}
model = null;
}
helper = null;
.....................
super.dispose();
}
Run Code Online (Sandbox Code Playgroud)
如您所见,处理控制器发生在UI线程中,而不将其委托给处理程序线程.
例如,当onDestroy在onStartup()方法中间调用时会发生问题:onDestroy清理模型和所有其他引用,但在内部onStartup方法中它尝试在某个时刻访问模型,但考虑到它null,会抛出异常.
解决此问题的最佳方法是什么?我不想锁定每个控制器方法,因为它们中的一些可能同时发生而不会相互干扰.
我有一个Nexus maven repo,我想利用REST API来查询我特定组中的工件列表.我偶然发现了这个文档,但它似乎非常简洁,我找不到我需要的东西.
https://oss.sonatype.org/nexus-restlet1x-plugin/default/docs/rest.html
我想要这样的东西
http://mydomain:8081/nexus/service/local/repositories/list?groupId=com.test.superproduct&repo=snapshots
Run Code Online (Sandbox Code Playgroud)
它会输出一个列表
更具体地说,我需要一个位于组中的工件版本列表,但我也可以从工件名称中提取版本.
我有一个Spring集成流程,涉及异步执行,从网关返回值到控制器,返回值后继续集成流程.
这是网关:
@MessagingGateway
public interface GW {
@Gateway(requestChannel = "f.input")
Task input(Collection<MessengerIncomingRequest> messages);
}
Run Code Online (Sandbox Code Playgroud)
这是流程:
@Bean
IntegrationFlow jFlow() {
return IntegrationFlows.from(
MessageChannels.executor("f.input", executor()))
.split()
.channel(MessageChannels.executor(executor()))
.transform(transformer)
.channel(routerChannel())
.get();
}
@Bean
ThreadPoolTaskExecutor executor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
...
return pool;
}
@Bean
MessageChannel routerChannel() {
return MessageChannels
.publishSubscribe("routerChannel", executor())
.get();
}
@Bean
IntegrationFlow routerChannelFlow() {
return IntegrationFlows
.from(routerChannel())
.publishSubscribeChannel(s -> s
.subscribe(f -> f.bridge(null))
.subscribe(process()))
.get();
}
@Bean
IntegrationFlow process() {
return f ->
f.route(p -> p.getKind().name(),
m -> …Run Code Online (Sandbox Code Playgroud) 复制一个文件并用 gradle 重命名它的最简洁、最优雅和最短的方法是什么?
到目前为止我只能想到这个:
copy {
from projectDir
into projectDir
include '.env.template'
rename '.env.template', '.env'
}
Run Code Online (Sandbox Code Playgroud) 我有一个 spring 应用程序和这个应用程序的集成测试。我想用模拟豆替换豆。
我真正的豆子长这个样子
@Service
public class MyService {
}
Run Code Online (Sandbox Code Playgroud)
为了测试,我想更换它
@Service
public class TestMyService {
}
Run Code Online (Sandbox Code Playgroud)
我能想到的就是将配置文件用于不同的服务。例如:
@Service
@Profile("!test")
public class MyService implements IMyService {
}
@Service
@Profile("test")
public class TestMyService implements IMyService {
}
Run Code Online (Sandbox Code Playgroud)
然后我像这样自动装配 bean
@Autowired
private IMyService myService;
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
我有一个由Java配置支持的Spring MVC应用程序,我想为所有涉及Callable <>接口的异步调用设置默认超时.例如,考虑这样的控制器方法:
@RequestMapping
public Callable<String> doSmth() {
return () -> {
return "myview";
}
}
Run Code Online (Sandbox Code Playgroud)
我希望有一个控制器(每个应用程序)控制器有多少时间在请求超时之前完成它的工作.
我想有一个Java配置的例子,而不是xml
我之前曾经使用过EventBus,它易于使用且易于理解.但是,这一次,我想尝试使用RxJava进行类似事件总线的通信,但是不清楚如何从RxJava中删除事件,或者更好的说,它应该如何正确设计以具有与EventBus类似的行为我打电话的时候removeStickyEvent?
在RxJava中,即使我订阅了这个observable,我也可以使用BehaviorSubject来回复,但是当处理这个事件时我该怎么办?如果我不想让这个事件再次播放怎么办?
例如,一个片段触发事件然后结束.另一个片段侦听此事件并处理它.然后,如果此应用程序从不同的情况再次触发"另一个"活动,那么它将再次订阅相同的BehaviorSubject并将再次处理该陈旧事件,这不是我想要实现的.
我在我的ant脚本中使用ant-contrib库,但是我不知道如何使用foreach标签创建固定数量的循环?通过固定的迭代次数,我不是指一些硬编码值,而是指从命令行提供的ant属性.
我有一个 Spring 集成应用程序,其中使用IntegrationFlow构建器模式引导 Java DSL 配置。我需要使用一个.handle()方法,并且我不想像大多数网络示例那样在那里有 lambda 函数。相反,我想将其委托给一个单独的 bean(服务)。我该如何实施?
我发现下面的一个示例使用了内部类,但我需要使用依赖于其他 bean 的自动装配 Spring bean,因此内部类对我来说不是一个选择。对我来说最好的方法是什么?
@Bean
public IntegrationFlow icedFlow() {
return IntegrationFlows.from(MessageChannels.queue("iced", 10))
.handle(new GenericHandler<OrderItem>() {
@Override
public Object handle(OrderItem payload, Map<String, Object> headers) {
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
System.out.println(Thread.currentThread().getName()
+ " prepared cold drink #" + coldDrinkCounter.incrementAndGet()
+ " for order #" + payload.getOrderNumber() + ": " + payload);
return payload;
}
})
.channel("output")
.get();
}
Run Code Online (Sandbox Code Playgroud) 我已经开始使用 docker 和 docker-compose 来定义数据库。由于我没有在配置中定义卷(我的错误),数据库已经在容器内创建,现在下次运行 docker-pull 时,我将重新创建这个数据库,导致我的数据丢失。在不丢失数据的情况下将数据移出容器的最佳方法是什么?
db:
image: "mydbimage"
environment:
MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
MYSQL_DATABASE: "jirio"
MYSQL_USER: "${DB_USERNAME}"
MYSQL_PASSWORD: "${DB_PASSWORD}"
Run Code Online (Sandbox Code Playgroud)
文件
FROM mysql:5.6
ADD schema.sql /docker-entrypoint-initdb.d/schema.sql
Run Code Online (Sandbox Code Playgroud) 我有一个连接到 kafka 集群的 Spring Boot 应用程序。如何从 Java 代码运行 KSQL?
我有旗帜
int A = 1;
int B = 2;
int C = 4;
Run Code Online (Sandbox Code Playgroud)
我想做一个检查,只能为一个函数指定一个标志
check(A | B | C) ; // invalid
check(A); // valid
check(B); // valid
check(B | C); // invalid
void check(int flags) {
// check that if A is specified, then B and C can't
// check that if B is specified, then A and C can't
// check that if C is specified, then B and A can't
}
Run Code Online (Sandbox Code Playgroud)
如果没有大量的"if"陈述,我怎样才能做到这一点?
java ×3
android ×2
spring ×2
ant ×1
ant-contrib ×1
c# ×1
concurrency ×1
dispose ×1
docker ×1
gradle ×1
jpa ×1
ksqldb ×1
nexus ×1
rest ×1
rx-android ×1
rx-java ×1
spring-boot ×1
spring-mvc ×1