<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Constructor-Based_Dependency_Injection</id>
	<title>Constructor-Based Dependency Injection - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Constructor-Based_Dependency_Injection"/>
	<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Constructor-Based_Dependency_Injection&amp;action=history"/>
	<updated>2026-04-23T05:52:52Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>http://rasimsen.com/index.php?title=Constructor-Based_Dependency_Injection&amp;diff=179&amp;oldid=prev</id>
		<title>Rasimsen: Created page with &quot;In the case of constructor-based dependency injection, the container will invoke a constructor with arguments each representing a dependency we want to set.  Spring resolves e...&quot;</title>
		<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Constructor-Based_Dependency_Injection&amp;diff=179&amp;oldid=prev"/>
		<updated>2018-06-03T11:26:26Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;In the case of constructor-based dependency injection, the container will invoke a constructor with arguments each representing a dependency we want to set.  Spring resolves e...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In the case of constructor-based dependency injection, the container will invoke a constructor with arguments each representing a dependency we want to set.&lt;br /&gt;
&lt;br /&gt;
Spring resolves each argument primarily by type, followed by name of the attribute and index for disambiguation. Let’s see the configuration of a bean and its dependencies using annotations:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
@Configuration&lt;br /&gt;
public class AppConfig {&lt;br /&gt;
 &lt;br /&gt;
    @Bean&lt;br /&gt;
    public Item item1() {&lt;br /&gt;
        return new ItemImpl1();&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    @Bean&lt;br /&gt;
    public Store store() {&lt;br /&gt;
        return new Store(item1());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The [[@Configuration]] annotation indicates that the class is a source of bean definitions. Also, we can add it to multiple configuration classes.&lt;br /&gt;
&lt;br /&gt;
The [[@Bean]] annotation is used on a method to define a bean. If we don’t specify a custom name, the bean name will default to the method name.&lt;br /&gt;
&lt;br /&gt;
For a bean with the default singleton scope, Spring first checks if a cached instance of the bean already exists and only creates a new one if it doesn’t. If we’re using the prototype scope, the container returns a new bean instance for each method call.&lt;br /&gt;
&lt;br /&gt;
Another way to create the configuration of the beans is through XML configuration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
&amp;lt;bean id=&amp;quot;item1&amp;quot; class=&amp;quot;org.baeldung.store.ItemImpl1&amp;quot; /&amp;gt; &lt;br /&gt;
&amp;lt;bean id=&amp;quot;store&amp;quot; class=&amp;quot;org.baeldung.store.Store&amp;quot;&amp;gt; &lt;br /&gt;
    &amp;lt;constructor-arg type=&amp;quot;ItemImpl1&amp;quot; index=&amp;quot;0&amp;quot; name=&amp;quot;item&amp;quot; ref=&amp;quot;item1&amp;quot; /&amp;gt; &lt;br /&gt;
&amp;lt;/bean&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rasimsen</name></author>
		
	</entry>
</feed>