Spring – Setter based Dependency Injection | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • In setter method at a time we only apply one single input and setter method value we can pass through attribute or child tag (<value>). We can not call property name again. If we call property again it will shows duplicatevalue exception. Below code is not valid. Container will not accept.
<bean id="t" class="com.Test">
	<property name = "name">
		<value>Code</value>
	</property>
	<property name = "name">
		<value>Factory</value>
	</property>
</bean>
  • For primitive type values we use value attribute but for secondary we use ref (reference) attribute.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create DI-Setter project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    • commons-logging-X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-expression-X.X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean id="t" class="com.codeFactory.beans.Test">
		<property name="name" value="Code" />
		<property name="age">
			<value>20</value>
		</property>
		<property name="gender" value="Male" />
	</bean>
</beans>

Test.java

package com.codeFactory.beans;

/**
 * @author code.factory
 *
 */
public class Test {
	String name;
	int age;
	String gender;

	public void setName(String name) {
		this.name = name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public void print() {
		System.out.println("Name " + name);
		System.out.println("Age " + age);
		System.out.println("Gender " + gender);
	}
}

Client.java

package com.codeFactory.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.codeFactory.beans.Test;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		
		Test t = (Test) context.getBean("t");
		t.print();
	}
}

Output:

Dec 11, 2020 8:41:59 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4bf558aa: startup date [Fri Dec 11 20:41:59 IST 2020]; root of context hierarchy
Dec 11, 2020 8:42:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/codeFactory/resources/spring.xml]
Name Code
Age 20
Gender Male

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s