我是Spring Framework的新手,所以我决定买一本书("Spring in action"第3版).目前我的第一章涵盖了基础知识 - 依赖注入(DI)和面向方面编程(AOP).
我设法运行显示DI的代码.
1#骑士实施的界面
package com.springinaction.knights;
public interface Knight {
void embarkOnQuest() throws QuestException;
}
Run Code Online (Sandbox Code Playgroud)
1#骑士班
package com.springinaction.knights;
public class BraveKnight implements Knight {
private Quest quest;
public BraveKnight(Quest quest) {
this.quest = quest; //<co id="co_injectedQuest"/>
}
public void embarkOnQuest() throws QuestException {
quest.embark();
}
}
Run Code Online (Sandbox Code Playgroud)
#2Quest界面
package com.springinaction.knights;
public interface Quest {
void embark() throws QuestException;
}
Run Code Online (Sandbox Code Playgroud)
#2Quest例外
package com.springinaction.knights;
public class QuestException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
Run Code Online (Sandbox Code Playgroud)
#2Quest类型类实现 …
我有以下验证,我必须检查返回的正文是否包含字符串"id": 6354,但它解释特殊字符的斜杠.我如何验证包含双引号的字符串?
码
import static org.hamcrest.Matchers.containsString;
import com.jayway.restassured.response.Response;
response.then()
.body(containsString("\"id\": 6354"));
Run Code Online (Sandbox Code Playgroud)
错误
Response body doesn't match expectation.
Expected: a string containing "\"id\": 6354"
Actual: {...,"id": 6354, ...}
Run Code Online (Sandbox Code Playgroud) 根据gradle tomcat插件常见问题解答部分的主页:
如何通过插件启动远程调试我的Tomcat?
我需要添加以下环境属性:
GRADLE_OPTS = -Xdebug Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
Run Code Online (Sandbox Code Playgroud)
在容器运行期间,我应该看到它在特定端口上侦听的信息:Listening for transport dt_socket at address: 5005但在我的情况下,它不起作用.
我该如何正确配置?我正在运行intellij idea 14.1 Ultimate.
我已经为android studio使用了android插件的自动生成功能,它为我生成了以下代码,但我无法理解为什么需要final valfor CREATORfield?我final在kotlin第一次看到关键字.
data class Person(
val name: String,
val surname: String
) : Parcelable {
constructor(source: Parcel): this(source.readString(), source.readString())
override fun describeContents(): Int {
return 0
}
override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.writeString(name)
dest?.writeString(surname)
}
companion object {
@JvmField final val CREATOR: Parcelable.Creator<Person> = object : Parcelable.Creator<Person> {
override fun createFromParcel(source: Parcel): Person {
return Person(source)
}
override fun newArray(size: Int): Array<Person?> {
return arrayOfNulls(size)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 在我的练习中,我应该验证电话号码,其中正确的是:
1234567890
123-456-7890
123.456.7890
(123)456-7890
(123) 456-7890
456-7890
Run Code Online (Sandbox Code Playgroud)
我试过了[(]?[0-9][0-9][0-9][).-]? ?[0-9][0-9][0-9][.-]?[0-9][0-9][0-9][0-9],但它似乎也接受了类似的东西(123.456-7890.我怎么能处理这个?或者我应该采取完全不同的方式?
以下是一些无效的电话号码:
123-45-6789
123:4567890
123/456-7890
Run Code Online (Sandbox Code Playgroud) 我stdlib.jar在IntelliJ(12.1.4)的项目中添加了外部jar文件(图片上),但是当我想在创建的package(ChapterOne)中使用一些静态方法时,它无法解决它.但是它在默认包中找到类没有问题.我怎么能让它发挥作用?
似乎依赖是可以的.
我正在设置一个划痕项目,目前我正在Spring MVC 4.1.5使用java 配置进行配置.整个应用程序正在tomcat gradle插件上运行.
有人可以解释我为什么我需要对类进行以下调用DefaultServletHandlerConfigurer才能将请求映射到我的控制器?
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
Run Code Online (Sandbox Code Playgroud)
没有启用它我的所有请求都被拒绝,服务器说没有特定请求的映射.
我看了春天的文档要找出来,但描述并没有告诉我太多.
启用转发到"默认"Servlet.使用此方法时,DefaultServletHttpRequestHandler将尝试自动检测"默认"Servlet名称.或者,您可以通过enable(String)指定默认Servlet的名称.
我正在与朋友编程项目一起创建。我们把它分成两部分,我负责客户端(简单的窗口应用程序),他负责服务器。我应该在 websocket 的帮助下将 JSON 对象发送到他的服务器(他给了我信息,我应该发送什么http://pastebin.com/dmYBtN25)。我知道如何创建 json 对象,但对我来说问题是如何将 websocket lib 与 json 结合使用(目前我正在使用 weberknecht 和 json-lib )。下面是我发现的一个例子,它可能是我的客户的基础。我会很高兴获得提示和帮助,或者只是如何做到这一点的简单示例。
import java.net.URI;
import java.net.URISyntaxException;
import de.roderick.weberknecht.WebSocket;
import de.roderick.weberknecht.WebSocketConnection;
import de.roderick.weberknecht.WebSocketEventHandler;
import de.roderick.weberknecht.WebSocketException;
import de.roderick.weberknecht.WebSocketMessage;
public class App {
public static void main(String[] args) {
try {
URI url = new URI("ws://127.0.0.1/test");
WebSocket websocket = new WebSocketConnection(url);
// Register Event Handlers
websocket.setEventHandler(new WebSocketEventHandler() {
public void onOpen() {
System.out.println("--open");
}
public void onMessage(WebSocketMessage message) {
System.out.println("--received message: "
+ message.getText());
}
public void …Run Code Online (Sandbox Code Playgroud) 我正在阅读Joshua Blochs的"Effective Java"第2版.目前我在第22项描述了内部和嵌套类,但我无法理解这句话的意思:
当且仅当它们出现在非静态上下文中时,匿名类才会封闭实例.
有人能给我一个代码示例并解释它究竟做了什么吗?我知道如果它InnerClass是OuterClass其封闭实例的成员OuterClass,但就匿名类而言,这对我来说听起来很奇怪.
我有一个集合,Map<Pair<DateTime, String>, List<Entity>>其中先前使用流分组.Entity是一个带有int属性和getValue()方法的简单类.
现在,我想聚合Entity使用我的简单EntityAccumulatormodyfing前一个地图的类型的值Map<Pair<DateTime, String>, EntityAccumulator>.据我所知,实现这一目标的唯一方法是创建我自己的自定义收集器,但是我已经坚持finisher()应该返回的方法了Pair.
或者,也许有更简单的方法来实现我想要的结果?
StreamProcessing
Map<Pair<DateTime, String>, EntityAccumulator> collect = entities.stream()
.collect(Collectors.groupingBy(entity-> Pair.of(entity.getTimestamp(), entity.getName())))
.entrySet().stream()
.collect(new EntityCollector()));
Run Code Online (Sandbox Code Playgroud)
EntityAccumulator
private static class EntityAccumulator {
private int result = 0.0;
public EntityAccumulator() { }
public EntityAccumulator(int result) {
this.result = result;
}
public void calculate(Entity entity) {
result += entity.getValue();
}
public EntityAccumulatoradd(EntityAccumulator other) {
return new EntityAccumulator(this.result + other.result);
} …Run Code Online (Sandbox Code Playgroud) java ×10
spring ×2
android ×1
aop ×1
collectors ×1
dictionary ×1
gradle ×1
hamcrest ×1
ide ×1
java-8 ×1
java-stream ×1
json ×1
kotlin ×1
parcel ×1
regex ×1
rest-assured ×1
spring-aop ×1
spring-mvc ×1
tomcat ×1
websocket ×1