假设我有这个课程:
public class FooToBarTransformer {
public Bar transform(Foo foo) {
// do some cool stuff
}
}
Run Code Online (Sandbox Code Playgroud)
而且我想Function在其他类中使用它:
public class Thing {
public Thing(Function<Foo, Bar> f) {
this.converter = f;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我Thing通过Java 实例化,我会用Java8 Lambdas这样做:
FooToBarTransformer transformer = new FooToBarTransformer();
new Thing((foo) -> transformer.transform(foo));
// or new Thing(transformer::transform);
Run Code Online (Sandbox Code Playgroud)
我的问题是,我需要创造一个Thing虽然春天.
<bean id="fooToBarTransformer" class="com.mypackage.FooToBarTransformer"/>
<bean id="theThing" class="com.mypackage.Thing">
<constructor-arg index="0" ????????? />
</bean>
Run Code Online (Sandbox Code Playgroud)
现在,我已经考虑过一些可能的解决方法,使这更容易:
FooToBarTransformer实施Function,那就简单了ref="fooToBarTransformer" FooToBarTransformer实现和更改Thing以获取该接口的实例而不是Function …JSF Web应用程序尝试从Java EE Web服务检索资源.JSF Web应用程序运行正常.自从我将Http客户端源添加到反向Web服务资源以来,JBoss实例声称:
15:29:47,689 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http--0.0.0.0-443-1) Error Rendering View[/index.xhtml]: javax.el.ELException: /surfaceParts/sideBarLeft.xhtml @14,79 value="#{categories.cats}": java.lang.LinkageError: loader constraint violation: when resolving method "org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor.<init>(Lorg/apache/http/client/HttpClient;Lorg/apache/http/protocol/HttpContext;)V" the class loader (instance of org/jboss/modules/ModuleClassLoader) of the current class, at/fhj/ase/ssl/SSLClientHelper, and the class loader (instance of org/jboss/modules/ModuleClassLoader) for resolved class, org/jboss/resteasy/client/core/executors/ApacheHttpClient4Executor, have different Class objects for the type xecutors.ApacheHttpClient4Executor.<init>(Lorg/apache/http/client/HttpClient;Lorg/apache/http/protocol/HttpContext;)V used in the signature
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIData.getValue(UIData.java:731) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIData.getDataModel(UIData.java:1798) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIData.setRowIndex(UIData.java:473) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at …Run Code Online (Sandbox Code Playgroud) 我有一个工作代码,它动态加载具有不同类名的不同类实现。类文件被加载到内存数据库( Apache Derby Db ) 中,并且类加载器从BLOB列中检索.class文件。
我想要做的是,将.class文件作为带有版本列和IS_ENABLED标志的二进制 BLOB插入,然后类加载器将在运行时加载不同版本的类。将有相同数量的已编译类版本的 db 条目,并且只有一个类的IS_ENABLED标志设置为TRUE。
因为我尝试使用自定义类加载器加载相同的类名,所以我得到以下异常;
Exception in thread "main" java.lang.LinkageError: loader (instance of com/levent/classloader/DerbyServerClassLoader): attempted duplicate class definition for name: "com/levent/greeter/Greeter"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at com.levent.classloader.DerbyServerClassLoader.findClass(DerbyServerClassLoader.java:38)
at com.levent.example.ClientClassLoaderDBVersionDemo.main(ClientClassLoaderDBVersionDemo.java:43)
Run Code Online (Sandbox Code Playgroud)
同一个接口(Greeter.java)有两个不同的.class文件(Greeter.class.v1、Greeter.class.v2)(在代码开头插入)
在测试代码的开头,从 lib/classes/ 文件夹中检索类文件并将其作为 blob 二进制数据插入到内存数据库中,然后,.class 文件由数据库顺序检索并加载。加载同名类时,发生异常。
我怎么解决这个问题?有没有办法卸载一个类,或者无论如何重新加载一个同名的类?
package com.levent.classloader;
import java.sql.Blob; …Run Code Online (Sandbox Code Playgroud) 所以我只想在这里创建一个非常简单的应用程序用于演示目的:
将以下代码添加到控制器的头文件中:
@property (weak, nonatomic) IBOutlet UIView *myView;
Run Code Online (Sandbox Code Playgroud)现在,我明白我可以通过以下方式将UIView链接到控制器:
我的问题是:我可以不按Ctrl键拖动吗?如果是这样,怎么样?
更具体地说 - 必须同时将我的头文件和故事板放在屏幕上是令人讨厌的,似乎应该有一种方法来实现这种连接而不这样做.
我也明白我可以通过在我的控制器的viewDidLoad函数中创建它来手动放置视图,但我真的想使用界面构建器来简化/可视化事物.
编辑:我的问题的答案是否影响我是否使用故事板或xib/nib文件?(我转而使用它工作的那个)
我有一个程序可以创建 2 个面板,然后在其中放置一个标签和两个按钮。将标签设置为不可见setVisible(false),然后添加两个按钮并打包框架。当我单击第一个按钮时,显示标签,setVisible(true),第二个按钮再次隐藏它,setVisible(false)。当我单击每个按钮时,它们会移动以填充标签隐藏的空间,然后再次移动以避开显示的标签。我想阻止这种情况发生,即使标签被隐藏,按钮也保持在同一个位置。
这是代码:
public class MainFrame extends JFrame{
public JLabel statusLabel;
public JButton show;
public JButton hide;
public MainFrame(){
super("MagicLabel");
JPanel topPanel = new JPanel(); //Create Top Panel
statusLabel = new JLabel(""); //Init label
statusLabel.setVisible(false); //Hide label at startup
topPanel.setSize(400, 150); //Set the size of the panel, Doesn't work
topPanel.add(statusLabel); //Add label to panel
JPanel middlePanel = new JPanel(); //Create Middle Panel
show= new JButton("Show"); //Create show button
hide= new JButton("Hide"); //Create hide …Run Code Online (Sandbox Code Playgroud) 我想要的是获取数据库更新.
即如果数据库发生任何更改或插入新记录,它应通知用户.
直到知道我实现的是使用jQuery,如下所示
$(document).ready(function() {
var updateInterval = setInterval(function() {
$('#chat').load('Db.jsp?elect=<%=emesg%>');
},1000);
});
Run Code Online (Sandbox Code Playgroud)
它对我来说很好,但我的老师告诉我,推荐使用彗星或长轮询技术不是一个好方法.
任何人都可以给我一些使用servlet/jsp中的comet或long轮询获取数据库更新的示例吗?我正在使用Tomcat作为服务器.
出于项目目的,我必须坚持使用Java 1.6进行开发,我使用了异步客户端jar文件并将其合并到我的项目中以实现异步功能,并使用java 1.6开发并导出了JAR。这个jar包含在java 7上运行的另一个Web项目的构建路径中。它在java 7 64位和tomcat7 64位上运行良好。但是当使用java 7 32位和tomcat7 32位时看到以下错误:
java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type taticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory; used in the signature
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:299)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
at com.ning.http.client.AsyncHttpClient.<clinit>(AsyncHttpClient.java:146)
at com.netspective.ems.invoker.AsyncApiInvoker.post(AsyncApiInvoker.java:39)
at com.netspective.ems.invoker.AsyncApiInvoker.post(AsyncApiInvoker.java:32)
at com.netspective.ems.invoker.AsyncApiInvoker.post(AsyncApiInvoker.java:73)
at com.netspective.ems.api.ExceptionApi.notify(ExceptionApi.java:42)
at com.netspective.ems.NetspectiveEMS.prepareAndPostDetails(NetspectiveEMS.java:101)
at com.netspective.ems.NetspectiveEMS.log(NetspectiveEMS.java:168)
at com.netspective.watchtower.sdk.log4j.WatchtowerClientAppender.append(WatchtowerClientAppender.java:32)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
at org.apache.log4j.Category.callAppenders(Category.java:206) …Run Code Online (Sandbox Code Playgroud) 我花了最后一点时间拔出我的头发试图在我的测试中找到问题,并最终发现它与模拟一个采用原始参数的方法有关.这是一个演示问题的示例测试:
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.Test;
public class MockitoTest {
public static interface Foo {
public Object causeProblems(long arg);
}
@Test
public void testFoo() {
Foo foo = mock(Foo.class);
foo.causeProblems(123);
verify(foo, times(1)).causeProblems(any());
}
}
Run Code Online (Sandbox Code Playgroud)
在运行此测试时(我正在使用Mockito 1.10和Java8),由于某种原因,我的堆栈跟踪显示在线上的NPE verify:
java.lang.NullPointerException
at com.amazon.jetstream.executor.worker.invoke.MockitoTest.testFoo(MockitoTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
....
Run Code Online (Sandbox Code Playgroud)
我认为我的堆栈跟踪的一部分正在被抑制(?)进一步深入研究它,如果我在Eclipse中运行并"检查"该行,我可以从中获得更多信息,这简单地告诉我:
java.lang.NullPointerException at longValue()
Run Code Online (Sandbox Code Playgroud)
问题:
我有一个ThingsProvider要尝试使用Mockito(简化版)测试的界面,定义如下:
interface ThingsProvider {
Iterable<? extends Thing> getThings()
}
Run Code Online (Sandbox Code Playgroud)
现在,当我要使用Mockito对其进行测试时,我正在执行以下操作(再次简化该问题):
ThingsProvider thingsProvider = mock(ThingsProvider.class);
List<Thing> things = Arrays.asList(mock(Thing.class));
when(thingsProvider.getThings()).thenReturn(things); // PROBLEM IS HERE
Run Code Online (Sandbox Code Playgroud)
编译错误信息: The method thenReturn(Iterable<capture#11-of ? extends Thing>) in the type OngoingStubbing<Iterable<capture#11-of ? extends Thing>> is not applicable for the arguments (List<Thing>)
现在,纯粹是为了进行测试,我将最后一行更改为
when(thingsProvider.getThings()).thenReturn((List)things); // HAHA take THAT generics!
Run Code Online (Sandbox Code Playgroud)
...但是在非测试代码中这样做显然是不好的。
我的问题:
Thing,这是接口所期望的。在#2上-我不简单返回的主要原因Iterable<Thing>是,有几种不同的扩展,其中具体类型中埋藏着返回特定子类型的东西,而我最终Type mismatch: cannot convert from Iterable<MagicalThing> to Iterable<Thing>遇到了类似的问题-也许解决方案是一种更好的解决方案解决这个问题?
对于那些不太熟悉Mockito的人,下面是更纯粹的Java版本#1:
public static void main(String...args) {
List<Integer> …Run Code Online (Sandbox Code Playgroud) 当我尝试在android中运行此代码时,我得到的结果是,"Name":"Text1\/Text2"但结果应该是{"Name":"Text1/Text2"}.
try {
String str;
JSONObject json = new JSONObject();
json.put("Name", "Text1/Text2");
str = json.toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
谢谢.