-
Jun 25th, 2012, 08:02 AM
#1
Related to dependency Injection
Hi Folks ,
could you please provide my sample example in which dependency Injection is happening through spring and How we are doing the same thing without spring I mean through simple java classes. As I want a very simple example through which I can explain it in simple terms to a lay man..!!
-
Jun 25th, 2012, 08:28 PM
#2
Here is an example for Spring DI: outputGenerator is injected by Spring per xml bean config below
public class OutputHelper
{
IOutputGenerator outputGenerator;
public void setOutputGenerator(IOutputGenerator outputGenerator){
this.outputGenerator = outputGenerator;
}
}
<beans>
<bean id="OutputHelper" class="com.abc.OutputHelper">
<property name="outputGenerator">
<ref bean="CsvOutputGenerator" />
</property>
</bean>
<bean id="CsvOutputGenerator" class="com.abc.CsvOutputGenerator" />
</beans>
Without Spring or other DI engine, DI will not happen. You may ask, you can create another class, e.g. OutputHelperMgr which create an instance of IOutputGenerator and call setOutputGenerator to 'inject' outputGenerator, well in this case you can think of this OutputHelperMgr as the simplest 'Spring DI framework' which manages the life cycle of the object or bean (IOutputGenerator in this case).
Hope this helps
Last edited by tannoy; Jun 26th, 2012 at 03:29 PM.
-
Jul 1st, 2012, 03:07 AM
#3
Hi Folks,
Yeah thanks a lot could you please show me how could this be achieved with out spring DI in normal java terms it will be a great help if you show in java classes itself, Thanks in advance
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules