@ -9,8 +9,7 @@
<para > This chapter covers the Spring Framework implementation of the
<para > This chapter covers the Spring Framework implementation of the
Inversion of Control (IoC) <footnote >
Inversion of Control (IoC) <footnote >
<para > See the section entitled <xref
<para > See <xref linkend= "background-ioc" /> </para>
linkend="background-ioc" /></para>
</footnote> principle. IoC is also known as <emphasis > dependency
</footnote> principle. IoC is also known as <emphasis > dependency
injection</emphasis> (DI). It is a process whereby objects define their
injection</emphasis> (DI). It is a process whereby objects define their
dependencies, that is, the other objects they work with, only through
dependencies, that is, the other objects they work with, only through
@ -216,8 +215,8 @@ The footnote should x-ref to first section in that chapter but I can't find the
metadata from a variety of external resources such as the local file
metadata from a variety of external resources such as the local file
system, from the Java <literal > CLASSPATH</literal> , and so on.</para>
system, from the Java <literal > CLASSPATH</literal> , and so on.</para>
<programlisting language= "java" > ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml",
<programlisting language= "java" > ApplicationContext context =
"daos.xml"});</programlisting>
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});</programlisting>
<note >
<note >
<para > After you learn about Spring's IoC container, you may want to
<para > After you learn about Spring's IoC container, you may want to
@ -233,7 +232,7 @@ The footnote should x-ref to first section in that chapter but I can't find the
<para > The following example shows the service layer objects
<para > The following example shows the service layer objects
<literal > (services.xml)</literal> configuration file:</para>
<literal > (services.xml)</literal> configuration file:</para>
<programlisting > < ?xml version="1.0" encoding="UTF-8"?>
<programlisting language= "xml" > < ?xml version="1.0" encoding="UTF-8"?>
< beans xmlns="http://www.springframework.org/schema/beans"
< beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
@ -256,13 +255,14 @@ The footnote should x-ref to first section in that chapter but I can't find the
<para > The following example shows the data access objects
<para > The following example shows the data access objects
<literal > daos.xml</literal> ) file:</para>
<literal > daos.xml</literal> ) file:</para>
<programlisting > < ?xml version="1.0" encoding="UTF-8"?>
<programlisting language= "xml" > < ?xml version="1.0" encoding="UTF-8"?>
< beans xmlns="http://www.springframework.org/schema/beans"
< beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
< bean id="accountDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapAccountDao">
< bean id="accountDao"
class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapAccountDao">
< !-- additional collaborators and configuration for this bean go here -->
< !-- additional collaborators and configuration for this bean go here -->
< /bean>
< /bean>
@ -361,7 +361,8 @@ The footnote should x-ref to first section in that chapter but I can't find the
to read bean definitions and access them as follows:</para>
to read bean definitions and access them as follows:</para>
<programlisting language= "java" > // create and configure beans
<programlisting language= "java" > // create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
// retrieve configured instance
// retrieve configured instance
PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);
PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);
@ -1076,7 +1077,7 @@ public class ExampleBean {
properties themselves are not set until the bean <emphasis > is actually
properties themselves are not set until the bean <emphasis > is actually
created</emphasis> . Beans that are singleton-scoped and set to be
created</emphasis> . Beans that are singleton-scoped and set to be
pre-instantiated (the default) are created when the container is
pre-instantiated (the default) are created when the container is
created. Scopes are defined in the section <xref
created. Scopes are defined in <xref
linkend="beans-factory-scopes" /> Otherwise, the bean is created only
linkend="beans-factory-scopes" /> Otherwise, the bean is created only
when it is requested. Creation of a bean potentially causes a graph of
when it is requested. Creation of a bean potentially causes a graph of
beans to be created, as the bean's dependencies and its dependencies'
beans to be created, as the bean's dependencies and its dependencies'
@ -1300,19 +1301,19 @@ public class ExampleBean {
linkend="beans-p-namespace">p-namespace</link> for even more succinct
linkend="beans-p-namespace">p-namespace</link> for even more succinct
XML configuration.</para>
XML configuration.</para>
<programlisting > < beans xmlns="http://www.springframework.org/schema/beans"
<programlisting language= "xml" > < beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
< bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
< bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/mydb"
p:url="jdbc:mysql://localhost:3306/mydb"
p:username="root"
p:username="root"
p:password="masterkaoli"/>
p:password="masterkaoli"/>
< /beans>
< /beans>
</programlisting>
</programlisting>
@ -1329,8 +1330,9 @@ public class ExampleBean {
<para > You can also configure a
<para > You can also configure a
<classname > java.util.Properties</classname> instance as:</para>
<classname > java.util.Properties</classname> instance as:</para>
<programlisting language= "xml" > < bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<programlisting language= "xml" > < bean id="mappings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<lineannotation > < !-- typed as a <classname > java.util.Properties</classname> --> </lineannotation>
<lineannotation > < !-- typed as a <classname > java.util.Properties</classname> --> </lineannotation>
< property name="properties">
< property name="properties">
< value>
< value>
@ -1395,7 +1397,7 @@ public class ExampleBean {
time.</para>
time.</para>
<programlisting language= "xml" > < property name="targetName">
<programlisting language= "xml" > < property name="targetName">
<lineannotation > < !-- a bean with an id of '<literal > theTargetBean</literal> ' must exist; otherwise an XML exception will be thrown --> </lineannotation>
<lineannotation > < !-- a bean with id '<literal > theTargetBean</literal> ' must exist; otherwise an exception will be thrown --> </lineannotation>
< idref local="theTargetBean"/>
< idref local="theTargetBean"/>
< /property> </programlisting>
< /property> </programlisting>
@ -1465,14 +1467,12 @@ public class ExampleBean {
< /bean> </programlisting>
< /bean> </programlisting>
<programlisting language= "xml" > <lineannotation > < !-- in the child (descendant) context --> </lineannotation>
<programlisting language= "xml" > <lineannotation > < !-- in the child (descendant) context --> </lineannotation>
< bean id="accountService" <lineannotation > < -- notice that the name of this bean is the <emphasis
< bean id="accountService" <lineannotation > < -- bean name is the same as the parent bean --> </lineannotation>
role="bold">same</emphasis> as the name of the <literal > parent</literal> bean</lineannotation>
class="org.springframework.aop.framework.ProxyFactoryBean">
class="org.springframework.aop.framework.ProxyFactoryBean">
< property name="target">
< property name="target">
< ref parent="accountService"/> <lineannotation > < -- notice how we refer to the <emphasis
< ref parent="accountService"/> <lineannotation > < !-- notice how we refer to the parent bean --> </lineannotation>
role="bold">parent</emphasis> bean</lineannotation>
< /property>
< /property>
<lineannotation > < !-- insert other configuration and dependencies as required as here --> </lineannotation>
<lineannotation > < !-- insert other configuration and dependencies as required here --> </lineannotation>
< /bean> </programlisting>
< /bean> </programlisting>
</section>
</section>
@ -2231,8 +2231,8 @@ support=support@example.co.uk</programlisting>
</table>
</table>
<para > If you use Java 5 and thus have access to source-level
<para > If you use Java 5 and thus have access to source-level
annotations, you may find <xref
annotations, you may find <literal > < xref
linkend="metadata-annotations-required" /> to be of interest.</para>
linkend="metadata-annotations-required" /></literal> to be of interest.</para>
</section>
</section>
<section id= "beans-factory-method-injection" >
<section id= "beans-factory-method-injection" >
@ -2278,12 +2278,13 @@ public class CommandManager implements ApplicationContextAware {
return command.execute();
return command.execute();
}
}
<lineannotation > // the <interfacename > Command</interfacename> returned here could be an implementation that executes asynchronously, or whatever</lineannotation>
protected Command createCommand() {
protected Command createCommand() {
return this.applicationContext.getBean("command", Command.class); <lineannotation > // notice the Spring API dependency</lineannotation>
<lineannotation > // notice the Spring API dependency!</lineannotation>
return this.applicationContext.getBean("command", Command.class);
}
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
this.applicationContext = applicationContext;
}
}
}</programlisting>
}</programlisting>
@ -2565,6 +2566,14 @@ public class ReplacementComputeValue implements MethodReplacer {
</tbody>
</tbody>
</tgroup>
</tgroup>
</table>
</table>
<note >
<title > Thread-scoped beans</title>
<para > As of Spring 3.0, a <emphasis > thread scope</emphasis> is available, but is
not registered by default. For more information, see the documentation for
<ulink url= "http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/support/SimpleThreadScope.html" > SimpleThreadScope</ulink> .
For instructions on how to register this or any other custom scope, see
<xref linkend= "beans-factory-scopes-custom-using" /> .</para>
</note>
<section id= "beans-factory-scopes-singleton" >
<section id= "beans-factory-scopes-singleton" >
<title > The singleton scope</title>
<title > The singleton scope</title>
@ -2609,11 +2618,8 @@ public class ReplacementComputeValue implements MethodReplacer {
<programlisting language= "xml" > < bean id="accountService" class="com.foo.DefaultAccountService"/>
<programlisting language= "xml" > < bean id="accountService" class="com.foo.DefaultAccountService"/>
<lineannotation > < !-- the following is equivalent, though redundant (singleton scope is the default); using <literal > spring-beans-2.0.dtd</literal> --> </lineannotation>
<lineannotation > < !-- the following is equivalent, though redundant (singleton scope is the default) --> </lineannotation>
< bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
< bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/> </programlisting>
<lineannotation > < !-- the following is equivalent and preserved for backward compatibility in <literal > spring-beans.dtd</literal> --> </lineannotation>
< bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/> </programlisting>
</section>
</section>
<section id= "beans-factory-scopes-prototype" >
<section id= "beans-factory-scopes-prototype" >
@ -2648,10 +2654,7 @@ public class ReplacementComputeValue implements MethodReplacer {
<para > The following example defines a bean as a prototype in XML:</para>
<para > The following example defines a bean as a prototype in XML:</para>
<programlisting language= "xml" > <lineannotation > < !-- using <literal > spring-beans-2.0.dtd</literal> --> </lineannotation>
<programlisting language= "xml" > <lineannotation > < !-- using <literal > spring-beans-2.0.dtd</literal> --> </lineannotation>
< bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
< bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/> </programlisting>
<lineannotation > < !-- the following is equivalent and preserved for backward compatibility in <literal > spring-beans.dtd</literal> --> </lineannotation>
< bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/> </programlisting>
<para > In contrast to the other scopes, Spring does not manage the
<para > In contrast to the other scopes, Spring does not manage the
complete lifecycle of a prototype bean: the container instantiates,
complete lifecycle of a prototype bean: the container instantiates,
@ -2694,32 +2697,6 @@ public class ReplacementComputeValue implements MethodReplacer {
and injecting its dependencies. If you need a new instance of a
and injecting its dependencies. If you need a new instance of a
prototype bean at runtime more than once, see <xref
prototype bean at runtime more than once, see <xref
linkend="beans-factory-method-injection" /></para>
linkend="beans-factory-method-injection" /></para>
<note >
<title > Backwards compatibility and specifying the lifecycle scope in
XML</title>
<para > If you reference the <filename > spring-beans.dtd</filename> DTD
in a bean definition file, and you are explicit about the lifecycle
scope of your beans, you must use the <literal > singleton</literal>
attribute to express the lifecycle scope. The <link
linkend="beans-factory-scopes-singleton">singleton lifecycle
scope</link> is the default. If you reference the
<filename > spring-beans-2.0.dtd</filename> DTD or the Spring 2.0 XSD
schema, you must use the <literal > scope</literal> attribute, because
the <literal > singleton</literal> attribute was removed from the
definition of the new DTD and XSD files in favor of the
<literal > scope</literal> attribute.</para>
<para > This means that if you use the <literal > singleton</literal>
attribute in an XML bean definition, you <emphasis > must</emphasis>
reference the <filename > spring-beans.dtd</filename> DTD <emphasis > in
that file</emphasis> . If you use the <literal > scope</literal>
attribute, you <emphasis > must</emphasis> reference either the
<filename > spring-beans-2.0.dtd</filename> DTD or the
<filename > spring-beans-3.0.xsd</filename> XSD <emphasis > in that
file</emphasis> .</para>
</note>
</section>
</section>
<section id= "beans-factory-scopes-other" >
<section id= "beans-factory-scopes-other" >
@ -2766,7 +2743,9 @@ public class ReplacementComputeValue implements MethodReplacer {
<programlisting language= "xml" > < web-app>
<programlisting language= "xml" > < web-app>
...
...
< listener>
< listener>
< listener-class> org.springframework.web.context.request.RequestContextListener< /listener-class>
< listener-class>
org.springframework.web.context.request.RequestContextListener
< /listener-class>
< /listener>
< /listener>
...
...
< /web-app> </programlisting>
< /web-app> </programlisting>
@ -3131,18 +3110,21 @@ public class ReplacementComputeValue implements MethodReplacer {
<para > Suppose that you write your custom
<para > Suppose that you write your custom
<interfacename > Scope</interfacename> implementation, and then register
<interfacename > Scope</interfacename> implementation, and then register
it as follows:</para>
it as below.</para>
<note >
<para > The example below uses <literal > SimpleThreadScope</literal>
which is included with Spring, but not registered by default. The instructions
would be the same for your own custom <literal > Scope</literal> implementations.</para>
</note>
<programlisting language= "java" > <lineannotation > // note: the <classname > ThreadScope</classname> class does <emphasis
<programlisting language= "java" >
role="bold">not</emphasis> ship with the Spring Framework</lineannotation>
Scope threadScope = new SimpleThreadScope();
Scope customScope = new ThreadScope();
beanFactory.registerScope("<emphasis role= "bold" > thread</emphasis> ", threadScope);</programlisting>
beanFactory.registerScope("<emphasis role= "bold" > thread</emphasis> ", customScope);</programlisting>
<para > You then create bean definitions that adhere to the scoping
<para > You then create bean definitions that adhere to the scoping
rules of your custom <interfacename > Scope</interfacename> :</para>
rules of your custom <interfacename > Scope</interfacename> :</para>
<programlisting language= "xml" > < bean id="..." class="..." <emphasis
<programlisting language= "xml" > < bean id="..." class="..." scope="thread"> </programlisting>
role="bold">scope="thread"</emphasis> /> </programlisting>
<para > With a custom <interfacename > Scope</interfacename>
<para > With a custom <interfacename > Scope</interfacename>
implementation, you are not limited to programmatic registration of
implementation, you are not limited to programmatic registration of
@ -3163,13 +3145,13 @@ beanFactory.registerScope("<emphasis role="bold">thread</emphasis>", customScope
< property name="scopes">
< property name="scopes">
< map> <emphasis role= "bold" >
< map> <emphasis role= "bold" >
< entry key="thread">
< entry key="thread">
< bean class="com.foo. ThreadScope"/>
< bean class="org.springframework.context.support.Simple ThreadScope"/>
< /entry> </emphasis>
< /entry> </emphasis>
< /map>
< /map>
< /property>
< /property>
< /bean>
< /bean>
< bean id="bar" class="x.y.Bar" <emphasis role= "bold" > scope="thread"</emphasis> >
< bean id="bar" class="x.y.Bar" scope="thread">
< property name="name" value="Rick"/>
< property name="name" value="Rick"/>
< aop:scoped-proxy/>
< aop:scoped-proxy/>
< /bean>
< /bean>
@ -3546,7 +3528,7 @@ public final class Boot {
<interfacename > BeanFactory</interfacename> type if the field,
<interfacename > BeanFactory</interfacename> type if the field,
constructor, or method in question carries the
constructor, or method in question carries the
<interfacename > @Autowired</interfacename> annotation. For more
<interfacename > @Autowired</interfacename> annotation. For more
information, see the section entitled <xref
information, see <xref
linkend="beans-autowired-annotation" />.</para>
linkend="beans-autowired-annotation" />.</para>
<para > When an ApplicationContext creates a class that implements the
<para > When an ApplicationContext creates a class that implements the
@ -3792,11 +3774,13 @@ import org.springframework.beans.BeansException;
public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {
public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {
<lineannotation > // simply return the instantiated bean as-is</lineannotation>
<lineannotation > // simply return the instantiated bean as-is</lineannotation>
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean; <lineannotation > // we could potentially return <emphasis > any</emphasis> object reference here...</lineannotation>
return bean; <lineannotation > // we could potentially return <emphasis > any</emphasis> object reference here...</lineannotation>
}
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("Bean '" + beanName + "' created : " + bean.toString());
System.out.println("Bean '" + beanName + "' created : " + bean.toString());
return bean;
return bean;
}
}
@ -3862,8 +3846,7 @@ org.springframework.scripting.groovy.GroovyMessenger@272961</programlisting>
<para > Using callback interfaces or annotations in conjunction with a
<para > Using callback interfaces or annotations in conjunction with a
custom <interfacename > BeanPostProcessor</interfacename> implementation
custom <interfacename > BeanPostProcessor</interfacename> implementation
is a common means of extending the Spring IoC container. An example is
is a common means of extending the Spring IoC container. An example is
shown in the section entitled <xref
shown in <xref linkend= "metadata-annotations-required" /> which demonstrates the
linkend="metadata-annotations-required" /> which demonstrates the
usage of a custom <interfacename > BeanPostProcessor</interfacename>
usage of a custom <interfacename > BeanPostProcessor</interfacename>
implementation that ships with the Spring distribution which ensures
implementation that ships with the Spring distribution which ensures
that JavaBean properties on beans that are marked with an (arbitrary)
that JavaBean properties on beans that are marked with an (arbitrary)
@ -3907,8 +3890,7 @@ org.springframework.scripting.groovy.GroovyMessenger@272961</programlisting>
<emphasis > instances</emphasis> (the objects that are created from the
<emphasis > instances</emphasis> (the objects that are created from the
configuration metadata), then you instead need to use a
configuration metadata), then you instead need to use a
<interfacename > BeanPostProcessor</interfacename> (described above in
<interfacename > BeanPostProcessor</interfacename> (described above in
the section entitled <xref
<xref linkend= "beans-factory-extension-bpp" /> .</para>
linkend="beans-factory-extension-bpp" />.</para>
<para > Also, <literal > BeanFactoryPostProcessors</literal> are scoped
<para > Also, <literal > BeanFactoryPostProcessors</literal> are scoped
<emphasis > per-container</emphasis> . This is only relevant if you are
<emphasis > per-container</emphasis> . This is only relevant if you are
@ -4292,11 +4274,12 @@ dataSource.url=jdbc:mysql:mydb</programlisting>
<programlisting language= "java" > public class MovieRecommender {
<programlisting language= "java" > public class MovieRecommender {
private MovieCatalog movieCatalog;
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
@Autowired
public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
this.customerPreferenceDao = customerPreferenceDao;
}
}
@ -4460,7 +4443,8 @@ dataSource.url=jdbc:mysql:mydb</programlisting>
private CustomerPreferenceDao customerPreferenceDao;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
@Autowired
public void prepare(<emphasis role= "bold" > @Qualifier("main")</emphasis> MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
public void prepare(<emphasis role= "bold" > @Qualifier("main")</emphasis> MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
this.customerPreferenceDao = customerPreferenceDao;
}
}
@ -4668,14 +4652,14 @@ public @interface Offline {
public @interface MovieQualifier {
public @interface MovieQualifier {
String genre();
String genre();
Format format();
Format format();
}</programlisting>
}</programlisting>
<para > In this case <literal > Format</literal> is an enum:</para>
<para > In this case <literal > Format</literal> is an enum:</para>
<programlisting language= "java" > public enum Format {
<programlisting language= "java" > public enum Format {
VHS, DVD, BLURAY
VHS, DVD, BLURAY
}</programlisting>
}</programlisting>
@ -4766,7 +4750,8 @@ public @interface MovieQualifier {
if they are not annotated with Spring's
if they are not annotated with Spring's
<interfacename > @Qualifier</interfacename> annotation.</para>
<interfacename > @Qualifier</interfacename> annotation.</para>
<programlisting language= "xml" > < bean id="customAutowireConfigurer" class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
<programlisting language= "xml" > < bean id="customAutowireConfigurer"
class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
< property name="customQualifierTypes">
< property name="customQualifierTypes">
< set>
< set>
< value> example.CustomQualifier< /value>
< value> example.CustomQualifier< /value>
@ -4887,7 +4872,7 @@ public @interface MovieQualifier {
only recognizes the <interfacename > @Resource</interfacename> annotation
only recognizes the <interfacename > @Resource</interfacename> annotation
but also the JSR-250 <emphasis > lifecycle</emphasis> annotations.
but also the JSR-250 <emphasis > lifecycle</emphasis> annotations.
Introduced in Spring 2.5, the support for these annotations offers yet
Introduced in Spring 2.5, the support for these annotations offers yet
another alternative to those described in the sections on <link
another alternative to those described in <link
linkend="beans-factory-lifecycle-initializingbean">initialization
linkend="beans-factory-lifecycle-initializingbean">initialization
callbacks</link> and <link
callbacks</link> and <link
linkend="beans-factory-lifecycle-disposablebean">destruction
linkend="beans-factory-lifecycle-disposablebean">destruction
@ -5140,10 +5125,11 @@ public class JpaMovieFinder implements MovieFinder {
<row >
<row >
<entry > custom</entry>
<entry > custom</entry>
<entry > <literal > org.example.MyCustom TypeFilter</literal> </entry>
<entry > <literal > org.example.MyTypeFilter</literal> </entry>
<entry > A custom implementation of the
<entry > A custom implementation of the
<interfacename > org.springframework.core.type.TypeFilter</interfacename>
<interfacename > org.springframework.core.type
.TypeFilter</interfacename>
interface.</entry>
interface.</entry>
</row>
</row>
</tbody>
</tbody>
@ -5154,11 +5140,12 @@ public class JpaMovieFinder implements MovieFinder {
<interfacename > @Repository</interfacename> annotations and using "stub"
<interfacename > @Repository</interfacename> annotations and using "stub"
repositories instead.</para>
repositories instead.</para>
<programlisting language= "xml" > < beans ... >
<programlisting language= "xml" > < beans>
< context:component-scan base-package="org.example">
< context:component-scan base-package="org.example">
< context:include-filter type="regex" expression=".*Stub.*Repository"/>
< context:include-filter type="regex" expression=".*Stub.*Repository"/>
< context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
< context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
< /context:component-scan>
< /context:component-scan>
< /beans> </programlisting>
< /beans> </programlisting>
@ -5184,22 +5171,21 @@ public class JpaMovieFinder implements MovieFinder {
<literal > @Configuration</literal> annotated classes. Here is a simple
<literal > @Configuration</literal> annotated classes. Here is a simple
example:</para>
example:</para>
<programlisting > @Component
<programlisting language= "java" > @Component
public class FactoryMethodComponent {
public class FactoryMethodComponent {
@Bean @Qualifier("public")
@Bean @Qualifier("public")
public TestBean publicInstance() {
public TestBean publicInstance() {
return new TestBean("publicInstance");
return new TestBean("publicInstance");
}
}
public void DoWork()
public void doWork() {
{
// Component method implementation omitted
// Component method implementation omitted
}
}
}</programlisting>
}</programlisting>
<para > This class is a Spring component that has application-specific
<para > This class is a Spring component that has application-specific
code contained in its <methodname > D oWork</methodname> method. However,
code contained in its <methodname > d oWork</methodname> method. However,
it also contributes a bean definition that has a factory method
it also contributes a bean definition that has a factory method
referring to the method <methodname > publicInstance</methodname> . The
referring to the method <methodname > publicInstance</methodname> . The
<literal > @Bean</literal> annotation identifies the factory method and
<literal > @Bean</literal> annotation identifies the factory method and
@ -5208,37 +5194,39 @@ public class FactoryMethodComponent {
annotations that can be specified are <literal > @Scope</literal> ,
annotations that can be specified are <literal > @Scope</literal> ,
<literal > @Lazy</literal> , and custom qualifier annotations. Autowired
<literal > @Lazy</literal> , and custom qualifier annotations. Autowired
fields and methods are supported as previously discussed, with
fields and methods are supported as previously discussed, with
additional support for autowiring of @Bean methods:</para>
additional support for autowiring of <literal > @Bean</literal> methods:</para>
<programlisting language= "java" > @Component
<programlisting language= "java" > @Component
public class FactoryMethodComponent {
public class FactoryMethodComponent {
private static int i;
private static int i;
@Bean @Qualifier("public")
@Bean @Qualifier("public")
public TestBean publicInstance() {
public TestBean publicInstance() {
return new TestBean("publicInstance");
return new TestBean("publicInstance");
}
}
// use of a custom qualifier and autowiring of method parameters
// use of a custom qualifier and autowiring of method parameters
@Bean @BeanAge(1)
@Bean @BeanAge(1)
protected TestBean protectedInstance(@Qualifier("public") TestBean spouse, @Value("#{privateInstance.age}") String country) {
protected TestBean protectedInstance(@Qualifier("public") TestBean spouse,
TestBean tb = new TestBean("protectedInstance", 1);
@Value("#{privateInstance.age}") String country) {
tb.setSpouse(tb);
TestBean tb = new TestBean("protectedInstance", 1);
tb.setCountry(country);
tb.setSpouse(tb);
return tb;
tb.setCountry(country);
}
return tb;
}
@Bean @Scope(BeanDefinition.SCOPE_SINGLETON)
@Bean @Scope(BeanDefinition.SCOPE_SINGLETON)
private TestBean privateInstance() {
private TestBean privateInstance() {
return new TestBean("privateInstance", i++);
return new TestBean("privateInstance", i++);
}
}
@Bean @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Bean @Scope(value = WebApplicationContext.SCOPE_SESSION,
public TestBean requestScopedInstance() {
proxyMode = ScopedProxyMode.TARGET_CLASS)
return new TestBean("requestScopedInstance", 3);
public TestBean requestScopedInstance() {
}
return new TestBean("requestScopedInstance", 3);
}
}
}
</programlisting>
</programlisting>
@ -5304,7 +5292,7 @@ public class MovieFinderImpl implements MovieFinder {
scanner:</para>
scanner:</para>
</note>
</note>
<programlisting language= "xml" > < beans ... >
<programlisting language= "xml" > < beans>
< context:component-scan base-package="org.example"
< context:component-scan base-package="org.example"
name-generator="org.example.MyNameGenerator" />
name-generator="org.example.MyNameGenerator" />
@ -5341,7 +5329,7 @@ public class MovieFinderImpl implements MovieFinder {
scanner:</para>
scanner:</para>
</note>
</note>
<programlisting language= "xml" > < beans ... >
<programlisting language= "xml" > < beans>
< context:component-scan base-package="org.example"
< context:component-scan base-package="org.example"
scope-resolver="org.example.MyScopeResolver" />
scope-resolver="org.example.MyScopeResolver" />
@ -5356,7 +5344,7 @@ public class MovieFinderImpl implements MovieFinder {
interfaces, and targetClass. For example, the following configuration
interfaces, and targetClass. For example, the following configuration
will result in standard JDK dynamic proxies:</para>
will result in standard JDK dynamic proxies:</para>
<programlisting language= "xml" > < beans ... >
<programlisting language= "xml" > < beans>
< context:component-scan base-package="org.example"
< context:component-scan base-package="org.example"
scoped-proxy="interfaces" />
scoped-proxy="interfaces" />
@ -5528,8 +5516,7 @@ public class AppConfig {
<interfacename > @Configuration</interfacename> -annotated class supports
<interfacename > @Configuration</interfacename> -annotated class supports
the regular lifecycle callbacks. Any classes defined with the @Bean
the regular lifecycle callbacks. Any classes defined with the @Bean
annotation can use the @PostConstruct and @PreDestroy annotations from
annotation can use the @PostConstruct and @PreDestroy annotations from
JSR-250, see the section on <link
JSR-250, see <link linkend= "beans-factory-lifecycle-combined-effects" > JSR-250
linkend="beans-factory-lifecycle-combined-effects">JSR-250
annotations</link> for further details.</para>
annotations</link> for further details.</para>
<para > The regular Spring <link
<para > The regular Spring <link
@ -5540,13 +5527,13 @@ public class AppConfig {
<para > The standard set of <code > *Aware</code> interfaces such as
<para > The standard set of <code > *Aware</code> interfaces such as
<code > <link
<code > <link
linkend="beans-factory-aware- beanfactoryaware ">BeanFactoryAware</link> </code> ,
linkend="beans-beanfactory">BeanFactoryAware</link> </code> ,
<code > <link
<code > <link
linkend="beans-factory-aware-beannameaware ">BeanNameAware</link> </code> ,
linkend="beans-factory-aware">BeanNameAware</link> </code> ,
<code > <link
<code > <link
linkend="context-functionality-messagesource">MessageSourceAware</link> </code> ,
linkend="context-functionality-messagesource">MessageSourceAware</link> </code> ,
<code > <link
<code > <link
linkend="context-functionality-events ">ApplicationContextAware</link> </code> ,
linkend="beans-factory-aware ">ApplicationContextAware</link> </code> ,
and so on are also fully supported.</para>
and so on are also fully supported.</para>
<para > The <interfacename > @Bean</interfacename> annotation supports
<para > The <interfacename > @Bean</interfacename> annotation supports
@ -5716,7 +5703,7 @@ public CommandManager commandManager() {
the <code > name</code> attribute. <programlisting language= "java" > @Configuration
the <code > name</code> attribute. <programlisting language= "java" > @Configuration
public class AppConfig {
public class AppConfig {
@Bean(name = "bar ")
@Bean(name = "myFoo ")
public Foo foo() {
public Foo foo() {
return new Foo();
return new Foo();
}
}
@ -5732,7 +5719,7 @@ public class AppConfig {
<para > The <literal > context</literal> namespace introduced in Spring 2.5
<para > The <literal > context</literal> namespace introduced in Spring 2.5
provides a <literal > load-time-weaver</literal> element.<!-- Need to explain purpose of LoadTimeWeaver? Is this section ok here? --> </para>
provides a <literal > load-time-weaver</literal> element.<!-- Need to explain purpose of LoadTimeWeaver? Is this section ok here? --> </para>
<programlisting language= "xml" > < beans ... >
<programlisting language= "xml" > < beans>
< context:load-time-weaver/>
< context:load-time-weaver/>