[SPR-7858] polishing formatting, grammar, etc.

This commit is contained in:
Sam Brannen 2011-07-07 11:55:00 +00:00
parent 807d612978
commit c3f9e845e0
1 changed files with 74 additions and 70 deletions

View File

@ -2,30 +2,36 @@
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<section id="beans-standard-annotations"> <section id="beans-standard-annotations">
<title>Using JSR 330 standard annotations</title> <title>Using JSR 330 Standard Annotations</title>
<para>Starting from Spring 3.0, Spring offers support for JSR-330 standard annotations (Dependency Injection). <para>Starting with Spring 3.0, Spring offers support for JSR-330 standard annotations (Dependency Injection).
Those annotations are scanned in the same way as the Spring annotations. You just need to have the relevant jars in your classpath. Those annotations are scanned in the same way as the Spring annotations. You just need to have the relevant jars in your classpath.
</para> </para>
<note> <note>
If you are using Maven, the <interfacename>javax.inject</interfacename> artifact is available on the standard Maven repository (<ulink url="http://repo1.maven.org/maven2/javax/inject/javax.inject/1/">http://repo1.maven.org/maven2/javax/inject/javax.inject/1/</ulink>). You can add the following dependency to your file pom.xml: <para>
<programlisting language="xml"> If you are using Maven, the <interfacename>javax.inject</interfacename> artifact is available
&lt;dependency&gt; in the standard Maven repository
&lt;groupId&gt;javax.inject&lt;/groupId&gt; (<ulink url="http://repo1.maven.org/maven2/javax/inject/javax.inject/1/">http://repo1.maven.org/maven2/javax/inject/javax.inject/1/</ulink>).
&lt;artifactId&gt;javax.inject&lt;/artifactId&gt; You can add the following dependency to your file pom.xml:
&lt;version&gt;1&lt;/version&gt; </para>
&lt;/dependency&gt; <programlisting language="xml">
</programlisting> &lt;dependency&gt;
&lt;groupId&gt;javax.inject&lt;/groupId&gt;
&lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
&lt;version&gt;1&lt;/version&gt;
&lt;/dependency&gt;</programlisting>
</note> </note>
<section id="beans-inject-named"> <section id="beans-inject-named">
<title>Dependency Injection with <interfacename>@Inject</interfacename> and <interfacename>@Named</interfacename></title> <title>Dependency Injection with <interfacename>@Inject</interfacename> and <interfacename>@Named</interfacename></title>
<para>
Instead of <interfacename>@Autowired</interfacename>, <interfacename>javax.inject.Inject</interfacename> may be used as follows:
<programlisting language="java"> <para>Instead of <interfacename>@Autowired</interfacename>,
import javax.inject.Inject; <interfacename>@javax.inject.Inject</interfacename> may be used as follows:
public class SimpleMovieLister {
<programlisting language="java">import javax.inject.Inject;
public class SimpleMovieLister {
private MovieFinder movieFinder; private MovieFinder movieFinder;
@ -35,15 +41,18 @@
} }
<lineannotation>// ...</lineannotation> <lineannotation>// ...</lineannotation>
}</programlisting> }</programlisting>
</para>
As for <interfacename>@Autowired</interfacename>, it is possible to use <interfacename>@Inject</interfacename> at the class-level, field-level, method-level and constructor-argument level. <para>As with <interfacename>@Autowired</interfacename>, it is possible to use <interfacename>@Inject</interfacename>
at the class-level, field-level, method-level and constructor-argument level.
If you would like to use a qualified name for the dependency that should be injected, you should use the <interfacename>@Named</interfacename> annotation as follows: If you would like to use a qualified name for the dependency that should be injected,
<programlisting language="java"> you should use the <interfacename>@Named</interfacename> annotation as follows:
import javax.inject.Inject;
import javax.inject.Named;
public class SimpleMovieLister { <programlisting language="java">import javax.inject.Inject;
import javax.inject.Named;
public class SimpleMovieLister {
private MovieFinder movieFinder; private MovieFinder movieFinder;
@ -60,13 +69,12 @@
<section id="beans-named"> <section id="beans-named">
<title><interfacename>@Named</interfacename>: a standard equivalent to the <interfacename>@Component</interfacename> annotation</title> <title><interfacename>@Named</interfacename>: a standard equivalent to the <interfacename>@Component</interfacename> annotation</title>
<para> <para>
Instead of <interfacename>@Component</interfacename>, <interfacename>javax.inject.Named</interfacename> may be used as follows: Instead of <interfacename>@Component</interfacename>, <interfacename>@javax.inject.Named</interfacename> may be used as follows:
<programlisting language="java"> <programlisting language="java">import javax.inject.Inject;
import javax.inject.Inject; import javax.inject.Named;
import javax.inject.Named;
@Named("movieListener") @Named("movieListener")
public class SimpleMovieLister { public class SimpleMovieLister {
private MovieFinder movieFinder; private MovieFinder movieFinder;
@ -77,14 +85,17 @@
<lineannotation>// ...</lineannotation> <lineannotation>// ...</lineannotation>
}</programlisting> }</programlisting>
</para> </para>
<para>
It is very common to use <interfacename>@Component</interfacename> without specifying a name for the component. <interfacename>@Named</interfacename> can be used in a similar fashion:
<programlisting language="java">
import javax.inject.Inject;
import javax.inject.Named;
@Named <para>
public class SimpleMovieLister { It is very common to use <interfacename>@Component</interfacename> without
specifying a name for the component. <interfacename>@Named</interfacename>
can be used in a similar fashion:
<programlisting language="java">import javax.inject.Inject;
import javax.inject.Named;
@Named
public class SimpleMovieLister {
private MovieFinder movieFinder; private MovieFinder movieFinder;
@ -93,39 +104,38 @@ It is very common to use <interfacename>@Component</interfacename> without speci
this.movieFinder = movieFinder; this.movieFinder = movieFinder;
} }
<lineannotation>// ...</lineannotation> <lineannotation>// ...</lineannotation>
} }</programlisting>
</programlisting>
</para> </para>
<para> <para>
When using <interfacename>@Named</interfacename>, it is possible to use component-scanning in the exact same way as when using Spring annotations: When using <interfacename>@Named</interfacename>, it is possible to use
<programlisting language="xml"> component-scanning in the exact same way as when using Spring annotations:
&lt;beans&gt;
&lt;context:component-scan base-package="org.example"/&gt; <programlisting language="xml">&lt;beans&gt;
&lt;/beans&gt; &lt;context:component-scan base-package="org.example"/&gt;
</programlisting> &lt;/beans&gt;</programlisting>
</para> </para>
</section> </section>
<section id="beans-standard-annotations-limitations"> <section id="beans-standard-annotations-limitations">
<title>Limitations of the standard approach</title> <title>Limitations of the standard approach</title>
<para>When working with standard annotations, it is important to know that some significant features are not available as shown in the table below:</para>
<para>When working with standard annotations, it is important to know that
some significant features are not available as shown in the table below:</para>
<para><table id="annotations-comparison"> <para><table id="annotations-comparison">
<title>Spring annotations vs standard annotations</title> <title>Spring annotations vs. standard annotations</title>
<tgroup cols="3"> <tgroup cols="3">
<colspec colnum="1" colwidth="0.7*" /> <colspec colnum="1" colwidth="0.7*" />
<colspec colnum="2" colwidth="0.6*" /> <colspec colnum="2" colwidth="0.6*" />
<colspec colnum="3" colwidth="1.5*" /> <colspec colnum="3" colwidth="1.5*" />
<thead> <thead>
<row> <row>
<entry>Spring</entry> <entry>Spring</entry>
<entry>javax.inject.*</entry> <entry>javax.inject.*</entry>
<entry>javax.inject restrictions / comments</entry> <entry>javax.inject restrictions / comments</entry>
</row> </row>
</thead> </thead>
@ -133,58 +143,51 @@ When using <interfacename>@Named</interfacename>, it is possible to use componen
<tbody> <tbody>
<row> <row>
<entry>@Autowired</entry> <entry>@Autowired</entry>
<entry>@Inject</entry> <entry>@Inject</entry>
<entry>@Inject has no 'required' attribute</entry> <entry>@Inject has no 'required' attribute</entry>
</row> </row>
<row> <row>
<entry>@Component</entry> <entry>@Component</entry>
<entry>@Named</entry> <entry>@Named</entry>
<entry>&mdash;</entry>
<entry></entry>
</row> </row>
<row> <row>
<entry>@Scope("singleton")</entry> <entry>@Scope("singleton")</entry>
<entry>@Singleton</entry> <entry>@Singleton</entry>
<entry> <entry>
<para> <para>
jsr-330 default scope is like Spring's <interfacename>prototype</interfacename>. However, in order to keep it consistent with Spring's general defaults, a jsr-330 bean declared in the Spring container is a <interfacename>singleton</interfacename> by default. In order to use another scope than <interfacename>singleton</interfacename>, you should use Spring's <interfacename>@Scope</interfacename> annotation. The JSR-330 default scope is like Spring's <interfacename>prototype</interfacename>.
However, in order to keep it consistent with Spring's general defaults,
a JSR-330 bean declared in the Spring container is a
<interfacename>singleton</interfacename> by default. In order to use a
scope other than <interfacename>singleton</interfacename>, you should use Spring's
<interfacename>@Scope</interfacename> annotation.
</para> </para>
<para> <para>
<interfacename>javax.inject</interfacename> also provides a <ulink url="http://download.oracle.com/javaee/6/api/javax/inject/Scope.html">@Scope</ulink> annotation. Nevertheless, this one only aims to be used for creating your own annotations. <interfacename>javax.inject</interfacename> also provides a
<ulink url="http://download.oracle.com/javaee/6/api/javax/inject/Scope.html">@Scope</ulink> annotation.
Nevertheless, this one is only intended to be used for creating your own annotations.
</para> </para>
</entry> </entry>
</row> </row>
<row> <row>
<entry>@Qualifier</entry> <entry>@Qualifier</entry>
<entry>@Named</entry> <entry>@Named</entry>
<entry>&mdash;</entry>
<entry></entry>
</row> </row>
<row> <row>
<entry>@Value</entry> <entry>@Value</entry>
<entry>&mdash;</entry>
<entry>-</entry>
<entry>no equivalent</entry> <entry>no equivalent</entry>
</row> </row>
<row> <row>
<entry>@Required</entry> <entry>@Required</entry>
<entry>&mdash;</entry>
<entry>-</entry>
<entry>no equivalent</entry> <entry>no equivalent</entry>
</row> </row>
<row> <row>
<entry>@Lazy</entry> <entry>@Lazy</entry>
<entry>&mdash;</entry>
<entry>-</entry>
<entry>no equivalent</entry> <entry>no equivalent</entry>
</row> </row>
</tbody> </tbody>
@ -193,4 +196,5 @@ When using <interfacename>@Named</interfacename>, it is possible to use componen
</para> </para>
</section> </section>
</section> </section>