Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The equivalent to the <code>Provider</code> of guice is the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-extension-factorybean" rel="nofollow"><code>FactoryBean</code></a> in Spring.</p> <p>From the documentation:</p> <blockquote> <p>The FactoryBean interface is a point of pluggability into the Spring IoC container's instantiation logic. If you have complex initialization code that is better expressed in Java as opposed to a (potentially) verbose amount of XML, you can create your own FactoryBean, write the complex initialization inside that class, and then plug your custom FactoryBean into the container.</p> </blockquote> <p><strong>Sample</strong></p> <pre><code>public class MyFactoryBean implements FactoryBean&lt;MyClassInterface&gt; { @Override public MyClassInterface getObject() throws Exception { return new MyClassImplementation(); } @Override public Class&lt;?&gt; getObjectType() { return MyClassInterface.class; } @Override public boolean isSingleton() { return true; } } </code></pre> <p>This is a good approach when you have external libraries and complex object creation to avoid long XML configuration to setup your beans.</p> <p>In order to use it within your context, you could just provide it as a normal bean within your context XML like:</p> <pre><code>... &lt;bean id="myClass" class="foo.bar.MyFactoryBean" /&gt; ... </code></pre> <p><strong>BUT</strong>, if your bean is quite simple to be instantiated (not lots of dependencies), you can setup it directly on the context XML like:</p> <pre><code>&lt;bean id="myClass" class="foo.bar.MyClassImplementation" /&gt; </code></pre> <p><strong>Alternative</strong></p> <p>If you use Spring 3 you could write a configuration class (class annotated with <code>@Configuration</code>) as documented <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-java" rel="nofollow">here</a>. With this approach you could do something like:</p> <pre><code>@Configuration public class MyConfiguration { @Bean public MyClassInterface getMyClass() { return new MyClassImplementation(); } } </code></pre> <p>This would add your instance to spring context and let autowire it within other classes. But some configuration is required, and its quite easy following the documentation provided.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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