标签: annotations

Spring @ContextConfiguration如何为xml放置正确的位置

在我们的项目中,我们正在编写测试以检查控制器是否返回正确的模型视图

@Test
    public void controllerReturnsModelToOverzichtpage()
    {
        ModelAndView modelView = new ModelAndView();
        KlasoverzichtController controller = new KlasoverzichtController();
        modelView = controller.showOverzicht();

        assertEquals("Klasoverzichtcontroller returns the wrong view ", modelView.getViewName(), "overzicht");
    }
Run Code Online (Sandbox Code Playgroud)

这将返回异常null.

我们现在正在配置@contextconfiguration但是我们不知道如何加载位于src\main\webapp\root\WEB-INF\root-context.xml的正确的xml

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestOverzichtSenario{
....
Run Code Online (Sandbox Code Playgroud)

此文档不够清晰,无法理解

关于如何确保contextannotation加载正确的xml的任何建议?

编辑v2

我将配置.xml文件从webINF文件夹复制到

src\main\resources\be\..a bunch of folders..\configuration\*.xml 
Run Code Online (Sandbox Code Playgroud)

并将webinf中的web.xml更改为

<param-name>contextConfigLocation</param-name>
<param-value>
            classpath*:configuration/root-context.xml
            classpath*:configuration/applicationContext-security.xml
        </param-value>
Run Code Online (Sandbox Code Playgroud)

现在得到错误

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
    org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
    org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397) …
Run Code Online (Sandbox Code Playgroud)

java spring unit-testing annotations maven

50
推荐指数
5
解决办法
18万
查看次数

为什么Android下的注释会出现性能问题(速度慢)?

我是ORMLite的主要作者,它在类上使用Java注释来构建数据库模式.我们的软件包的一个重大启动性能问题原来是在Android 1.6下调用注释方法.我看到3.0的相同行为.

我们看到以下简单的注释代码是令人难以置信的 GC密集型和真正的性能问题.在快速的Android设备上,1000次调用注释方法需要几乎一秒钟.在我的Macbook Pro上运行的相同代码可以同时执行2800万(sic)调用.我们有一个注释,里面有25个方法,我们希望每秒做50多个.

有谁知道为什么会这样,如果有任何解决方法?当然,ORMLite可以在缓存这些信息方面做些什么,但我们可以做些什么来"修复"Android下的注释?谢谢.

public void testAndroidAnnotations() throws Exception {
    Field field = Foo.class.getDeclaredField("field");
    MyAnnotation myAnnotation = field.getAnnotation(MyAnnotation.class);
    long before = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++)
        myAnnotation.foo();
    Log.i("test", "in " + (System.currentTimeMillis() - before) + "ms");
}
@Target(FIELD) @Retention(RUNTIME)
private static @interface MyAnnotation {
    String foo();
}
private static class Foo {
    @MyAnnotation(foo = "bar")
    String field;
}
Run Code Online (Sandbox Code Playgroud)

这导致以下日志输出:

I/TestRunner(  895): started: testAndroidAnnotations
D/dalvikvm(  895): GC freed 6567 objects / …
Run Code Online (Sandbox Code Playgroud)

java performance android garbage-collection annotations

50
推荐指数
3
解决办法
1万
查看次数

@size(max = value)和@min(value)@max(value)之间的差异

我想要做一些域验证

在我的对象中,我有一个整数,

现在我的问题是如果我写

@Min(SEQ_MIN_VALUE)
@Max(SEQ_MAX_VALUE)
private Integer sequence;
Run Code Online (Sandbox Code Playgroud)

 @Size(min = 1, max = NAME_MAX_LENGTH)
 private Integer sequence;
Run Code Online (Sandbox Code Playgroud)

如果是整数哪一个适合域验证.

任何人都能解释一下他们之间的区别是什么?

谢谢.

java validation spring annotations

50
推荐指数
3
解决办法
9万
查看次数

如何在给定的包中找到带注释的方法?

我有一个方法的简单标记注释(类似于Effective Java(第2版)第35项中的第一个例子):

/**
 * Marker annotation for methods that are called from installer's 
 * validation scripts etc. 
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InstallerMethod {
}
Run Code Online (Sandbox Code Playgroud)

然后,在一个给定的包(比如说com.acme.installer)中,它有几个包含20个类的子包,我想找到所有用它注释的方法.(因为我想对单元测试中所有带注释的方法进行一些检查.)

什么(如果有的话)是最简单的方法呢?最好不要添加新的第三方库或框架.

编辑:澄清,显然method.isAnnotationPresent(InstallerMethod.class)将是检查方法是否具有注释的方法 - 但是这个问题包括找到所有方法.

java annotations

49
推荐指数
2
解决办法
4万
查看次数

如何在Java中使用@inherited注释?

我没有@Inherited在Java中获得注释.如果它自动为你继承了这些方法,那么如果我需要以自己的方式实现该方法那么那么呢?

如何了解我的实施方式?

再加上它是说,如果我不想用这个,做它,而在一个老式的Java办法,我必须实现的equals(),toString()以及hashCode()该方法的Object类,也是的注释类型的方法java.lang.annotation.Annotation类.

这是为什么?

即使我不知道@Inherited注释和用于工作的程序,我也从未实现过.

请有人从头开始解释我这件事.

java inheritance annotations

49
推荐指数
1
解决办法
3万
查看次数

在Spring中@Order注释有什么用?

我看到了使用@Order注释的代码.我想知道关于Spring Security或Spring MVC的这个注释有什么用处.

这是一个例子:

@Order(1)
public class StatelessAuthenticationSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private TokenAuthenticationService tokenAuthenticationService;

}
Run Code Online (Sandbox Code Playgroud)

如果我们不使用这个注释,上面提到的类的顺序会发生什么?

spring annotations spring-security

49
推荐指数
3
解决办法
4万
查看次数

如何在注释中使用数组常量

我想将常量用于注释值.

interface Client {

    @Retention(RUNTIME)
    @Target(METHOD)
    @interface SomeAnnotation { String[] values(); }

    interface Info {
        String A = "a";
        String B = "b";
        String[] AB = new String[] { A, B };
    }

    @SomeAnnotation(values = { Info.A, Info.B })
    void works();

    @SomeAnnotation(values = Info.AB)
    void doesNotWork();
}
Run Code Online (Sandbox Code Playgroud)

常数Info.AInfo.B可以在注释中使用,但不是数组Info.AB,因为它必须是在这个地方数组初始化.注释值仅限于可以内联到类的字节代码中的值.这对于数组常量是不可能的,因为它必须在Info加载时构造.这个问题有解决方法吗?

java annotations

48
推荐指数
3
解决办法
3万
查看次数

来自javax.validation.constraints的注释无效

什么样的配置,需要使用注释来自javax.validation.constraints@Size,@NotNull等等?这是我的代码:

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Person {
      @NotNull
      private String id;

      @Size(max = 3)
      private String name;

      private int age;

      public Person(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
      }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在另一个类中使用它时,验证不起作用(即创建对象时没有错误):

Person P = new Person(null, "Richard3", 8229));
Run Code Online (Sandbox Code Playgroud)

为什么这不适用于idname?我还需要做什么?

java spring annotations constraints bean-validation

48
推荐指数
7
解决办法
9万
查看次数

使用Hibernate注释的文本字段

我在设置字符串的类型时遇到了问题

public void setTextDesc(String textDesc) {
    this.textDesc = textDesc;
}

@Column(name="DESC")
@Lob
public String getTextDesc() {
    return textDesc;
}
Run Code Online (Sandbox Code Playgroud)

它没有用,我检查了mysql架构,它仍然是varchar(255),我也尝试过,

@Column(name="DESC", length="9000")
Run Code Online (Sandbox Code Playgroud)

要么

@Column(name="DESC")
@Type(type="text")
Run Code Online (Sandbox Code Playgroud)

我正在努力使类型成为TEXT,任何想法都将受到赞赏!

annotations hibernate blob

47
推荐指数
1
解决办法
7万
查看次数

Android Studio中@override的含义

我是Android Studio的新手,我想知道@OverrideAndroid Studio中语句的用途.

java android overriding annotations

47
推荐指数
3
解决办法
5万
查看次数