标签: annotations

Annotation可以实现接口吗?

注释中是否有可能实现接口?就像是:

public @interface NotNull implements LevelInterface  {
    ValidationLevel level();
};
Run Code Online (Sandbox Code Playgroud)

java annotations interface

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

如何正确地使用vim indent java注释?

在使用注释缩进java代码时,vim坚持这样缩进:

@Test
    public void ...
Run Code Online (Sandbox Code Playgroud)

我希望注释与方法定义在同一列中,但我似乎找不到告诉vim这样做的方法,除了可能使用缩进表达式但是我不确定我是否可以将它与常规的.

编辑:文件类型插件已经打开我只是对缩进插件有点困惑.接受的答案可能有点hackish但也适合我.

java vim annotations

20
推荐指数
2
解决办法
4084
查看次数

与xml配置文件相比,注释(非编译器)的优缺点是什么?

当我查看像Hibernate,JPA或Spring这样的Java框架时,我通常可以通过xml文件进行配置或直接在我的类中添加注释.

我一直在问自己应该走哪条路.

当我使用注释时,我将类和它的配置放在一起,但是使用xml我可以更好地了解我的系统,因为我可以看到所有的类配置.

注释也以某种方式编译,我猜它应该比解析xml更快,但另一方面,如果我想更改我的配置,我必须重新编译它而不是只更改xml文件(可能会变得更多方便客户方的生产环境).

因此,在查看我的观点时,我倾向于采用xml方式.但是在查看论坛或教程时,通常会使用注释.

你有什么优缺点?

java annotations

20
推荐指数
1
解决办法
4539
查看次数

在多对多单向映射中保留枚举集

我正在使用带有注释的Hibernate 3.5.2-FINAL来指定我的持久性映射.我正在努力建立应用程序和一组平台之间的关系.每个应用程序都可用于一组平台.

从我所做的所有阅读和搜索中,我认为我需要将平台枚举类保持为实体,并使用连接表来表示多对多关系.我希望关系在对象级别是单向的,也就是说,我希望能够获得给定应用程序的平台列表,但我不需要找出给定平台的应用程序列表.

这是我的简化模型类:

@Entity
@Table(name = "TBL_PLATFORM")
public enum Platform {
    Windows,
    Mac,
    Linux,
    Other;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id = null;

    @Column(name = "NAME")
    private String name;

    private DevicePlatform() {
        this.name = toString();
    }

    // Setters and getters for id and name...
}

@Entity
@Table(name = "TBL_APP")
public class Application extends AbstractEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name = "NAME")
    protected String _name;

    @ManyToMany(cascade = javax.persistence.CascadeType.ALL)
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    @JoinTable(name = "TBL_APP_PLATFORM", …
Run Code Online (Sandbox Code Playgroud)

java persistence annotations hibernate jpa

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

20
推荐指数
1
解决办法
7987
查看次数

如何对Spring MVC带注释的控制器进行单元测试?

我正在关注Spring 2.5教程并尝试将代码/设置更新到Spring 3.0.

Spring 2.5中我有HelloController(供参考):

public class HelloController implements Controller {
    protected final Log logger = LogFactory.getLog(getClass());
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("Returning hello view");
        return new ModelAndView("hello.jsp");
    }
}
Run Code Online (Sandbox Code Playgroud)

以及HelloController的JUnit测试(供参考):

public class HelloControllerTests extends TestCase {
    public void testHandleRequestView() throws Exception{
        HelloController controller = new HelloController();
        ModelAndView modelAndView = controller.handleRequest(null, null);
        assertEquals("hello", modelAndView.getViewName());
    }
}
Run Code Online (Sandbox Code Playgroud)

但现在我将控制器更新到Spring 3.0,它现在使用注释(我还添加了一条消息):

@Controller
public class HelloController {
    protected final …
Run Code Online (Sandbox Code Playgroud)

java junit spring annotations

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

枚举Java枚举值的枚举值

Java允许enum作为注释值的值.如何enumenum注释值定义一种通用默认值?

我考虑过以下内容,但不会编译:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public <T extends Enum<T>> @interface MyAnnotation<T> {

    T defaultValue();

}
Run Code Online (Sandbox Code Playgroud)

这个问题是否有解决方案?

BOUNTY

似乎没有直接解决这个Java角落案例.所以,我正在开始寻找最优雅的解决方案来解决这个问题.

理想的解决方案应该非常符合下列条件:

  1. 所有枚举都可以重复使用一个注释
  2. 从注释实例中检索作为枚举的默认枚举值的最小努力/复杂性

最好的解决方案

通过沙丘:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotation {

    // By not specifying default,
    // we force the user to specify values
    Class<? extends Enum<?>> enumClazz();
    String defaultValue();

}

...

public enum MyEnumType {
    A, B, D, Q;
}

...

// Usage
@MyAnnotation(enumClazz=MyEnumType.class, defaultValue="A"); 
private MyEnumType myEnumField;
Run Code Online (Sandbox Code Playgroud)

当然,我们不能强制用户在编译时指定有效的默认值.但是,任何注释预处理都可以验证这一点valueOf().

改进 …

java enums annotations default-value

20
推荐指数
2
解决办法
1万
查看次数

类是否有可能继承超类的注释

我正在使用Spring Framework事务注释进行事务管理,我有一个抽象类注释@Transactional,如下所示:

package org.tts.maqraa.service;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Parts of this code have been copied from JARVANA site.
 * 
 * @author Younis alomoush
 * 
 */
@Transactional(propagation=Propagation.REQUIRED)
public abstract class AbstractMaqraaService implements MaqraaService {


    private Logger logger = LoggerFactory.getLogger(this.getClass());


    private int defaultMaxResults = DEFAULT_MAX_RESULTS;

    @PersistenceContext(type=PersistenceContextType.TRANSACTION)
    private EntityManager em;
    /**
     * The {@link EntityManager} which is …
Run Code Online (Sandbox Code Playgroud)

java inheritance annotations class

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

Spring MVC中有多个@PathVariable

遗憾的是找不到答案,所以希望有人可以提供帮助.

在Spring MVC 3.1.0中,这是我的方法:

@RequestMapping(value = "/{app}/conf/{fnm}", method=RequestMethod.GET)
public ResponseEntity<?> getConf(@PathVariable String app, @PathVariable String fnm) {
    log.debug("AppName:" + app);
    log.debug("fName:" + fnm);
            ...
            return ...
    }
Run Code Online (Sandbox Code Playgroud)

我在网上看过一些例子,看起来理论上有多个@PathVariables没问题.

但是,当我这样做时,"app"和"fnm"都包含相同的值(这是分配给"app"的任何值).

真的很感激有人可能对我出错的地方有任何见解?

谢谢!

java spring annotations path-variables spring-mvc

20
推荐指数
1
解决办法
5万
查看次数

注释处理器似乎打破了Java泛型

背景

我试图使用Annotation Processors来生成特定Factory接口的实现.这些接口看起来如下:

public interface ViewFactory<T extends View> {

    <S extends Presenter<T>> T create(S presenter);

}
Run Code Online (Sandbox Code Playgroud)

public interface PresenterFactory<T extends View> {

    <S extends Presenter<T>> S create();

}
Run Code Online (Sandbox Code Playgroud)

Annotation Processor正在执行正确的操作,并为每个匹配的类生成一个工厂,该工厂使用相应的注释进行注释.

问题

注释处理器的输出如下:

public final class TestViewImplFactory implements ViewFactory {

    public final TestView create(TestPresenter presenter) {
        return new TestViewImpl(presenter);
    }
}
Run Code Online (Sandbox Code Playgroud)

和相应的其他类:

public final class TestPresenterImplFactory implements PresenterFactory {

    public final TestPresenter create() {
        return new TestPresenterImpl();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,TestViewImplFactory无法编译.错误消息是:

"类'TestViewImplFactory'必须声明为abstract或在'ViewFactory'中实现抽象方法create(S)"

Java说,以下是正确的:

@Override
public View create(Presenter presenter) {
    return new …
Run Code Online (Sandbox Code Playgroud)

java generics annotations annotation-processing

20
推荐指数
1
解决办法
763
查看次数