beans-standard-annotations.xml 7.0 KB
Newer Older
M
Michael Isvy 已提交
1
<?xml version="1.0" encoding="UTF-8"?>
2 3 4 5
<section xmlns="http://docbook.org/ns/docbook" version="5.0"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xi="http://www.w3.org/2001/XInclude"
    xml:id="beans-standard-annotations">
6
  <title>Using JSR 330 Standard Annotations</title>
M
Michael Isvy 已提交
7
  
8
  <para>Starting with Spring 3.0, Spring offers support for JSR-330 standard annotations (Dependency Injection).
M
Michael Isvy 已提交
9 10
  Those annotations are scanned in the same way as the Spring annotations. You just need to have the relevant jars in your classpath.
  </para>
11

M
Michael Isvy 已提交
12
  <note>
13 14 15 16 17 18 19 20 21 22 23 24
  	<para>
      If you are using Maven, the <interfacename>javax.inject</interfacename> artifact is available
      in 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">
&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>
M
Michael Isvy 已提交
25 26 27 28
  </note>
  
  <section id="beans-inject-named">
    <title>Dependency Injection with <interfacename>@Inject</interfacename> and <interfacename>@Named</interfacename></title>
29 30 31 32 33 34 35

    <para>Instead of <interfacename>@Autowired</interfacename>,
      <interfacename>@javax.inject.Inject</interfacename> may be used as follows:

    <programlisting language="java">import javax.inject.Inject;

public class SimpleMovieLister {
M
Michael Isvy 已提交
36 37 38 39 40 41 42 43 44

  private MovieFinder movieFinder;

  @Inject
  public void setMovieFinder(MovieFinder movieFinder) {
      this.movieFinder = movieFinder;
  }
  <lineannotation>// ...</lineannotation>
}</programlisting>
45
    </para>
M
Michael Isvy 已提交
46

47 48
    <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.
M
Michael Isvy 已提交
49

50 51 52 53 54 55 56
	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:

    <programlisting language="java">import javax.inject.Inject;
import javax.inject.Named;

public class SimpleMovieLister {
M
Michael Isvy 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  private MovieFinder movieFinder;

  @Inject
  public void setMovieFinder(@Named("main") MovieFinder movieFinder) {
      this.movieFinder = movieFinder;
  }
  <lineannotation>// ...</lineannotation>
}</programlisting>	
	
    </para>
  </section>

<section id="beans-named">
    <title><interfacename>@Named</interfacename>: a standard equivalent to the <interfacename>@Component</interfacename> annotation</title>
    <para>
73 74 75
    Instead of <interfacename>@Component</interfacename>, <interfacename>@javax.inject.Named</interfacename> may be used as follows:
        <programlisting language="java">import javax.inject.Inject;
import javax.inject.Named;
M
Michael Isvy 已提交
76
  
77 78
@Named("movieListener")
public class SimpleMovieLister {
M
Michael Isvy 已提交
79 80 81 82 83 84 85 86 87 88

  private MovieFinder movieFinder;

  @Inject
  public void setMovieFinder(MovieFinder movieFinder) {
      this.movieFinder = movieFinder;
  }
  <lineannotation>// ...</lineannotation>
}</programlisting>	
</para>
89

M
Michael Isvy 已提交
90
<para>
91 92 93 94 95 96
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;
M
Michael Isvy 已提交
97
  
98 99
@Named
public class SimpleMovieLister {
M
Michael Isvy 已提交
100 101 102 103 104 105 106 107

  private MovieFinder movieFinder;

  @Inject
  public void setMovieFinder(MovieFinder movieFinder) {
      this.movieFinder = movieFinder;
  }
  <lineannotation>// ...</lineannotation>
108
}</programlisting>
M
Michael Isvy 已提交
109
</para>
110

M
Michael Isvy 已提交
111
<para>
112 113 114 115 116 117
When using <interfacename>@Named</interfacename>, it is possible to use
component-scanning in the exact same way as when using Spring annotations:

    <programlisting language="xml">&lt;beans&gt;
    &lt;context:component-scan base-package="org.example"/&gt;
&lt;/beans&gt;</programlisting>	
M
Michael Isvy 已提交
118 119
</para>
</section>
120

M
Michael Isvy 已提交
121 122
<section id="beans-standard-annotations-limitations">
    <title>Limitations of the standard approach</title>
123 124 125

	<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>
M
Michael Isvy 已提交
126 127
	
	 <para><table id="annotations-comparison">
128
          <title>Spring annotations vs. standard annotations</title>
M
Michael Isvy 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

          <tgroup cols="3">

            <colspec colnum="1" colwidth="0.7*" />
            <colspec colnum="2" colwidth="0.6*" />
            <colspec colnum="3" colwidth="1.5*" />

            <thead>
              <row>
                <entry>Spring</entry>
                <entry>javax.inject.*</entry>
                <entry>javax.inject restrictions / comments</entry>
              </row>
            </thead>

            <tbody>
              <row>
                <entry>@Autowired</entry>
                <entry>@Inject</entry>
                <entry>@Inject has no 'required' attribute</entry>
              </row>
              <row>
                <entry>@Component</entry>
                <entry>@Named</entry>
153
                <entry>&mdash;</entry>
M
Michael Isvy 已提交
154 155 156 157 158 159
              </row>
              <row>
                <entry>@Scope("singleton")</entry>
                <entry>@Singleton</entry>
                <entry>
                	<para>
160 161 162 163 164 165
                		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.
M
Michael Isvy 已提交
166 167
                	</para>
                	<para>
168 169 170
                		<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.
M
Michael Isvy 已提交
171 172 173 174 175 176
                	</para>
                </entry>
              </row>              
              <row>
                <entry>@Qualifier</entry>
                <entry>@Named</entry>
177
                <entry>&mdash;</entry>
M
Michael Isvy 已提交
178 179 180
              </row>  
               <row>
                <entry>@Value</entry>
181
                <entry>&mdash;</entry>
M
Michael Isvy 已提交
182 183 184 185
                <entry>no equivalent</entry>
              </row> 
               <row>
                <entry>@Required</entry>
186
                <entry>&mdash;</entry>
M
Michael Isvy 已提交
187 188 189 190
                <entry>no equivalent</entry>
              </row> 
               <row>
                <entry>@Lazy</entry>
191
                <entry>&mdash;</entry>
M
Michael Isvy 已提交
192 193 194 195 196 197 198 199
                <entry>no equivalent</entry>
              </row> 
            </tbody>     
          </tgroup>
         </table>
        </para>
	
	</section>
200

201
</section>