Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring dependency injection, to use @Named or @Resource?
    primarykey
    data
    text
    <p>There are two separate annotations to perform dependency injection by name in Spring, <code>javax.annotation.Resource</code> and <code>javax.inject.Named</code>. The <a href="http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers" rel="noreferrer">documentation at Spring</a> indicates <code>@Resource</code> should be used for injection by name:</p> <blockquote> <p>If you intend to express annotation-driven injection by name, do not primarily use @Autowired, even if is technically capable of referring to a bean name through @Qualifier values. Instead, use the JSR-250 @Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.</p> </blockquote> <p>The above is a bit confusing, as Spring is only advocating <code>@Resource</code> instead of <code>@Autowired</code> combined with <code>@Qualifer</code>. There is no mention of <code>@Named</code> until <a href="http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-standard-annotations" rel="noreferrer">later in the documentation</a>.</p> <p>JSR-250 defines <code>@Resource</code>, whereas JSR-330 defines <code>@Inject</code> and <code>@Named</code>. I know they can be mixed-and-matched within Spring fairly easily. Which JSR to use? </p> <p>It seems like portability with Guice and CDI would be nice, and hence to use the JSR-330 annotations. On the other hand, the documentation also points out at a couple of <a href="http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-standard-annotations-limitations" rel="noreferrer">limitations</a> within Spring when using JSR-330 annotations. </p> <p>What is the best practice (if there is one) for annotation injection-by-name?</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload