谁能解释发生了什么?
public class MagicFinal {
public static void main(String[] args) {
System.out.println(A.s);
}
}
class A {
static {
System.out.println("class has been loaded");
}
public static final String s = "final";
public static final Integer i = 3;
}
Run Code Online (Sandbox Code Playgroud)
安慰 :
最后
那是什么?我不明白为什么没有加载类,我知道类总是在第一次调用时加载.字段s在字符串池中,我看到最终修饰符是魔术.
如果我删除final修饰符(public static String s = "final"),我会得到
安慰 :
类已加载
最后
注意:我更改了字段i:public static final int i = 3;并在控制台中显示它.我和String情况一样.为什么?
我有一个数组,我需要检查该数组中是否存在元素,或者使用jq,fruit.json从数组中获取该元素:
{
"fruit": [
"apple",
"orange",
"pomegranate",
"apricot",
"mango"
]
}
cat fruit.json | jq '.fruit .apple'
Run Code Online (Sandbox Code Playgroud)
不起作用
嗨,我需要通过AWS Java SDK为DynamoDB中的表设置编程时间.可能吗?我知道最近推出了TTL功能 - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html
更新: 没有特别的注释,但我们可以手动完成:
@DynamoDBAttribute
private long ttl;
Run Code Online (Sandbox Code Playgroud)
并在AWS中将其配置为ttl - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html
long now = Instant.now().getEpochSecond(); // unix time
long ttl = 60 * 60 * 24; // 24 hours in sec
setTtl(ttl + now); // when object will be expired
Run Code Online (Sandbox Code Playgroud) 我需要为我拥有的每个实体设置全局 TTL,并且它应该可以在一个地方进行配置。有机会通过 @RedisHash 注释来做到这一点:
@RedisHash(value = "persons",timeToLive = 100)
public class Person{
...
}
Run Code Online (Sandbox Code Playgroud)
或者我可以有一个领域
public class Person{
@TimeToLeave
Long ttl;
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我无法在一个地方更改它,并且维护它并不是很舒服。我在 applicaiton.properties 中有一个属性:
app.redis.ttl=100
Run Code Online (Sandbox Code Playgroud)
有机会在财产层面改变它会很棒。
我想知道是否有一个示例如何为Spring Cloud Config创建自定义EnvironmentRepository,原因是存在git,svn,Vault库,但是我不想使用它们,我需要我的自定义库。例如,如果我只想在地图中存储所有属性。
转换时我可以依赖订单吗?
public class DtoFunc implements Function<Entity,DTO>{
Entity previousEntity;
@Override
public DTO apply(Entity entity){
DTO dto = new DTO();
// do transforming
previousEntity = entity;
return dto;
}
}
Run Code Online (Sandbox Code Playgroud)
正如你可以看到我在我的函数中存储状态,并且我依赖于当我调用Lists.transform(entityList,new DtoFunc())时它会随之转换.它是否按顺序转换?我可以依靠它吗?
正确的答案:转换顺序取决于转换列表的访问权限,因为转换是惰性的.函数必须是无状态的,因为Guava不是线程安全的.
我有一个@Conditional bean-
@RestController("/user")
@ConditionalOnProperty(prefix = "user-controller", name = "enabled", havingValue = "true")
public void UserController {
@GetMapping
public String greetings() {
return "Hello User";
}
}
Run Code Online (Sandbox Code Playgroud)
它可以启用或禁用。我想创建一个涵盖两个用例的测试。我怎样才能做到这一点?我只有一个application.properties文件:
user-controller.enabled=true
Run Code Online (Sandbox Code Playgroud)
我可以将属性注入bean并添加一个setter来通过代码进行管理,但是这种解决方案并不完美:
@RestController("/user")
@ConditionalOnProperty(prefix = "user-controller", name = "enabled", havingValue = "true")
public void UserController {
@Value("${user-controller.enabled}")
private boolean enabled;
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@GetMapping
public String greetings() {
return enabled ? "Hello User" : "Endpoint is disabled";
}
}
Run Code Online (Sandbox Code Playgroud)
像这样
java ×3
spring ×2
arrays ×1
bash ×1
final ×1
guava ×1
jq ×1
json ×1
membership ×1
redis ×1
spring-boot ×1
spring-data ×1
spring-test ×1