Post details: A factory bean within Spring

Jan 25, 2006 : A factory bean within Spring

Spring is great at providing instances of a class to an object that depends on using one. You can also use one bean to provide multiple instances of the class to other beans:

    <bean id="list" class="java.util.ArrayList" singleton="false"/>
    <bean id="foo" class="my.FooImpl" />
        <property name="people" ref="list" />
        <property name="computers" ref="list" />
    <bean id="bar" class="my.BarImpl" />
        <property name="baz" ref="list" />

You can also have a "factory" so that a class Baz can create many Yaps (e.g. to populate a List<Yap>).

    <bean id="yap" class="my.YapImpl" singleton="false"/>
    <bean id="bar" class="my.BazImpl" />
        <lookup-method name="createNewYap" bean="yap" />

with Java code:

    public interface Yap { ... }
    public class YapImpl implements Yap { ... }
    public interface Baz { ... }
    public class BazImpl implements Baz {
        public getSomeYaps() {
            // I don't think Spring can provide a parameterized type implementation (e.g. the ArrayList<Yap>)
            List<Yap> yaps = new ArrayList<Yap>();
            for (int i = 0; i < 5; i++) {
                Yap yap = createNewYap();
            return yaps;
        // overridden by Spring
        protected Yap createNewYap()
            return null;

Comments, Pingbacks:

Comment from: peter k [Visitor]
yay for spring! IoC and dependency injection are the only way to go for managing your beans and pojos in a j2ee environment, especially the hooks it provides for out of container testing. between that and its jdbc/hibernate support (no more jdbc boilerplate), it makes my life a lot easier. dave, have you been using spring webflow or mvc yet?
Permalink 01/25/06 @ 23:34
Comment from: Dave Brondsema [Member] ·
No, but I am using the new web.portlet package, particularly the DispatcherPortlet. It's in 2.0 M2, but I've been dialoguing with the developer who contributed it and currently am using a pre-release version he sent me. We're using JSF in portlets. I'm using the jdbc/hibernate helper classes too.
Permalink 01/26/06 @ 08:19

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.
What is the 2-letter abbreviation for Michigan?
Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email & url)

<  September 2019  >
Mon Tue Wed Thu Fri Sat Sun
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29




Syndicate this blog XML

powered by