No golden rule. Two general schools of thought: Use constructor wherever you can and only use setter injection when you have circular dependencies. The idea is that constructors should define the ³contract² for creating the bean so you should put everything there unless you are unable to because of circular dependencies.
Other is use setter for everything (unless you need to call methods in the bean in the init and therefore have to have it in the constructor). Principle is that if you don¹t have a use case where you need to call methods in init on injected beans, you can just use setter for everything so you don¹t have to worry about which to use or refactor if circular dependencies arise. Here is some more reading: http://www.martinfowler.com/articles/injection.html#ConstructorVersusSetterI njection Best Wishes, Peter On 6/19/07 11:21 AM, "Jonathon Stierman" <[EMAIL PROTECTED]> wrote: > I¹m very new to the whole OO world, so please bear with me! > > I¹m playing around with ColdSpring on a new intranet site for my company. > It¹s one of the larger projects I¹ve worked on in terms of the number of CFC¹s > and dependencies, so I figured it¹d be a good fit for testing out ColdSpring. > > The problem I¹m running in to is that I¹m unsure whether I should be > specifying my dependencies as constructor-arg¹s or as properties. In some > cases, I don¹t have a choice (Domains is dependent on Contracts, and Contracts > is also dependent on Domains, so I have to use the <property> tag there). But > I was wondering if there¹s a ³golden rule² to follow for which ones to use > (when you have the luxury of choosing)? Right now, I¹m getting the feeling > that I¹ll be better off using the <property> tag for all my dependencies, then > I wouldn¹t even need to be aware of inter-dependent beans > > Here¹s my config.xml file, if that helps people understand: > > <!-- File loaded by ColdSpring to perform dependency injection --> > <beans> > <!-- Put definitions for your own beans and services here --> > <bean id="Domains" class="Components.Domains" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > > <constructor-arg name="statusesCFC"><ref > bean="DomainStatuses" /></constructor-arg> > <constructor-arg name="registrarsCFC"><ref > bean="DomainRegistrars" /></constructor-arg> > <constructor-arg name="typesCFC"><ref > bean="DomainTypes" /></constructor-arg> > <!-- Some Beans are dependent on the very same > beans that depend on them. CS to crashes when you try to wire inter-dependent > beans using the contstructor-arg parameter - as it cannot instantiate two > CFC's that both require eachother to be constructed. This is solved by > allowing the CFC's to be created and then after that, then setting the > properties. --> > <property name="emailsCFC"><ref > bean="DomainEmails" /></property> > <property name="measurementsCFC"><ref > bean="DomainMeasurements" /></property> > <property name="recordsCFC"><ref > bean="DomainRecords" /></property> > <property name="contractsCFC"><ref > bean="Contracts" /></property> > </bean> > <bean id="Clients" class="Components.Clients" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > <constructor-arg name="managersCFC"><ref > bean="Managers" /></constructor-arg> > </bean> > <bean id="ClientContacts" class="Components.ClientContacts" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > <constructor-arg name="clientsCFC"><ref > bean="Clients" /></constructor-arg> > </bean> > <bean id="Contracts" class="Components.Contracts" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > <constructor-arg name="clientsCFC"><ref > bean="Clients" /></constructor-arg> > <constructor-arg name="plansCFC"><ref > bean="HostingPlans" /></constructor-arg> > <constructor-arg name="passwordsCFC"><ref > bean="ContractPasswords" /></constructor-arg> > <property name="domainsCFC"><ref > bean="Domains" /></property> > </bean> > <bean id="ContractPasswords" > class="Components.ContractPasswords" singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > <!--<constructor-arg name="contractsCFC"><ref > bean="Contracts" /></constructor-arg>--> > </bean> > <bean id="DomainEmails" class="Components.DomainEmails" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > <property name="domainsCFC"><ref > bean="Domains" /></property> > </bean> > <bean id="DomainMeasurements" > class="Components.DomainMeasurements" singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > <constructor-arg name="typesCFC"><ref > bean="MeasurementTypes" /></constructor-arg> > <property name="domainsCFC"><ref > bean="Domains" /></property> > </bean> > <bean id="DomainRecords" class="Components.DomainRecords" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > <property name="domainsCFC"><ref > bean="Domains" /></property> > </bean> > <!-- non-dependent CFCs --> > <bean id="Managers" class="Components.Managers" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > </bean> > <bean id="HostingPlans" class="Components.HostingPlans" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > </bean> > <bean id="DomainTypes" class="Components.DomainTypes" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > </bean> > <bean id="Servers" class="Components.Servers" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > </bean> > <bean id="DomainRegistrars" > class="Components.DomainRegistrars" singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > </bean> > <bean id="DomainStatuses" class="Components.DomainStatuses" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > </bean> > <bean id="MeasurementTypes" > class="Components.MeasurementTypes" singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > </bean> > <!-- Service Beans --> > <bean id="Soap" class="Services.Soap" singleton="true"> > </bean> > <bean id="DNS" class="Services.DNS" singleton="true"> > <constructor-arg > name="dnsServer"><value>dns://192.168.0.2</value></constructor-arg> > <constructor-arg name="domainsCFC"><ref > bean="Domains" /></constructor-arg> > <constructor-arg name="recordsCFC"><ref > bean="DomainRecords" /></constructor-arg> > </bean> > <bean id="DomainServices" class="Services.DomainServices" > singleton="true"> > <constructor-arg > name="datasource"><value>cfCIS</value></constructor-arg> > <constructor-arg name="domainsCFC"><ref > bean="Domains" /></constructor-arg> > <constructor-arg name="serversCFC"><ref > bean="Servers" /></constructor-arg> > <constructor-arg name="emailsCFC"><ref > bean="DomainEmails" /></constructor-arg> > <constructor-arg name="measurementsCFC"><ref > bean="DomainMeasurements" /></constructor-arg> > <constructor-arg name="dnsCFC"><ref bean="DNS" > /></constructor-arg> > <constructor-arg name="soapCFC"><ref > bean="Soap" /></constructor-arg> > </bean> > </beans> > > Thanks for taking the time to read all that! > > Jonathon > > >
