<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Spring: Choosing the Right Dependency Injection Approach	</title>
	<atom:link href="https://myshittycode.com/2014/03/19/spring-choosing-the-right-dependency-injection-approach/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com/2014/03/19/spring-choosing-the-right-dependency-injection-approach/</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:03:15 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>
		By: Choon-Chern Lim		</title>
		<link>https://myshittycode.com/2014/03/19/spring-choosing-the-right-dependency-injection-approach/#comment-50</link>

		<dc:creator><![CDATA[Choon-Chern Lim]]></dc:creator>
		<pubDate>Wed, 26 Mar 2014 13:31:46 +0000</pubDate>
		<guid isPermaLink="false">http://myshittycode.com/?p=410#comment-50</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://myshittycode.com/2014/03/19/spring-choosing-the-right-dependency-injection-approach/#comment-48&quot;&gt;warmuuh&lt;/a&gt;.

@warmuuh, thank you for your feedback. You definitely have legit points here, and I fully respect your decision. In my case, I&#039;m leaning towards constructor-based injection for 2 reasons. 

First, I have peers who are learning Java for the first time in my team. The constructor-based injection &quot;feels&quot; more natural to them compared to field-based injection, even though we, the more experienced Spring users, know it is mostly syntactic sugar (with minor differences). I agree it is definitely cleaner when using field-based injection, but even with constructor-based injection, it isn&#039;t too bad. Think about it, if we are injecting 3 dependencies, with field-based injection, we have a total of 6 lines (3 lines for @Autowired and 3 lines for field declarations). With constructor-based injection, we usually have 3 lines more = 9 lines (3 lines for field declarations, 3 statement lines in the constructor, 1 line for constructor arguments, 1 line for @Autowired, and 1 line for closing curly brace).  

Second, if I use field-based injection, I can&#039;t set that field as &lt;code&gt;final&lt;/code&gt;. I&#039;m trying to make it a habit to always use &lt;code&gt;final&lt;/code&gt; as much as possible and constructor-based injection allows me to do that. With Java 8 moving towards functional-styled programming, the principle of immutability is going to be the key. Thus, I want to get my mindset ready for that, even though my corporation is only beginning to switch to Java 7... duh.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://myshittycode.com/2014/03/19/spring-choosing-the-right-dependency-injection-approach/#comment-48">warmuuh</a>.</p>
<p>@warmuuh, thank you for your feedback. You definitely have legit points here, and I fully respect your decision. In my case, I&#8217;m leaning towards constructor-based injection for 2 reasons. </p>
<p>First, I have peers who are learning Java for the first time in my team. The constructor-based injection &#8220;feels&#8221; more natural to them compared to field-based injection, even though we, the more experienced Spring users, know it is mostly syntactic sugar (with minor differences). I agree it is definitely cleaner when using field-based injection, but even with constructor-based injection, it isn&#8217;t too bad. Think about it, if we are injecting 3 dependencies, with field-based injection, we have a total of 6 lines (3 lines for @Autowired and 3 lines for field declarations). With constructor-based injection, we usually have 3 lines more = 9 lines (3 lines for field declarations, 3 statement lines in the constructor, 1 line for constructor arguments, 1 line for @Autowired, and 1 line for closing curly brace).  </p>
<p>Second, if I use field-based injection, I can&#8217;t set that field as <code>final</code>. I&#8217;m trying to make it a habit to always use <code>final</code> as much as possible and constructor-based injection allows me to do that. With Java 8 moving towards functional-styled programming, the principle of immutability is going to be the key. Thus, I want to get my mindset ready for that, even though my corporation is only beginning to switch to Java 7&#8230; duh.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marcus Pereira		</title>
		<link>https://myshittycode.com/2014/03/19/spring-choosing-the-right-dependency-injection-approach/#comment-49</link>

		<dc:creator><![CDATA[Marcus Pereira]]></dc:creator>
		<pubDate>Wed, 26 Mar 2014 13:03:30 +0000</pubDate>
		<guid isPermaLink="false">http://myshittycode.com/?p=410#comment-49</guid>

					<description><![CDATA[Fully agree with the previous comment.]]></description>
			<content:encoded><![CDATA[<p>Fully agree with the previous comment.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: warmuuh		</title>
		<link>https://myshittycode.com/2014/03/19/spring-choosing-the-right-dependency-injection-approach/#comment-48</link>

		<dc:creator><![CDATA[warmuuh]]></dc:creator>
		<pubDate>Wed, 26 Mar 2014 08:36:11 +0000</pubDate>
		<guid isPermaLink="false">http://myshittycode.com/?p=410#comment-48</guid>

					<description><![CDATA[If you use field-based @Autowired, you can still inject mocks in an easily and fast manner: E.g. if you use Mockito, you could use @Mock, @InjectMocks and the MockitoJUnitRunner and the mocks get injected automatically into your class.

I personally prefer using field-based @Autowired, because it adds less clutter to your code. If you have around 5 dependencies, you would have 5 fields only. If you then add the constructor or methods only for the injection to work, you basically add 10~15 lines of code that don&#039;t have *any* additional information for the reader, they are just organizational *white noise*.]]></description>
			<content:encoded><![CDATA[<p>If you use field-based @Autowired, you can still inject mocks in an easily and fast manner: E.g. if you use Mockito, you could use @Mock, @InjectMocks and the MockitoJUnitRunner and the mocks get injected automatically into your class.</p>
<p>I personally prefer using field-based @Autowired, because it adds less clutter to your code. If you have around 5 dependencies, you would have 5 fields only. If you then add the constructor or methods only for the injection to work, you basically add 10~15 lines of code that don&#8217;t have *any* additional information for the reader, they are just organizational *white noise*.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching 50/60 queries in 0.039 seconds using Disk

Served from: myshittycode.com @ 2026-02-18 02:17:46 by W3 Total Cache
-->