我有一个spring mvc项目设置如下:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/configuration-context.xml</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-contexts/configuration-context.xml</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
如果我在configuration-context.xml中创建一个bean并在servlet-context.xml中引用一个bean,它就无法找到它.这些是作为两个不同的背景创建的吗?为什么这种情况会发生/一般这样?
我有一个这样的超类,我期望很多类继承:
public abstract class Super {
protected Object myField; //Not used anywhere in this class
//a load more code here which does useful stuff
}
Run Code Online (Sandbox Code Playgroud)
所有这些类都需要使用一个实例myField.但是超类没有.我在某个地方做出了糟糕的设计决定吗?
如果我有一个带有两种SEPARATE方法的Spring Controller,则用以下方法注释:
@ExceptionHandler(Exception.class)
Run Code Online (Sandbox Code Playgroud)
另一个注释:
@ExceptionHandler(SubException.class)
Run Code Online (Sandbox Code Playgroud)
然后我的控制器抛出异常SubException.class,这是由两种方法处理还是仅由两种方法处理@ExceptionHandler(SubException.class)?
http://stackoverflow.com/questions/6645263/unit-testing-overridden-methods-which-call-super(这个问题有类似的措辞,但不一样)
我有类似的东西:
public class SuperClass {
private int superClassInteger = 2;
public void superClassMethod() {
superClassInteger = 5;
}
public int getSuperClassInteger() {
return superClassInteger();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的测试中我有:
public class SuperClassTest {
public void testSuperClassMethod() {
SuperClass superClass = new SuperClass();
superClass.superClassMethod();
assertEquals(5, super.getSuperClassInteger())
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个子类:
public class SubClass {
private int subClassInteger = 2;
public void subClassMethod() {
super.superClassMethod();
subClassInteger = 32;
}
public int getSuperClassInteger() {
subClassInteger;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我测试子类:
public class SubClassTest {
public void testSubClassMethod() …Run Code Online (Sandbox Code Playgroud) 阅读规范后,我得到了:
如果结构的所有字段都可比较,则它们的值可比较。如果两个结构值对应的非空白字段相等,则它们相等。
对我而言,这意味着这样做structA == structB意味着该结构中每个非空白字段的值都将fieldA == fieldB应用于该字段。那么,为什么我们需要一个深度平等的概念?因为如果该结构具有也属于该结构的字段,则所提供的信息对我而言意味着也将使用来检查这些字段的相等性==,因此肯定会触发遍历对象图的遍历吗?
在下列情况下,我遇到问题,请查看下面的内联评论:
public void exampleMethod() {
//Intuitively I would expect this to mean that test is set containing objects
//that subclass AbstractGroup
Set<? extends AbstractGroup> test;
//Yet the compiler complains here and I do not understand why?
test.add(new AnyAbstractGroupSubGroup());
//I would guess that a method call such as this at runtime
test = new HashSet<SubGroupA>()
//would mean that only objects of subgroupA can be added to the collection, but then
//what is the point in using the wildcard in the first …Run Code Online (Sandbox Code Playgroud) 我正在将异常的主体转换为字符串,然后将该异常通过电子邮件发送到java中的给定地址.我想用html格式化我的异常字符串,使其成为一种人类可读的格式,类似于它在堆栈溢出时的显示方式.我想知道Java中是否有任何库可以执行此操作?
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
t.printStackTrace(printWriter);
String body = stringWriter.toString();
//add html to body here
setMessageBody(body);
Run Code Online (Sandbox Code Playgroud)
要详细说明,我的意思是将行分开<br/>,使用不同颜色的字体显示类名称,使用不同颜色的字体显示行号.这可以通过一些正则表达式完成,但我想知道是否有一个开箱即用的库.
我正在做这个和easymock语法的噩梦:
public void foo(Class<?> clazz);
EasyMock.expects(object.foo(EasyMock.isA(???)));
Run Code Online (Sandbox Code Playgroud)
如果我的参数是String.class,我该怎么办?我最初想:
EasyMock.isA(((Class<?>)(String.class)).getClass())
然而,当我调用foo(String.class)时,我得到:
java.lang.IllegalStateException: missing behavior definition for the preceding method call:
在Android上使用OrmLite我尝试过一个简单的查询:
GenericRawResults<String[]> results = queryRaw("SELECT * FROM transaction");
Run Code Online (Sandbox Code Playgroud)
不幸的是我接受了一个例外:
near "transaction": syntax error: , while compiling: SELECT * FROM transaction
Run Code Online (Sandbox Code Playgroud)
我发现这令人困惑,在逐步完成并看到Orm通过编译语句生成的内容后,我意识到语法应该是:
GenericRawResults<String[]> results = queryRaw("SELECT * FROM `transaction`");
Run Code Online (Sandbox Code Playgroud)
我只是有点困惑,为什么这是必需的?
我可以看到scanner.go结构有一个error方法.
// A SyntaxError is a description of a JSON syntax error.
type SyntaxError struct {
msg string // description of error
Offset int64 // error occurred after reading Offset bytes
}
func (e *SyntaxError) Error() string { return e.msg }
Run Code Online (Sandbox Code Playgroud)
但编译器告诉我这个:
api/errors.go:24: impossible type switch case: err (type error) cannot have dynamic type json.SyntaxError (missing Error method) 当试图在类型上做一个开关案例
func myFunction(err error) {
switch err.(type) {
case validator.ErrorMap, json.SyntaxError:
response.WriteErrorString(http.StatusBadRequest, "400: Bad Request")
//etc
Run Code Online (Sandbox Code Playgroud)
为什么这不编译?因为struct有Error …
我正在研究遗留项目,将其转换为基于注释的MVC.我在onsubmit方法中注意到很多次,其中遵循以下模式:
public ModelAndView onSubmit(Command command) {
try {
service.doSomeBusinessLogic(command);
}
catch (ServiceException) {
//return one type of model and view here
}
//return another type of model and view here
}
Run Code Online (Sandbox Code Playgroud)
直觉上,这里的异常处理是错误的,但我不确定春天给我的替代解决方案是什么?任何想法或者这不是我想的反模式吗?
java ×7
spring ×3
go ×2
spring-mvc ×2
android ×1
collections ×1
easymock ×1
generics ×1
html ×1
java-ee ×1
junit ×1
ormlite ×1
spring-bean ×1
sqlite ×1
unit-testing ×1
wildcard ×1