Tag: annotations

Spring 4中有多个@ComponentScan?

我使用Spring 4.16和Java Annotations,我想做的事情如下: @Configuration @ComponentScan(basePackages = “com.example.business”, includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ServiceComponent.class)) @ComponentScan(basePackages = “com.example.business.framework”) public class ServicesBaseConfiguration { } 很可惜,它没有编译。 但我希望你明白我的观点。 我想要多个ComponentScans包含不同的包和filter。 我无法统一两个ComponentsScan,因为它不会从框架创建任何组件,而是那些用ServiceComponent注释的组件,我是对的吗? 你知道我怎么解决这个问题? 提前致谢

JUnit4根据自定义java注释跳过测试

我希望我的JUnit4测试可以根据我用Java创建的自定义注释来执行。 此自定义注释的目的是让JUnit4注意只有在机器的平台与注释中指定的平台匹配时才应运行测试。 说我有以下注释: public @interface Annotations { String OS(); … } 以下测试: public class myTests{ @BeforeClass public setUp() { … } @Annotations(OS=”mac”) @Test public myTest1() { … } @Annotations(OS=”windows”) @Test public myTest2() { … } @Annotation(OS=”unix”) @Test public myTest3() { … } } 如果我要在Mac机器上执行这些测试,那么只应执行myTest1(),其余部分应该被忽略。 但是,我目前仍然坚持我应该如何实现这一点。 如何让JUnit读取我的自定义注释并检查是否应该运行测试。

spring@Autowired @Lazy

我正在使用Spring注释,我想使用延迟初始化。 我遇到了一个问题,当我想从另一个类导入一个bean时,我被迫使用@Autowired似乎没有使用lazy init。 反正有没有强制这种懒惰的初始化行为? 在这个例子中,我不希望看到“正在加载父bean”,因为我只加载了对lazyParent没有依赖性的lazyParent 。 @Configuration public class ConfigParent { @Bean @Lazy public Long lazyParent(){ System.out.println(“Loading parent bean”); return 123L; } } @Configuration @Import(ConfigParent.class) public class ConfigChild { private @Autowired Long lazyParent; @Bean public Double childBean() { System.out.println(“loading child bean”); return 1.0; } @Bean @Lazy public String lazyBean() { return lazyParent+”!”; } } public […]

Java注释的默认值是否编译为字节码?

我尝试为Java字节码实现几个静态分析。 他们试图计算某个方法是否具有特定属性,例如是工厂方法。 因为这些分析很难测试,所以我决定编写一些Java代码并使用正确的属性直接注释方法。 运行分析后,很容易自动检查计算属性和带注释的属性是否相同。 MyAnnotation: @Retention(RUNTIME) @Target(METHOD) public @interface FactoryMethodProperty { FactoryMethodKeys value() default FactoryMethodKeys.NonFactoryMethod; } 示例测试代码: public class PublicFactoryMethod { private PublicFactoryMethod(){ // I’m private } @FactoryMethodProperty public static void newInstanceAfterOtherConstructorCall(){ new TransFacoryMethod(); new PublicFactoryMethod(); } @FactoryMethodProperty(FactoryMethodKeys.IsFactoryMethod) public static PublicFactoryMethod newInstance(){ return new PublicFactoryMethod(); } } 因为我的测试代码中的大多数方法都不是工厂方法,所以我将默认设置为枚举值“FactoryMethodKeys.NonFactoryMethod”。 但是当我没有将枚举值显式传递给注释时,它不会被编译为字节码。 字节码: #23 = Utf8 value #24 […]

如何在类路径中找到具有特定方法注释的所有类?

我想在Java中实现一个基于注释的初始化机制。 具体来说,我有一个我定义的注释: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Initialization { /** * If the eager initialization flag is set to true then the * initialized class will be initialized the first time it is created. * Otherwise, it will be initialized the first time it is used. * * @return true if the initialization method should be […]

为什么注释字符串值没有实现?

尽管重用了字符串常量和文字,但以下代码段打印出4个不同的哈希码。 为什么字符串值没有插入注释元素? public class Foo { @Retention(RetentionPolicy.RUNTIME) @interface Bar { String CONSTANT = “foo”; String value() default CONSTANT; } public static void main(String[] args) throws Exception { System.out.println(System.identityHashCode(Bar.CONSTANT)); System.out.println(System.identityHashCode(Foo.class.getMethod(“test1”).getAnnotation(Bar.class).value())); System.out.println(System.identityHashCode(Foo.class.getMethod(“test2”).getAnnotation(Bar.class).value())); System.out.println(System.identityHashCode(Foo.class.getMethod(“test3”).getAnnotation(Bar.class).value())); } @Bar public void test1() {} @Bar(“foo”) public void test2() {} @Bar(Bar.CONSTANT) public void test3() {} }

什么是“覆盖等价”,它与@Override有什么关系?

读取@Override注释的Javadoc ,我遇到了以下规则: 如果使用此注释类型注释方法,则编译器需要生成错误消息,除非至少满足下列条件之一: 该方法会覆盖或实现在超类型中声明的方法。 该方法的签名覆盖等效于Object中声明的任何公共方法。 我对第一点很清楚,但我不确定第二点。 “覆盖等价”是什么意思? 在这方面, Object公共方法如何特殊? 为什么这不属于第一个标准? 更新说明:这仅适用于Java 7文档。 Java 6 doc没有提到有关覆盖等价的任何内容。 为什么要改变? 更新: 在咨询了JLS( 第8.4.2节 )之后,我发现了以下对覆盖等价的解释: 方法m1的签名是方法m2的签名的子签名,如果: m2与m1具有相同的签名,或 m1的签名与m2签名的擦除( §4.6 )相同。 如果m1是m2签名或m2是m1签名,则两个方法签名m1和m2是覆盖等价的 。 据我所知,这回答了第一个问题(“它是什么意思?”)和第三个问题(“为什么第一个条件不包括这个?”)。 如果我理解正确(如果我不理解,请通知我!),只有一种情况,两种方法是覆盖等价的, 并且不属于原始问题的第一个条件。 当子类方法的签名的擦除与超类方法的签名相同时,情况就是这种情况,而不是相反。 因此,当我们在尝试“覆盖” Object类的公共方法时尝试添加类型参数时,原始问题的第二个条件才会发挥作用。 我尝试了以下简单示例来测试它,使用未使用的类型参数: public class Foo { @Override public boolean equals(Object obj) { return true; } } 当然,这个类不编译,因为该方法实际上并没有覆盖equals方法,因此与它发生冲突。 但是我仍然收到使用@Override注释的错误。 假设这是@Override使用的第二个条件的有效示例,我错了吗? 或者编译器是否生成此错误, 尽管不需要 ?

Hibernate / JPA注释 – 未知实体

几个月来一直运行良好的应用程序已经停止了几个月来已经成为其中一部分的JPA @Entity注释。 随着我的集成测试运行,我看到了几十个“ org.hibernate.MappingException: Unknown entity: com.whatever.OrderSystem ”类型错误。 我不清楚这里出了什么问题。 我没有hibernate.cfg.xml文件,因为我正在使用Hibernate实体管理器。 由于我只使用注释,因此我的实体没有.hbm.xml文件。 我的persistence.xml文件很小,并且按照预期存在于META-INF中。 我显然错过了一些东西,但不能把手指放在上面。 我正在使用hibernate-annotations 3.2.1,hibernate-entitymanager 3.2.1,persistence-api 1.0和hibernate 3.2.1。 hibernate-commons-annotations也是项目POM的一部分,但我不知道这是否相关。 是否有一个已消失的web.xml条目,或者一个意外删除的Spring配置条目?

如何在Selenium WebDriver中使用@FindBy注释

我想知道我的代码有什么问题,因为当我尝试测试我的代码时,我什么也得不到。 public class SeleniumTest { private WebDriver driver; private String nome; private String idade; @FindBy(id = “j_idt5:nome”) private WebElement inputNome; @FindBy(id = “j_idt5:idade”) private WebElement inputIdade; @BeforeClass public void criarDriver() throws InterruptedException { driver = new FirefoxDriver(); driver.get(“http://localhost:8080/SeleniumWeb/index.xhtml”); PageFactory.initElements(driver, this); } @Test(priority = 0) public void digitarTexto() { inputNome.sendKeys(“Diego”); inputIdade.sendKeys(“29”); } @Test(priority = 1) public […]

使用Spring CrudRepository的Hibernate LazyInitializationException

我总是得到例外: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.knapp.vk.domain.Business.businessCategorySet, could not initialize proxy – no Session 我不想把fetch设置为渴望。 什么是其他好的解决方案? 商业实体类: @Entity public class Business { @Id @GeneratedValue private int pk; @ManyToMany private Set businessCategorySet = new HashSet(); … } BusinessRepository接口: import org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional; @Transactional public interface BusinessRepository extends CrudRepository { } 配置: import […]