什么是javax.inject.Named注释应该用于? [英] What is javax.inject.Named annotation supposed to be used for?

查看:333
本文介绍了什么是javax.inject.Named注释应该用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图理解 javax.inject 包,我不清楚 javax.inject.Named 注释应该用于。 Javadoc没有解释它背后的想法。



Javadoc在 http://download.oracle.com/javaee/6/api/javax/inject/Named.html



我正在使用 Spring 3.0 来编写一些示例程序,通过在bean上放置 @Named ,似乎将其添加到bean工厂,但Javadoc描述很轻不能说这是标准行为还是Spring的具体行为。



我的问题是:


  1. @Named @Qualifier

  2. 之间有什么区别?你应该如何告诉运行时系统一个类应该在其他类中注入什么注释是什么? Spring中的$ code> @Component 相当于

更新1 有关于 @Named @Qualifier 的优秀说明,关于 @Named @Qualifier http://java.dzone.com/articles/java-ee6-cdi-named-components 谢谢@xmedeko链接到下面的评论。

解决方案

使用@Named区分同一范围内绑定的同一类型的不同对象。

  @Named(maxWaitTime)
public long maxWaitTimeMs;

@Named(minWaitTime)
public long minWaitTimeMs;

如果没有@Named限定词,注入器将不知道要绑定到哪个变量的长度。 p>

如果要创建类似@Named的注释,请在创建时使用@Qualifier注释。如果你看@Named,它本身用@Qualifier注释。


I am trying to understand the javax.inject package and I am not clear what the javax.inject.Named annotation is supposed to be used for. The Javadoc does not explain the the idea behind it.

Javadoc is at http://download.oracle.com/javaee/6/api/javax/inject/Named.html

I am using Spring 3.0 to write some sample programs, by putting @Named on a bean it seems to add it to the bean factory but the Javadoc description is so light I can't tell if that is the standard behavior or Spring specific behavior.

My questions are:

  1. What is the difference between @Named and @Qualifier
  2. How are you supposed to tell the Runtime system a class should be injectable in other classes what's the annotation for that? The equivalent of @Component in Spring?

Update 1 there is an excellent explanation of @Named and @Qualifier at Nice article about @Named and @Qualifier http://java.dzone.com/articles/java-ee6-cdi-named-components thanks @xmedeko for linking to it the comment below.

解决方案

Use @Named to differentiate between different objects of the same type bound in the same scope.

@Named("maxWaitTime")
public long maxWaitTimeMs;

@Named("minWaitTime")
public long minWaitTimeMs;

Without the @Named qualifier, the injector would not know which long to bind to which variable.

If you want to create annotations that act like @Named, use the @Qualifier annotation when creating them. If you look at @Named, it is itself annotated with @Qualifier.

这篇关于什么是javax.inject.Named注释应该用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆