Announcement Announcement Module
Collapse
No announcement yet.
inner beans; good or bad? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • inner beans; good or bad?

    This is covered in section 3.3.3.3 of the reference manual. The stuff that's commented out is the way I was doing it, following it is the inner bean way. In some ways I like the inner bean way because it's easier to see the dependencies, but the "russian doll" effect could be bothersome.

    I'm wondering if others have used inner beans and have any experiences, thoughts, etc.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <!--
        <bean id="dataSource"
            class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${db.driverClassName}" />
    
            <property name="username" value="${db.username}" />
    
            <property name="password" value="${db.password}" />
    
            <property name="url" value="${db.url}" />
        </bean>
    
        <bean id="sqlMapClient"
            class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <property name="configLocation" value="sqlmap-config.xml" />
                
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <bean id="printingLogDao"
            class="edu.berkeley.ist.aswa.cars.dao.db.PrintingLogDao">
            <property name="sqlMapClient" ref="sqlMapClient" />
        </bean>
    -->
        <bean id="printingLogDao"
            class="edu.berkeley.ist.aswa.cars.dao.db.PrintingLogDao">
            <property name="sqlMapClient">
                <bean id="sqlMapClient"
                    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                    <property name="configLocation" value="sqlmap-config.xml"/>
    
                    <property name="dataSource">
                        <bean id="dataSource"
                          class="org.apache.commons.dbcp.BasicDataSource">
                            <property name="driverClassName"
                              value="${db.driverClassName}" />
    
                            <property name="username" value="${db.username}" />
                    
                            <property name="password" value="${db.password}" />
                    
                            <property name="url" value="${db.url}" />
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </beans>

  • #2
    I like them a lot. Why when using a ProxyFactoryBean (or one of it's relatives) I don't want to have a [servicename]Target bean and a [servicename] bean in my configuration. It reduces the risk of using the wrong bean.

    However in your case I don't see the added value. If you now would add a new dao you cannot reuse your sqlMapClient because it is an inner bean and thus not visible for other beans.

    The same goes for the datasource, especially when you want to have different configurations for development, accept. or production it is going to be harder.

    So inner classes are they good yes, but you need to know when to use them.

    For your example I don't see the added value.

    Comment


    • #3
      Very good point; thanks!

      In this case the app is very dinky and this is very likely as far as it will go.

      Comment


      • #4
        I'd agree with Marten's points. I only use inner beans where they add value. It's similar to when your coding. Do you want a class to be visible to anything, the package or within the class? If you want to make sure nothing can access the bean directly an inner bean makes sense. I've found this to be quite common with TransactionProxyFactoryBean's when you don't want people to inject the target instead of the transactional proxy.

        Comment

        Working...
        X